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

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.

this is working when i am using the setquery aprameter but when i try to pass value from the client scrip to the script include -it is not setting the value again

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();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var jsonResponse= JSON.parse(responseBody);
jsonResponse = jsonResponse.result;
return JSON.stringify(jsonResponse);
},
    type: 'test'
});

 i tried to use these 2 

//var certi = this.getParameter("sysparm_id"); //please provide here some certificate ID hardcoded for testing
var certi="baa7af2b935402104ccd73118bba10a0" but these are giving error 
attached the error also
 

Hi @abhi56 

 

You can tweak the script include and client script as below for that :

 

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://lticoe8.service-now.com/api/now/table/incident/'+ certi );
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());
	var responseBody = response.getBody();

				var httpStatus = response.getStatusCode();

				var jsonResponse= JSON.parse(responseBody);
		
				jsonResponse = jsonResponse.result;
	gs.info(JSON.stringify(jsonResponse));
				return JSON.stringify(jsonResponse);
//return JSON.stringify(response.getBody());
},
    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.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.