REST-API - custom field id instead of name

Hi all,
I’m connecting to freshservice to retrieve Purchase orders with Items via API. Eveything works fine, I’m using Pyhon. I’m trying to generate an email that has in the body some fields for purchase orders.
For standard fields I’ve no problems.
The problem is with custom fields.

When I try to get the custom field named “cost_center” that is a dropdown menu from a purchase order with this code:

Centro di costo{po['**custom_fields**'].get('**cost_center**', 'Non specificato')}

instead of getting, for example, “Field merchandising service” that is one of values, I get the ID of the value, for example “16”.
How can I get the names as per the standard fields?

Any help would be appreciated

@Freddy, your thoughts on this?

Hi MaxCiarra and Bene_Immanuel,

When using the API to access custom fields, it’s typical for the system to return the ID instead of the display name. To work around this issue, you’ll want to create a mapping of the IDs to their respective names. You can do this by creating a dictionary that holds the ID-value pairs.

Here’s a sample of how you can achieve this:

  1. First, create a dictionary with the ID-value pairs for your “cost_center” dropdown menu. You’ll need to manually gather this information from your system.
cost_center_mapping = {
    '16': 'Field merchandising service',
    '17': 'Another cost center option',
    # Add more pairs as needed
}
  1. Next, update your code to use this dictionary to lookup the name corresponding to the ID that is returned from the API.
<th>Centro di costo</th><td>{cost_center_mapping.get(po['**custom_fields**'].get('**cost_center**', ''), 'Non specificato')}</td></tr>

Now, the code will look up the value in the dictionary using the received ID and display the name of the cost center instead of the ID.

I hope this helps! Let me know if you have any questions or need further clarification.

Hi, thanks for the response. The problem Is that I’ve 3 dropdown menus with many values that are updated quite often. The best way would be retrieve data through API to avoid errors and problems later.
Really there’s no way to get the values?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.