I need to get translate from the label to ID for 3 different CRM fields (Territory, business type and industry type).
The only API I can find to get field values (and thus the choice IDs is the following:
GET /crm/sales/settings/sales_accounts/forms.json
But annoyingly it gives you the hierarchical structure shown in the GUI. This means I need to recursively navigate that entire structure looking for a particular field item id. I ended up writing the method (below for anyone who may need this) but I would like to know if there is a cleaner way.
Is there an API that I can get just a single field? I.e. something like this:
GET /crm/sales/settings/sales_accounts/forms/1/fields/
@freshworks, if no API exists, it really should. This is far too complicated for everyone to have to work thru.
Below is my solution for parsing this file if anyone is interested:
This function takes the 1st level list of fields:
(I left logging in this function for informational)
// Annoyingly need this due to Freshdesk having recursive fields are returned in hierarchical structure of GUI
function recurseFieldList(fields, field_id) {
console.log("Entering recurseFieldList with ", fields.length, " fields.")
found = fields.find(x => x.id === field_id)
if(found) return found.choices // As soon as we find it, return it
console.log("Didn't find it. Loop thru child fields")
for(let item of fields) {
console.log("Checking first child")
res=recurseFieldList(item.fields,field_id)
if(res) return res // As soon as we got back a non-null result, return it
}
}
To get the 1st level list of fields, I use this function:
(I left logging in this function for informational)
async function getCRMFieldValueId(field_name, field_id, field_value) {
url=crm_base_url + "/settings/sales_accounts/forms.json"
res = await asyncRequest("get", url, crm_headers)
fields=JSON.parse(res.response).forms[0].fields
item_choices=recurseFieldList(fields, field_id)
console.log(item_choices)
// We know there is at least 1 choice so find and extract it
item_id = item_choices.find(x => x.value === field_value).id
console.log("Got ", field_name, " ", item_id, " for ", field_value )
return item_id
}
And call above with something like this:
async function someFunction(payload) {
territory_id = await getCRMFieldValueId("territory_id", 'XXXf36-f547-498e-aXX6-67f1a7aXXX', record.custom_fields.territory)
}
Note that its important for the functions with the ‘async’ keyword to have that and to prefix API calls inside of those with ‘await’ so that it waits for results before proceeding.
NOTE: I got the field id (i.e. XXXf36-f547-498e-aXX6-67f1a7aXXX) by calling the GET /crm/sales/settings/sales_accounts/forms.json API with POSTMan. You may ask, why did I not just get the IDs for the choices. This is to avoid having to back and update code if people add new choices.