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-30-2022 07:04 AM
When you want to get a record in JSON format this is pretty much the ideal use case for GlideQuery.
The following ajax script include would work with the provided client side script...
var MABUserUtils = Class.create();
MABUserUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkSite: function() {
return JSON.stringify(
new global.GlideQuery('sys_user')
.get(this.getParameter('sysparm_user'), ["name", "email"])
.orElse({})
);
},
type: 'MABUserUtils'
});