Trying to get multiple values from GlideAjax Script Include

Wayne Richmond
Tera Guru

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:

find_real_file.png

What's wrong with my code?

10 REPLIES 10

Mohith Devatte
Tera Sage
Tera Sage

Hello @Wayne Richmond ,

try this format instead if JSON in this case in 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()) {
            
            return checkSiteGr.site_name_fileld_backend_name.toString()+'|'checkSiteGr.rbm_field_back_end_name.toString();
        }
    },
    type: 'MABUserUtils'
});

Client Script :

unction onLoad() {

    var usr = g_user.userID;

    var ga = new GlideAjax('MABUserUtils');
    ga.addParam('sysparm_name', 'checkSite');
    ga.addParam('sysparm_user', usr);
    ga. getXML(showMessage);
}

function showMessage(response) {
    var values = response.responseXML.documentElement.getAttribute('answer').toString().split('|');
   
    alert(values[0]); // site name
alert(values[1])//rbm
}

PLEASE MARK MY ANSWER CORRECT IF IT HELPS YOU

Thanks Mohith, your solution does work, which is great. However, I was hoping to reuse this function elsewhere. I had assumed that by passing the whole object, I'd be able to choose which values I needed for a given requirement. In your solution, I need to add each field I need to the array, which is more work. Any idea how I can acheive my requirement?

Thanks again!

Hi @Wayne Richmond ,

1) Instead of JSON.encode, use JSON.stringify(obj)

2) Another approach to pass multiple values is you can push elements into array and return array as string.

 

Regards,
Sumanth

Hi Sumanth, thanks for your reply. I tried different variations of JSON.encode and JSON.stringify, but get them to work. If it isn't too much trouble, could you put your answer into context using my code samples please?