- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2018 06:49 AM
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.
Solved! Go to Solution.
- Labels:
-
Service Catalog

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2018 04:02 PM
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);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2018 04:01 PM
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:
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2018 04:02 PM
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);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2020 12:50 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2020 06:03 AM
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.
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
});
}