Fetch location from the requested for field

NelishaJ
Tera Contributor

I want to fetch the location of the user i select in Requested for field. How to do this?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@NelishaJ 

you can use onchange client script and use getReference with callback

something like this

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading) {
		return;
	}
	if(newValue == ''){
		g_form.clearValue('location');	// location variable name here
	}

	var ref = g_form.getReference('requested_for', callBackMethod);	// requestedFor variable name
}

function callBackMethod(ref){
	if(ref.location)
		g_form.setValue('location', ref.location); // location variable name here
}

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

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@NelishaJ 

use this and no script is required

Auto-populate a variable based on a reference type variable (Utah) 

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

Ankur Bawiskar
Tera Patron
Tera Patron

@NelishaJ 

you can use onchange client script and use getReference with callback

something like this

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading) {
		return;
	}
	if(newValue == ''){
		g_form.clearValue('location');	// location variable name here
	}

	var ref = g_form.getReference('requested_for', callBackMethod);	// requestedFor variable name
}

function callBackMethod(ref){
	if(ref.location)
		g_form.setValue('location', ref.location); // location variable name here
}

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

Anand Kumar P
Giga Patron
Giga Patron

Hi @NelishaJ ,

 

You have different ways to populate the requested for location 

 

1) Oob auto populate configuration 

1. Enable Auto-populate on a catalog variable, select a dependent reference-type variable, choose the reference table, and define the dot-walk path to location fetch data.

Save the configuration, and the selected variable will automatically populate based on the referenced value during form load or updates.

 

 

 

 

 

2) Script include with glide ajax

 

Client script

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

	// Call the GA function
	var ga = new GlideAjax('u_userInfoAjax');
	ga.addParam('sysparm_name', "getInfo");
	ga.addParam('sysparm_user', g_form.getValue('requested_by'));
	ga.getXMLAnswer(function(answer){
		// Save the return to a global variable
		var info = JSON.parse(answer);
		 g_form.setValue('requested_region', info.location);
	   //g_form.setValue('phone_number', info.phone);

	});

}

Script Includes

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

	getInfo: function(){
		var user = this.getParameter('sysparm_user');
		var gr = new GlideRecord("sys_user");
		gr.get(user);
		var location = gr.location.parent;
		var phone = gr.phone;

		var response = {};
		response.location = location;
		response.phone = phone;

		return JSON.stringify(response);
	},

	type: 'u_userInfoAjax'
});

 3) Using getreference method with calllbackfunction

 

var user = g_form.getReference('requested_for',doAlert); // doAlert is our callback function

}

function doAlert(caller) { //reference is passed

alert(user.location);

if (user.location != ''){
g_form.setValue('location',user.location);

}

If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand