Recent searches


No recent searches

Help retrieving custom organization field



Posted Jan 15, 2025

Hi everyone!

I'm building my first ZCLI app and I'm trying to pull the value of a custom organization dropdown field with the following line:

organization.custom_fields.arr

note: ‘arr’ is the field key

This works except it returns the tag, not the value, of the dropdown selection. How do I get it to show the value?


1

1

1 comment

image avatar

Greg Katechis

Zendesk Developer Advocacy

Hi Ben! That is technically the expected behavior, except we don't have that documented currently, which makes “expected” kind of confusing. I'll get something updated there to prevent this confusion down the line. 

 

I think the easiest way to get the value in this situation is to make the initial request a promise and chain it to a `client.get('organizationFields:arr.options')` call, then filter those results by the response that you get in the initial request by the `value` key, and then return the corresponding `label`that is associated with it. I haven't tested this code, but you can try something like this:

 

async function getLabelForValue(client) {
try {
const response = await client.get('organization.customField:arr');
const fieldValue = response['organization.customField:arr'];

const optionsResponse = await client.get('organizationField:arr.options');

const options = optionsResponse['organizationFields:arr.options'];
if (!Array.isArray(options)) {
throw new Error('Field not found or invalid format.');
}

const matchingOption = options.find(option => option.value === fieldValue);

if (matchingOption) {
return matchingOption.label;
} else {
throw new Error('No matching field value found.');
}
} catch (error) {
throw error;
}
}

(async () => {
try {
const label = await getLabelForValue(client);
console.log('Matching label:', label);
} catch (error) {
console.error('Error:', error.message);
}
})();

0


Please sign in to leave a comment.

Didn't find what you're looking for?

New post