- 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-19-2023 10:44 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2023 10:53 PM
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');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2023 11:09 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2023 11:34 PM
Sorry, no luck. still getting the same error. The script include is not recognizing the sys id of the record under this.parameter.