- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2023 10:29 PM
Hello All,
I have a Script include working on Change client script. But it is throwing Javascript error.
Script include:
Souvick
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2023 01:12 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2023 01:12 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2023 01:14 AM
Thanks alot Sagar for your help. Very much appreciated.
Regards
Souvick

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2023 12:21 AM
@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.