JSON not working in Catalog Client Script while building the catalog in CSM (Customer Service) scope

Raj87
Kilo Contributor

Hi,

I am trying to get a value from a table to field in catalog through glide ajax (script include) whereas the catalog and script include both are in CSM (Customer Service) application scope. I am using JSON object to capture the value in script include and parsing the same in client script to set the values to the Catalog fields, but it seems not working. Whereas, if I comment the JSON part and try to get values then the script is working. Could anyone please help on this. Thanks !!

Script Include:

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

SetFrontEndSupportGroup :function(){

var frontEndSupportGroup = new GlideRecord('sn_customerservice_fesa_and_fesg');
frontEndSupportGroup.addQuery('u_front_end_support_agency',this.getParameter('sysparm_fesa'));
frontEndSupportGroup.query();
var json = new JSON();
if(frontEndSupportGroup.next()){
var fesg = {'supportGroup':frontEndSupportGroup.getValue('u_front_end_support_group')};
return json.encode(fesg);
}

},

type: 'SetFrontEndSupportGroup'
});

 

Client Script:

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

var ga = new GlideAjax('SetFrontEndSupportGroup');
ga.addParam('sysparm_name','SetFrontEndSupportGroup');
ga.addParam('sysparm_fesa',g_form.getValue('front_end_support_agency'));
ga.getXML(SetFrontEndSupportGroup);

function setFrontEndSupportGroup(result){
var answer = result.responseXML.documentElement.getAttribute("answer");
var resultData = JSON.parse(answer);
g_form.setValue('contract',resultData.supportGroup);
}

}

4 REPLIES 4

Dubz
Mega Sage

the JSON() method encode doesn't work in scoped dev, replace with stringify:

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

SetFrontEndSupportGroup :function(){

var frontEndSupportGroup = new GlideRecord('sn_customerservice_fesa_and_fesg');
frontEndSupportGroup.addQuery('u_front_end_support_agency',this.getParameter('sysparm_fesa'));
frontEndSupportGroup.query();
//var json = new JSON();
if(frontEndSupportGroup.next()){
var fesg = {'supportGroup':frontEndSupportGroup.getValue('u_front_end_support_group')};
return JSON.stringify(fesg);
}

},

type: 'SetFrontEndSupportGroup'
});

 

Client Script:

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

var ga = new GlideAjax('SetFrontEndSupportGroup');
ga.addParam('sysparm_name','SetFrontEndSupportGroup');
ga.addParam('sysparm_fesa',g_form.getValue('front_end_support_agency'));
ga.getXML(SetFrontEndSupportGroup);

function setFrontEndSupportGroup(result){
var answer = result.responseXML.documentElement.getAttribute("answer");
var resultData = JSON.parse(answer);
g_form.setValue('contract',resultData.supportGroup);
}

}


Raj87
Kilo Contributor

Hi David,

 

Thanks for the information. Now, I re-wrote the function as you suggested but this time I am getting an error "Unhandled exception in GlideAjax"

 

Attached Scripts and Screenshots for your reference. Thanks a lot for your support !!

 

Script Include:

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

SetFrontEndSupportGroup :function(){

var frontEndSupportGroup = new GlideRecord('sn_customerservice_fesa_and_fesg');
frontEndSupportGroup.addQuery('u_front_end_support_agency',this.getParameter('sysparm_fesa'));
frontEndSupportGroup.query();


if(frontEndSupportGroup.next()){
var fesg = {'supportGroup':frontEndSupportGroup.getValue('u_front_end_support_group')};
return JSON.stringify(fesg);
}

},

type: 'SetFrontEndSupportGroup'
});

 

 

Client Script:

 

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

var ga = new GlideAjax('SetFrontEndSupportGroup');
ga.addParam('sysparm_name','SetFrontEndSupportGroup');
ga.addParam('sysparm_fesa',g_form.getValue('u_front_end_support_agency'));
ga.getXML(SetFrontEndSupportGroup);

function SetFrontEndSupportGroup(result){
var answer = result.responseXML.documentElement.getAttribute("answer");
var resultData = JSON.parse(answer);
g_form.setValue('u_front_end_support_group',resultData.supportGroup);
}

}

 

Also attached the error snippet which am getting in the CSM Portal. Thanks !!

 

Regards,

Raj

Hi,

Replace "SetFrontEndSupportGroup" by scopeName.SetFrontEndSupportGroup in your AJAX call constructor initialization at belw line

 

var ga = new GlideAjax('SetFrontEndSupportGroup');

 

In screenshot below , sn_cmp is the scope name. So you need to access your script include via API name is scoped app and not via NAME

 

Also, you need to make changes to JSON.stringify() to global.JSON.stringify() and same thing for parsing as well

 

Note: Please mark reply as correct if it has answered your original question 

 

find_real_file.png

Raj87
Kilo Contributor

Hi Deepak,

 

Thanks for the answer !!

I tried but again I got the same error. But I got the solution, I changed prototype to  Object.extendsObject(global.AbstractAjaxProcessor, {  });

It worked now. 

 

Working Script Include:

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

SetFrontEndSupportGroup :function(){

var frontEndSupportGroup = new GlideRecord('sn_customerservice_fesa_and_fesg');
frontEndSupportGroup.addQuery('u_front_end_support_agency',this.getParameter('sysparm_fesa'));
frontEndSupportGroup.query();
//var json = new JSON();
if(frontEndSupportGroup.next()){
var fesg = {'supportGroup':frontEndSupportGroup.getValue('u_front_end_support_group')};
return JSON.stringify(fesg);
}

},

type: 'SetFrontEndSupportGroup'
});

 

Working Client Script:

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

var ga = new GlideAjax('SetFrontEndSupportGroup');
ga.addParam('sysparm_name','SetFrontEndSupportGroup');
ga.addParam('sysparm_fesa',g_form.getValue('u_front_end_support_agency'));
ga.getXML(SetFrontEndSupportGroup);

function SetFrontEndSupportGroup(result){
var answer = result.responseXML.documentElement.getAttribute("answer");
var resultData = JSON.parse(answer);
g_form.setValue('u_front_end_support_group',resultData.supportGroup);
}

}

 

Regards,

Raj