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.

Glide Ajax not working ad throwing Javascript error in browser console

Souvick A
Tera Contributor

Hello All,

 

I have a Script include working on Change client script. But it is throwing Javascript error.

Script include:

var PracticeUtilsNew = Class.create();
PracticeUtilsNew.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
 
getCost: function() {
        var nameid = this.getParameter('sysparam_nameValue');
gs.log('Value of Souvick1:'+nameid);
        var getName = new GlideRecord('cmn_cost_center');
        getName.addQuery('sys_id', nameid);
        getName.query();
        while (getName.next()) {
          var JsonObj = {};
JsonObj.code = getName.getValue('code');
JsonObj.account_number = getName.getValue('account_number');
return JSON.stringify('JsonObj');
        }
    },
    type: 'PracticeUtilsNew'
});
 
The gliderecord is working separately in background script.
 
Onchange client script:
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
 
    var nameValue = g_form.getValue('name');
alert('Name is:'+nameValue);
    var ga = new GlideAjax('PracticeUtilsNew');
    ga.addParam('sysparam_name', 'getCost');
    ga.addParam('sysparam_nameValue', nameValue);
    ga.getXML(callback);
 
function callback(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
 
var JsonObj = JSON.parse(answer);
alert(answer);
g_form.setValue('code',JsonObj.code);
g_form.setValue('account_number',JsonObj.account_number);
 
}
}
Null value is coming in answer on the alert.
 
Any guidance is helpful
 
Regards
Souvick
1 ACCEPTED SOLUTION

Hi @Souvick A,

 

1) In return statement JSON should not be inside quotes.

 

return JSON.stringify(JsonObj);

 

2) replace sysparam_name with sysparm_name and sysparam_nameValue with sysparm_nameValue

 

 

Try this updated scripts.

Client scripts:

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

	var nameValue = g_form.getValue('name');
	alert('Name is:' + nameValue);
	var ga = new GlideAjax('PracticeUtilsNew');
	ga.addParam('sysparm_name', 'getCost');
	ga.addParam('sysparm_nameValue', nameValue);
	ga.getXML(callback);

	function callback(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");

		var JsonObj = JSON.parse(answer);
		alert(answer);
		g_form.setValue('code', JsonObj.code);
		g_form.setValue('account_number', JsonObj.account_number);

	}
}

 

Script include:

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

	getCost: function() {
		var nameid = this.getParameter('sysparam_nameValue');
		// gs.info('Value of Souvick1:' + nameid);
		var JsonObj = {};

		var getName = new GlideRecord('cmn_cost_center');
		getName.addQuery('sys_id', nameid);
		getName.query();
		while (getName.next()) {
			JsonObj.code = getName.getValue('code');
			JsonObj.account_number = getName.getValue('account_number');
		}
		return JSON.stringify(JsonObj);
	},
	type: 'PracticeUtilsNew'
});

 


If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers! 👍🏻
Thanks,
Sagar Pagar

The world works with ServiceNow

View solution in original post

7 REPLIES 7

Hi @Souvick A,

 

1) In return statement JSON should not be inside quotes.

 

return JSON.stringify(JsonObj);

 

2) replace sysparam_name with sysparm_name and sysparam_nameValue with sysparm_nameValue

 

 

Try this updated scripts.

Client scripts:

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

	var nameValue = g_form.getValue('name');
	alert('Name is:' + nameValue);
	var ga = new GlideAjax('PracticeUtilsNew');
	ga.addParam('sysparm_name', 'getCost');
	ga.addParam('sysparm_nameValue', nameValue);
	ga.getXML(callback);

	function callback(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");

		var JsonObj = JSON.parse(answer);
		alert(answer);
		g_form.setValue('code', JsonObj.code);
		g_form.setValue('account_number', JsonObj.account_number);

	}
}

 

Script include:

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

	getCost: function() {
		var nameid = this.getParameter('sysparam_nameValue');
		// gs.info('Value of Souvick1:' + nameid);
		var JsonObj = {};

		var getName = new GlideRecord('cmn_cost_center');
		getName.addQuery('sys_id', nameid);
		getName.query();
		while (getName.next()) {
			JsonObj.code = getName.getValue('code');
			JsonObj.account_number = getName.getValue('account_number');
		}
		return JSON.stringify(JsonObj);
	},
	type: 'PracticeUtilsNew'
});

 


If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers! 👍🏻
Thanks,
Sagar Pagar

The world works with ServiceNow

Thanks alot Sagar for your help. Very much appreciated.

 

Regards

Souvick

Sandeep Rajput
Tera Patron
Tera Patron

@Souvick A Please update the script include source code as follows.

 

var PracticeUtilsNew = Class.create();
PracticeUtilsNew.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
 
getCost: function() {
        var nameid = this.getParameter('sysparam_nameValue');
gs.log('Value of Souvick1:'+nameid);
        var getName = new GlideRecord('cmn_cost_center');
        getName.addQuery('sys_id', nameid);
        getName.query();
        while (getName.next()) {
          var JsonObj = {};
JsonObj.code = getName.getValue('code');
JsonObj.account_number = getName.getValue('account_number');
        }
return JSON.stringify(JsonObj); //This was the line causing issue instead of 'JsonObj' you should write JsonObj. 
    },
    type: 'PracticeUtilsNew'
});

 

The line which was causing the issue was 

return JSON.stringify('JsonObj');

Instead of writing the JsonObj in quotes ' ', it should be written without quotes, as follows.

return JSON.stringify(JsonObj);

 

The above line converts an object into a plain string.

 

Hope this helps.