How to Auto-Populate Location on a Catalog Item based on a User's name?

Raza156
Kilo Contributor

Hey,

So I want to Auto-Populate the Location of a User based on the User's name that I input.

When I Select a User in the "Requested For" reference variable field, I want the Location to Auto-Populate to whatever that User's location is. The Reference for the Location Table is cmn_location.

Thanks.

find_real_file.png

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

you need script like below (modify bold with your variable names)

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var loc = g_form.getReference('Requested_For', populateReqForDetails);

function populateReqForDetails(loc) {
g_form.setValue('Requested_For_location', loc.location);
}
}

View solution in original post

4 REPLIES 4

Carl Fransen1
Tera Guru

In my imple,entation I have used the below to automate the pulling through of the manager value from the user, this is set as the 'defautl value' in a variable attached to a record producer.

javascript: gs.getUser().getManagerID();

 

Check out the gs.getUser function to see if this can also be done for location.  This is a nice way as it's not running any client side scripts.

The other way we did this was using a Catalog Client Script, see below:

find_real_file.png

This could be modified to grab the 'opened_for.location' - note this is for an HR Case, so if using the Incident table you'd need to use the correct field.

Hope this help.

 

Cheers

Carl.

Mike Patel
Tera Sage

you need script like below (modify bold with your variable names)

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var loc = g_form.getReference('Requested_For', populateReqForDetails);

function populateReqForDetails(loc) {
g_form.setValue('Requested_For_location', loc.location);
}
}

Hi Mike,

This is fine. But This way of auto-populating fields is not recommended to use in ServiceNow.

I hope better to use Ajax. can u pls suggest.

Yes, Best practice is to use GlideAJAX and that's why I created below article to help other users to switch from old code with new.

https://community.servicenow.com/community?id=community_article&sys_id=67cdfcd3db4c985c2be0a851ca961...

Make sure onchange field on client script is Requested_For

SI

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

	getUserInfo: function(){
		var user = this.getParameter('sysparm_user');
		var gr = new GlideRecord("sys_user");
		gr.get(user);
		var location = gr.getValue('location');
		var response = {};
		response.location = location;
		
		return JSON.stringify(response);
	},

	type: 'commonUtils'
});

Client

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	// Call the GA function
	var ga = new GlideAjax('commonUtils');
	ga.addParam('sysparm_name', "getUserInfo");
	ga.addParam('sysparm_user', newValue); //Onchange field is reference field to sys_user table
	ga.getXMLAnswer(function(answer){
		var response = JSON.parse(answer);
		g_form.setValue('Requested_For_location', response.location); //used for reference field to location table
		
	});
}