- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2025 04:43 AM
Hi All,
On a catalog form, I have below 3 fields. Here, Hotel code is lookup select box as it is a string field in the location table. And Brand and country are reference fields.
Requirement is to auto populate Brand and Country when user selects hotel code. I have written Script include and on change Client Script, but it is not working. I am not getting any values.
Scripts:
Script include:
var GetLocationDetails = Class.create();
GetLocationDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDetails: function() {
var holidex = this.getParameter('sysparm_holidex');
gs.log('Hotel Code received: ' + holidex);
var locationGR = new GlideRecord('cmn_location');
locationGR.addQuery('u_holidex', holidex);
locationGR.query();
if (locationGR.next()) {
var details = {
brand: locationGR.u_brand.getValue(),
country: locationGR.u_country_ref.getValue()
};
gs.log('Details found: ' + JSON.stringify(details));
return JSON.stringify(details);
}
gs.log('No details found for hotel code: ' + holidex);
return '';
}
});
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
console.log('Hotel Code selected: ' + newValue);
// Define the GlideAjax call
var ga = new GlideAjax('GetLocationDetails');
ga.addParam('sysparm_name', 'getDetails');
ga.addParam('sysparm_holidex', newValue);
ga.getXMLAnswer(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
console.log('Response from Script Include: ' + answer);
var details = JSON.parse(answer);
// Set the values of brand and country
g_form.setValue('brand', details.brand);
g_form.setValue('country', details.country);
});
}
Please assist.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2025 05:34 AM
so basically it's referring to cmn_location table
Ensure your lookup value - Sys Id
Lookup label Field - Holidex
In below screenshot instead of name, enter your field -> u_holidex
1) is your script include client callable
try this
var GetLocationDetails = Class.create();
GetLocationDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDetails: function() {
var holidex = this.getParameter('sysparm_holidex');
gs.info('Hotel Code received: ' + holidex); // this line if returns sysId then use sysId in query
var locationGR = new GlideRecord('cmn_location');
locationGR.addQuery('sys_id', holidex);
locationGR.query();
if (locationGR.next()) {
var details = {
brand: locationGR.u_brand.getValue(),
country: locationGR.u_country_ref.getValue()
};
gs.info('Details found: ' + JSON.stringify(details));
return JSON.stringify(details);
}
gs.log('No details found for hotel code: ' + holidex);
return '';
}
});
client script: check what came in console for JSON
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
console.log('Hotel Code selected: ' + newValue);
// Define the GlideAjax call
var ga = new GlideAjax('GetLocationDetails');
ga.addParam('sysparm_name', 'getDetails');
ga.addParam('sysparm_holidex', newValue);
ga.getXMLAnswer(function(response) {
console.log(response);
var details = JSON.parse(response);
// Set the values of brand and country
g_form.setValue('brand', details.brand);
g_form.setValue('country', details.country);
});
}
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
03-26-2025 05:17 AM