Script help for g_form.addOption
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2022 01:41 PM
I am using a client script to trigger a script include, to call a rest message to ADO for a few fields. I am getting back the ADO ID and ADO Name. I need the ADO ID for the next rest message URL I am calling. I use the ADO Name for the options in a choice field. The client script is displaying the Rest Message results with g_form.addOption. This processing is working, but when I click on one of the options, and save, the value is no longer the ADO Name, but the ADO ID. And when I inspected the field I saw the value after selection had the ADO ID for both value and DisplayValue, like it was a new option. Any advice on what I am doing wrong?
Before Save:
After Save:
My Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
function GetSortOrder(prop) {
return function(a, b) {
if (a[prop] > b[prop]) {
return 1;
} else if (a[prop] < b[prop]) {
return -1;
}
return 0;
};
}
//populate project names based on organization selected
var ga = new GlideAjax('ADOProjectList');
ga.addParam('sysparm_name', 'getData');
ga.addParam('orgName', g_form.getValue('u_ado_org'));
ga.getXML(populateList);
function populateList(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var jsonData = JSON.parse(answer);
jsonData.value.sort(GetSortOrder("name"));
for (var i = 0; i < jsonData.value.length; i++) {
g_form.addOption('u_ado_project', jsonData.value[i].id, jsonData.value[i].name);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2022 02:31 PM
Without knowing the contents of answer, or specifically what's getting stored as values for id and name, the script/syntax looks fine. Have you tried running it without the sort, to take that out of the equation? Does it do the same thing when you make different selections? The only other thing I can think to try is add/dummy in a member with a shorter id (1234) and name (Bob) to see if selecting that one gives the same results - maybe it doesn't like the special characters in the name?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2022 03:31 PM
Hi, your script shows how you create your choice options, but doesn't seem to show how you are populating the options into your form or clarify how your rest scripts interacts with the form\these results?
Have you checked\confirmed that choice options are being created correctly in sys_choice?
Also, the creation of large numbers of choice options in this manner may not be a scalable approach,
perhaps you can clarify your business requirements\drivers and end to end process requirements as the indicated solution may not be ideal.