Two level dot walking to return multiple value

Brijmohan
Tera Contributor

Hi All,

I want to auto populate requestor location details like city, street, country on the catalog form based on the requestor select in requestor field. There are three fields City, Country and Street on catalog form where i want to return these values. 

Please help me with the script include and catalog client script code. I have idea about how we will do for single value like only City but not sure for multiple value.

Thanks in Advance!

1 ACCEPTED SOLUTION

MrMuhammad
Giga Sage

Hi,

Please try something like below.

1. Create an onChange catalog client script on the requester variable.

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

	var ga = new GlideAjax('UserInformation');
	ga.addParam('sysparm_name', "userInfo");
	ga.addParam('sysparm_userID', g_form.getValue('requested_for_variable')); // replace with your variable name inside getValue.
	ga.getXMLAnswer(function(answer){
		if(answer){
			var parser = JSON.parse(answer);
			g_form.setValue('city_variable', parser.city); // replace with your variable name 
			g_form.setValue('country_variable', parser.country); // replace with your variable name 
			g_form.setValue('street_variable', parser.street); // replace with your variable name

		}
	});
	//Type appropriate comment here, and begin script below

}

Create a script include:

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

	userInfo: function(){
		var obj = {};
		var id = this.getParameter('sysparm_userID');            
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id', id); 
		gr.query();
		if(gr.next()){
			obj['city'] = gr.getValue('city');
			obj['country'] = gr.getValue('country');
			obj['street'] = gr.getValue('street');
		}
		return JSON.stringify(obj);
	},
	type: 'UserInformation'
});

I hope that helps!

Regards,

Muhammad

Regards,
Muhammad

View solution in original post

2 REPLIES 2

MrMuhammad
Giga Sage

Hi,

Please try something like below.

1. Create an onChange catalog client script on the requester variable.

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

	var ga = new GlideAjax('UserInformation');
	ga.addParam('sysparm_name', "userInfo");
	ga.addParam('sysparm_userID', g_form.getValue('requested_for_variable')); // replace with your variable name inside getValue.
	ga.getXMLAnswer(function(answer){
		if(answer){
			var parser = JSON.parse(answer);
			g_form.setValue('city_variable', parser.city); // replace with your variable name 
			g_form.setValue('country_variable', parser.country); // replace with your variable name 
			g_form.setValue('street_variable', parser.street); // replace with your variable name

		}
	});
	//Type appropriate comment here, and begin script below

}

Create a script include:

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

	userInfo: function(){
		var obj = {};
		var id = this.getParameter('sysparm_userID');            
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id', id); 
		gr.query();
		if(gr.next()){
			obj['city'] = gr.getValue('city');
			obj['country'] = gr.getValue('country');
			obj['street'] = gr.getValue('street');
		}
		return JSON.stringify(obj);
	},
	type: 'UserInformation'
});

I hope that helps!

Regards,

Muhammad

Regards,
Muhammad

Ankur Bawiskar
Tera Patron
Tera Patron

@Brijmohan 

Should be simple enough

You can use onChange + GlideAjax and return multiple values from script include and then populate in catalog form variables

Get User Details based on the Logged in user/ User selected in Client Script

Regards
Ankur

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