Glide Ajax returns null

suzanneswanson
Kilo Guru

I have a catalog client script which uses GlideAjax to call a script Include in order to get the name field from the sys_user table. The answer is null.  Can you please help me find the error?

The Script Include is Client Callable. The gs.logs show the correct data in the Script Include.

Script Include:

var getReferenceOnUserTable = Class.create();
getReferenceOnUserTable.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// _initialize: function() { },

//***************************** getName function ***********************************//
getName: function () {

    // gets the parameters that are passed into the script include
    var userId = this.getParameter('sysparm_userId');
    gs.log('xxxxxxxxxxxxxxxxxxxx userId coming in is ' + userId);

    // read the sc_category table to get the sys_id of the category that is passed in
    var gr = new GlideRecord('sys_user');
    gr.addQuery('sys_id', userId);
    gr.query();

    if(gr.next()) {
        var userName = gr.getValue('name');
        gs.log('xxxxxxxxxxxxxxxxxxxx userName is ' + userName);
        return userName;
    }
},
type: 'getReferenceOnUserTable'
});

 

the Client Script is onChange. The answer comes back null.

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

var userId = g_form.getValue('user');
alert('userId is ' + userId);

var ga = new GlideAjax('getReferenceOnUserTable'); // Script Include Name
ga.addParam('sysparm_name', 'getName'); // function defined in the script include
ga.addParam('sysparm_userId', 'userId'); // Parameter to pass the client side value to Script include
ga.getXML(informationParse); // The function to parse the return values

  function informationParse(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer"); 
    alert(answer);
    var answerx = JSON.parse(answer);
    alert(answerx);
    alert('after JSON.parse answer is ' + answerx);
    g_form.setValue('user_name_txt', answerx);
  }
}

 

1 ACCEPTED SOLUTION

Mahendra RC
Mega Sage

Hi suzanneswanson

Could you please try the below script and replace as per your need:

I have return this on Incident table on Caller field and updating the username in Description field of Incident form.

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	var userId = g_form.getValue('caller_id');
	
	var ga = new GlideAjax('getReferenceOnUserTable'); // Script Include Name
	ga.addParam('sysparm_name', 'getName'); // function defined in the script include
	ga.addParam('sysparm_userId', userId); // Parameter to pass the client side value to Script include
	ga.getXML(informationParse); // The function to parse the return values
	
	function informationParse(response) {
		var answer = response.responseXML.documentElement.getAttribute("username");
		alert(answer);
		g_form.setValue('description', answer);
	}
}

 

Script Include: Client Callable

var getReferenceOnUserTable = Class.create();
getReferenceOnUserTable.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// _initialize: function() { },

//***************************** getName function ***********************************//
getName: function () {
var userName = '';
    // gets the parameters that are passed into the script include
    var userId = this.getParameter('sysparm_userId'); 
    gs.log('xxxxxxxxxxxxxxxxxxxx userId coming in is ' + userId);

    // read the sc_category table to get the sys_id of the category that is passed in
    var gr = new GlideRecord('sys_user');
    gr.addQuery('sys_id', userId);
    gr.query();

    if(gr.next()) { 
        userName = gr.getValue('name');
        gs.log('xxxxxxxxxxxxxxxxxxxx userName is ' + userName);
        this.getRootElement().setAttribute('username', userName);
    }
},
type: 'getReferenceOnUserTable'
});

Please mark it correct/helpful, if it resolves your problem.

Thanks

View solution in original post

10 REPLIES 10

Runjay Patel
Giga Sage

Check out this video, it will clear all your doubts and help you to understand Script Include And Ajax Call queries in details.

Link: https://www.youtube.com/watch?v=qNiYWMMzC2o&t=209s&ab_channel=ServiceNowHelpdesk

It help you to understand below points.

 

  • Understand Script Include And Ajax Call
  • When, How and in which scenario we will use it.
  • Understand Synchronous (getXMLWait()) and aSysnchronous (getXML()) Ajax call and scenario based use of it.

 

Please mark reply as Helpful/Correct, if applicable. Thanks!!