Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Debug GlideAjax Catalog Client Script and Associated Script Include

alantpham
Kilo Contributor

I am a GlideAjax beginner so I am still lost even with the resources available on the topic. Below are the two scripts I am trying to debug. For context, in my catalog item I have a reference variable "suspended_user "that references the sys_user table. I am trying to autopopulate on change another variable "suspended_user_racfid" solely based on the user chosen for the suspended_user variable. Currently, the variable is returning null values when I try to change the suspended_user. Please edit and help debug the client script and script include. Both variables and the client script are within the same variable set within the catalog item.

Client Script: 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var ga = new GlideAjax('ScriptSuspendedRacfID');
ga.addParam('sysparm_name', 'getUserDetails');
ga.addParam('sysparm_suspended_user_racfid', newValue);
ga.getXML(getFunction);

function getFunction(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('suspended_user_racfid', answer); 
}
}

Script Include:

var ScriptSuspendedRacfID = Class.create();
ScriptSuspendedRacfID.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getUserDetails: function() {
var userId = this.getParameter('sysparm_suspended_user_racfid');
var userName = '';

var mygr = new GlideRecord('sys_user');
mygr.get(userId);
if (mygr.next()) {
userName = mygr.suspended_user;
}
return userName;

},

 

type: 'ScriptIncludeName'
});

4 REPLIES 4

vkachineni
Kilo Sage

If you are loading the record by .get(sys_id) you do not do a .next()

 

//try. untested.

var mygr = new GlideRecord('sys_user');
if (mygr.get(userId)) {
userName = mygr.suspended_user;
}

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Hi thanks for your suggestion. I made the change in the script include but still did not populate a value for suspended_user_racfid. Is there any changes that need to be made in the client script that you can suggest?

is the field name mygr.suspended_user correct?

is it a custom field?

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

var ScriptSuspendedRacfID = Class.create();
ScriptSuspendedRacfID.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserDetails: function() {
        var userId = this.getParameter('sysparm_suspended_user_racfid');
        var userName = '';

        var mygr = new GlideRecord('sys_user');        
        if (mygr.get(userId)) {
			gs.print(mygr.name);
            userName = mygr.suspended_user; //check the name of the field
        }
        return userName;

    },	
    type: 'ScriptSuspendedRacfID'
});
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022