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.

JSON PARSING not working in client script

abhi56
Tera Contributor

Hi all I have a requirement like whenever i get the json response from the script include i have fetch the short description value and put it in a variable in a record producer 

for that i have created a script include which is fetching the details from 3rd party tool 

then i am passing it to the client script 

when i am passing the whole response it is working (alert1)

then i try to access the short description it gives error 

i tried to use the comminuty post also

in line no 15 i used both
alert(parsedanswer.result.short_description);------this is giving blank and alert is also not coming
&
alert(parsedanswer.short_description);-------but this is giving undefined alert and setting the value as undefined
i also tried
parsedanswer.result[0].short_description ----this is also giving blank 
 
 
client script:
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }var ga = new GlideAjax('global.test');
ga.addParam('sysparm_name', 'getResponse');
ga.addParam('sysparm_id', newValue);
ga.getXML(getData);

}

function getData(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
var parsedanswer = JSON.parse(answer);
alert(parsedanswer.result.short_description);


g_form.setValue("short_description", parsedanswer.result.short_description);

}
 
script include:
var test = Class.create();
test.prototype = Object.extendsObject(AbstractAjaxProcessor, {getResponse: function() {
var request = new sn_ws.RESTMessageV2();
//var certi = this.getParameter("sysparm_id"); //please provide here some certificate ID hardcoded for testing
//var certi="baa7af2b935402104ccd73118bba10a0"
request.setHttpMethod('GET');
request.setQueryParameter("sysparm_query","sys_id=baa7af2b935402104ccd73118bba10a0^caller_id=62826bf03710200044e0bfc8bcbe5df1^short_descriptionLIKEabhinandan")
request.setQueryParameter("sysparm_fields","number,short_description")
var user = 'admin'; //provide here user name
var password = 'D55lYs/$tMYm'; //provide here password

request.setBasicAuth(user, password);
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');

var response = request.execute();
gs.info(response.getStatusCode());
gs.log(response.getBody());
return JSON.stringify(response.getBody());
},

    type: 'test'
});
1 ACCEPTED SOLUTION

Amit Verma
Kilo Patron
Kilo Patron

Hi @abhi56 

 

Please try with below code. I have tested it on my PDI and it is working as expected :

 

Script Include - 

 

var test = Class.create();
test.prototype = Object.extendsObject(AbstractAjaxProcessor, {getResponse:function() {
	var request = new sn_ws.RESTMessageV2();
//var certi = this.getParameter("sysparm_id"); //please provide here some certificate ID hardcoded for testing
//var certi="baa7af2b935402104ccd73118bba10a0"
request.setEndpoint('https://dev212357.service-now.com/api/now/table/incident' );
request.setHttpMethod('GET');
request.setQueryParameter("sysparm_query","sys_id=baa7af2b935402104ccd73118bba10a0^caller_id=62826bf03710200044e0bfc8bcbe5df1^short_descriptionLIKEabhinandan");
request.setQueryParameter("sysparm_fields","number,short_description");
var user = 'admin'; //provide here user name
var password = 'D55lYs/$tMYm'; //provide here password

request.setBasicAuth(user, password);
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');

var response = request.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var jsonResponse= JSON.parse(responseBody);
jsonResponse = jsonResponse.result;
return JSON.stringify(jsonResponse);
},
    type: 'test'
});

 

Client Script - 

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

   //Type appropriate comment here, and begin script below
   var ga = new GlideAjax('global.test');
ga.addParam('sysparm_name', 'getResponse');
ga.addParam('sysparm_id', newValue);
ga.getXML(getData);

}

function getData(response) {
	try{
var answer = response.responseXML.documentElement.getAttribute("answer");
var parsed = JSON.parse(answer);
var shortDescription = parsed[0].short_description;
g_form.setValue("short_description", shortDescription );  
	}
	catch(e){
        alert(e);
 
    }
}

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

View solution in original post

7 REPLIES 7

Anand Kumar P
Giga Patron

Hi @abhi56 ,

Try below script

var answer = response.responseXML.documentElement.getAttribute("answer");
var parsedanswer = JSON.parse(answer);
if (parsedanswer.result && parsedanswer.result.short_description) {
var shortDescription = parsedanswer.result.short_description;
alert(shortDescription);
g_form.setValue("short_description", shortDescription);

 

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand

kps sumanth
Mega Guru

Hello @abhi56 ,

 

Can you try making these changes to your code.

In script include add below lines:

var body = response.getBody();
var result = JSON.parse(body);
//gs.log("string "+JSON.stringify(result["result"]));
return JSON.stringify(result["result"]);
 
In the client script make these changes:
g_form.setValue("short_description", parsedanswer[0]["short_description"]);
 
Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Kps Sumanth

abhi56
Tera Contributor

these are not working @kps sumanth @Anand Kumar P 

Hello @abhi56 ,

 

Could you please share what you are getting in log messages and the alerts after making the code changes as I mentioned.