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

ARG645
Tera Guru

After reading this thread, assuming that client callable is checked on the script include. I just have one question from my side. 

Do you see the output for below log in Script log statements? 

gs.log('xxxxxxxxxxxxxxxxxxxx userName is ' + userName);

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

Hi Mahendra - thank you so much for this, because this works.

I see you used this.getRootElement().setAttribute('username', userName);  Can you explain why this works and the return userName; does not work? 

 

Worked for me as well!

Guessing it has something to do with it returning a value instead of XML.

I used it for returning an array of values.

 

Thanks a lot!

stojdev
Giga Guru

Try this; (Script Include must be "Client callable")

Client Script;
function onLoad() {
	
	var ga = new GlideAjax('getUserInfo');
	ga.addParam('sysparm_user_id', g_user.userID);
        ga.addParam('sysparm_name', 'getUserName');
	ga.getXMLAnswer(callBack);
	
	function callBack(answer) {
		var thisAnswer = JSON.parse(answer);
		var name = thisAnswer.name;
                if (name) {
                   g_form.setValue('u_user_name_txt', name)
                }
	}
}

Script Include;
var getUserInfo = Class.create();
getUserInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getUserName : function () {
		
		var thisUser = this.getParameter('sysparm_user_id');
		
		var gr = new GlideRecord('sys_user');
		gr.get(thisUser);				
		var name = {
			name : gr.getValue('name') + ''
		};
		
		return new JSON().encode(name);
	},
	
	type: 'getUserInfo '
});