Trying to get multiple values from GlideAjax Script Include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2022 03:18 AM
Hi devs. I have a catalog item on the service portal that has a number of fields:
Site name [site_name] (references sys_user)
Site number [site_bun] (single line text)
RBM [rbm] (single line text)
If the current user is a site, I want it to poplate all of those fields.
I have followed best practice and tried to achieve this by calling a script include and using GlideAjax to get the data and pass it back to a catalog client script. I can do this to get a single field, however, I don't want to do multiple calls, so I've found advice on how to acheive this by passing the response as a JSON string and converting it back to a JS object where I can get the values I need. However, I've tried different variations of my code and can't get it to work. This is where I'm at:
Script include:
var MABUserUtils = Class.create();
MABUserUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkSite: function() {
var checkSiteGr = new GlideRecord('sys_user');
checkSiteGr.addQuery('sys_id', this.getParameter('sysparm_user'));
checkSiteGr.query();
if (checkSiteGr.next()) {
var json = new JSON();
var data = json.encode(checkSiteGr); //JSON formatted string
return data;
}
},
type: 'MABUserUtils'
});
Catalog client script:
function onLoad() {
var usr = g_user.userID;
var ga = new GlideAjax('MABUserUtils');
ga.addParam('sysparm_name', 'checkSite');
ga.addParam('sysparm_user', usr);
ga.getXMLAnswer(showMessage);
}
function showMessage(response) {
var answer = response;
answer = JSON.parse(answer);
alert(answer.name);
}
The alert I get on the form:
What's wrong with my code?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-11-2022 05:44 AM
Update as below and try:
Script Include :
var MABUserUtils = Class.create();
MABUserUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkSite: function() {
var obj = {};
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', this.getParameter('sysparm_user'));
gr.query();
if (gr.next()) {
obj.site_num = gr.field_name_for_siteNum; //update with backend value of field in your user table
obj.rbm = gr.field_name_for_rbm; //update with backend value of field in your user table
//you can similarly declare more fields if needed
}
return JSON.stringify(obj);
},
type: 'MABUserUtils'
});
Catalog client script:
function onLoad() {
var usr = g_user.userID;
var ga = new GlideAjax('MABUserUtils');
ga.addParam('sysparm_name', 'checkSite');
ga.addParam('sysparm_user', usr);
ga.getXMLAnswer(showMessage);
}
function showMessage(response) {
var obj = JSON.parse(response);
g_form.setValue('site_bun', obj.site_num);
g_form.setValue('rbm', obj.rbm);
//you can similarly set other field values if passed from script include
}
If that didnt work, put some logs and share your code what you tried.
Mark as correct and helpful if it solved your query.
Regards,
Sumanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-12-2022 08:57 AM
Feel free to reach out if you have further questions or else you can mark an answer as correct and helpful to close the thread so that it benefits future visitors also.
Regards,
Sumanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-11-2022 05:47 AM
It's better to use JSON structure while sending multiple values.
Have a look at Catalog Lookup Definition and you may not require script
Create a catalog lookup definition
Catalog Data Lookup Definition on any table, eliminating Catalog Client Scripting
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2022 09:57 PM
Hope you are doing good.
Did my reply answer your question?
If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-11-2022 06:00 AM
Hi,
I don't think you can pass entire GlideRecord as JSON object. Instead create JSON object with each attributes required at Client side. Refer the script
var MABUserUtils = Class.create();
MABUserUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkSite: function() {
var checkSiteGr = new GlideRecord('sys_user');
checkSiteGr.addQuery('sys_id', this.getParameter('sysparm_user'));
checkSiteGr.query();
if (checkSiteGr.next()) {
var json = {
"name":checkSiteGr.getDisplayValue("name"),
"location":checkSiteGr.getDisplayValue("location"),
"company":checkSiteGr.getDisplayValue("company")
};
// Add more attributes to the above object based on your requirement
var data = JSON.stringify(json); //JSON formatted string
return data;
}
},
type: 'MABUserUtils'
});
Palani