- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 07:40 AM
I have created two field on custom table
1)u_access_to_countries - List Collector and refernce to the country table
2)u_access_to_region - string field
I want to populate region of country in string field. Below script is working for the initial value of u_access_to_countries.But when i am selecting 2nd value that time it is not working.
Script include:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 07:44 AM - edited 01-28-2025 07:47 AM
Your Script Include needs to accommodate multiple sys_ids, and you want to return a joined array, not JSON:
var AccessCountryScript = Class.create();
AccessCountryScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRegionsForCountries: function() {
var country_sysID = this.getParameter('sysparm_access');
var gr_cntry = new GlideRecord('core_country');
gr_cntry.addQuery('sys_id', 'IN', country_sysID);
gr_cntry.query();
var answer = [];
while (gr_cntry.next()) {
answer.push(gr_cntry.u_region.toString());
}
return answer.join(', ');
},
type: 'AccessCountryScript'
});
Your Client Script just needs to set the string field to the returned value:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('AccessCountryScript');
ga.addParam('sysparm_name', 'getRegionsForCountries');
ga.addParam('sysparm_access', g_form.getValue('u_access_to_countries').split(','));
ga.getXMLAnswer(populateRegion);
function populateRegion(answer) {
// Set the value of the u_access_to_region field with the returned regions
g_form.setValue('u_access_to_region', answer);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 07:59 AM
Try the approach shared by @Brad Bowman and it should work
Share us the updates.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 07:44 AM - edited 01-28-2025 07:47 AM
Your Script Include needs to accommodate multiple sys_ids, and you want to return a joined array, not JSON:
var AccessCountryScript = Class.create();
AccessCountryScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRegionsForCountries: function() {
var country_sysID = this.getParameter('sysparm_access');
var gr_cntry = new GlideRecord('core_country');
gr_cntry.addQuery('sys_id', 'IN', country_sysID);
gr_cntry.query();
var answer = [];
while (gr_cntry.next()) {
answer.push(gr_cntry.u_region.toString());
}
return answer.join(', ');
},
type: 'AccessCountryScript'
});
Your Client Script just needs to set the string field to the returned value:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('AccessCountryScript');
ga.addParam('sysparm_name', 'getRegionsForCountries');
ga.addParam('sysparm_access', g_form.getValue('u_access_to_countries').split(','));
ga.getXMLAnswer(populateRegion);
function populateRegion(answer) {
// Set the value of the u_access_to_region field with the returned regions
g_form.setValue('u_access_to_region', answer);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 07:59 AM
Try the approach shared by @Brad Bowman and it should work
Share us the updates.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader