How to retrieve JSON object returned from script include to Client script?

Ankita Kolhe
Tera Contributor

Hi Community,

The following client scripting showing alert message as 'undefined'.

 

Client Script:-

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

//Type appropriate comment here, and begin script below
var service={'servicelink':'','servicegroup':''};
alert('hii' + newValue);
//alert(g_form.getValue(newValue).getDialogBox());
var globalInvAjax = new GlideAjax('GlobalInvestment');
globalInvAjax.addParam('sysparm_name','getServiceLineAndServiceGroup');
globalInvAjax.addParam('sysparm_servicenetwork',newValue);
globalInvAjax.getXML(getServiceDetails);

function getServiceDetails(response){
answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);
service=answer;
//alert(JSON.parse(answer));
//alert(answer.servicegroup);
//service=JSON.parse(answer);
alert(service.serviceline);
}



}

 

 

 

Script Include:-

 

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

getServiceLineAndServiceGroup : function(){
gs.addInfoMessage('hello from sc');
gs.addInfoMessage(this.getParameter(' '));
var service={'serviceline':'','servicegroup':''};
var globalInv = new GlideRecord('u_global_investment_data');
globalInv.addQuery('sys_id',this.getParameter('sysparm_servicenetwork'));
globalInv.query();

if(globalInv.next()){
gs.addInfoMessage('1 '+globalInv.u_service_line);
gs.addInfoMessage('2 '+globalInv.u_service_group);
service.serviceline=globalInv.u_service_line;
service.servicegroup=globalInv.u_service_group;
}
var ser=service.serviceline + ', '+service.servicegroup;
//return new global.JSON().encode(service);
//return JSON.stringify(service);
return service;
},


type: 'GlobalInvestment'
});

 

 

Thanks,

Ankita 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Ankita Kolhe 

update as this

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

	getServiceLineAndServiceGroup : function(){
		var service = {};
		var globalInv = new GlideRecord('u_global_investment_data');
		globalInv.addQuery('sys_id',this.getParameter('sysparm_servicenetwork'));
		globalInv.query();
		if(globalInv.next()){
			service.serviceline = globalInv.u_service_line.toString();
			service.servicegroup = globalInv.u_service_group.toString();
		}
		return JSON.stringify(service);
	},

	type: 'GlobalInvestment'
});

client script

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

	var globalInvAjax = new GlideAjax('GlobalInvestment');
	globalInvAjax.addParam('sysparm_name','getServiceLineAndServiceGroup');
	globalInvAjax.addParam('sysparm_servicenetwork',newValue);
	globalInvAjax.getXML(getServiceDetails);

	function getServiceDetails(response){
		var answer = response.responseXML.documentElement.getAttribute("answer");
		var jsonData = JSON.parse(answer);	
		alert(jsonData.serviceline);
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Sagar Pagar
Tera Patron

Hi @Ankita Kolhe,

In your script include you are returning service, it should be ser. Try it and let me know.

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Hi Sagar,

I want to return the object & retrieve the same on client script.

 

Thanks,

Ankita

Ankur Bawiskar
Tera Patron
Tera Patron

@Ankita Kolhe 

update as this

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

	getServiceLineAndServiceGroup : function(){
		var service = {};
		var globalInv = new GlideRecord('u_global_investment_data');
		globalInv.addQuery('sys_id',this.getParameter('sysparm_servicenetwork'));
		globalInv.query();
		if(globalInv.next()){
			service.serviceline = globalInv.u_service_line.toString();
			service.servicegroup = globalInv.u_service_group.toString();
		}
		return JSON.stringify(service);
	},

	type: 'GlobalInvestment'
});

client script

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

	var globalInvAjax = new GlideAjax('GlobalInvestment');
	globalInvAjax.addParam('sysparm_name','getServiceLineAndServiceGroup');
	globalInvAjax.addParam('sysparm_servicenetwork',newValue);
	globalInvAjax.getXML(getServiceDetails);

	function getServiceDetails(response){
		var answer = response.responseXML.documentElement.getAttribute("answer");
		var jsonData = JSON.parse(answer);	
		alert(jsonData.serviceline);
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

Thanks for the response.

Tried with the above code but alert message  is showing as [object Object].