- 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:27 AM
Hello @Joshuu
To simplify, Create 1st variable i.e. "Hotel Code" as Reference field instead of lookup. This would also give you flexibility over time.
If this solution helped resolve your issue, please consider marking it as helpful or correct.
This will assist others in finding the solution faster and close the thread.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2025 05:32 AM
Hi @Shree_G ,
'Hotel Code' variable/question is a lookup select box type on the catalog form. and reference to the Holidex field under cmn_location table. And Holidex is a string field in location table. Like we would want to display the hotel code instead of location name here on the catalog form. But since that is not a display field, I have created a lookup select box question.
Could you please assist how to proceed further.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2025 05:17 AM
you can make the 1st variable as Reference and use Auto populate feature and no scripting required
Auto-populate a variable based on a reference type variable (Utah)
the 1st variable is referring to which table? share the variable configuration screenshot
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:25 AM - edited 03-26-2025 05:27 AM
Hi @Ankur Bawiskar ,
Hotel Code variable/question is a lookup select box type on the catalog form. and reference to the Holidex field under cmn_location table. And Holidex is a string field in location table. Like we would want to display the hotel code instead of location name here on the catalog form. But since that is not a display field, I have created a lookup select box question.
Could you please assist how to proceed further.
- 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