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

Sagar Pagar
Tera Patron

Hi @Souvick A,

 

You need to return the Script include outside of while loop.

 

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'
});

 

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('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);

	}
}


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

Thank for reply, but still not getting the value on alert. The issue seems below nameid is not getting the value from the addParam 'nameValue' on the client script as it is not coming on the logs, that is why separately the gliderecord is returning value. can you please tell where it is wrong as this is the missing link

var nameid = this.getParameter('sysparam_nameValue');

 

Hi @Souvick A,

 

Try by replacing line as:

	ga.addParam('sysparam_nameValue', newValue);

 


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

Sorry, no luck. still getting the same error. The script include is not recognizing the sys id of the record under this.parameter.