- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2014 05:10 PM
Hi all,
A bit of a beginner when it comes to javascript so I was hoping someone may have a recommendation for me. I've also found various tidbits of information out in the wiki on field auto-populating on forms but couldnt figure out how to piece the information together to make a workable code.
I have a form in the service catalog for employee changes. The first field in the form is a reference field to sys_user so that a user can be chosen that will need the changes. Below that I have reference fields for the old information such as department, manager, etc. What I would like to accomplish is that based on the user chosen in the field it will auto populate the rest of the fields with their current information. In the past we have used similar bits of code such as below on each variable to auto populate fields but gs.getUserID grabs the user filling out the form.
javascript:var id = gs.getUserID();
var user = new GlideRecord('sys_user');
user.addQuery("sys_id", id);
user.query();
var rc = "";
if (user.next())
rc = user.manager;
rc;
I am guessing I may need to do a Catalog Client Script using onCellEdit or onLoad? But I cant seem to find the correct setup of script to pull the information based on the Reference Lookup sys_user variable of name. Any help or guidance would be much appreciated, thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2014 05:28 PM
You are on the right track!
Use an onChange catalog client script. It should work for your needs.
var id = gs.getUserID(); is only going to give you the person that is using the form. This person may or may not be the same as the person who is in the first field... so it is better to get the value of the first field instead.
"I have a form in the service catalog for employee changes. The first field in the form is a reference field to sys_user so that a user can be chosen that will need the changes"
Try this:
function onChange(control, oldValue, newValue, isLoading) {
var id = g_form.getValue('u_first_field');//replace 'u_first_field' with the name of your reference field.
var user = new GlideRecord('sys_user');
user.addQuery('sys_id',id);
user.query();
if ( user.next() ) {
g_form.setValue('u_manager_field', user.manager);
g_form.setValue('u_last_name', user.last_name);
g_form.setValue('u_whatever', user.field_on_sys_user);
}
}
For the onChange script variable, choose the user reference field ( the first field on the form ).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2017 07:43 AM
Thanks so much for providing this code!
I was able to populate a request item field with the requester's department but it's showing the sys_id instead of the department name. Any ideas as to how to get the actual name resolved would be greatly appreciated.
Thanks in advance!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2017 01:59 AM
yes I did the same thing but still its not auto populating. Can you suggest possible reasons.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2017 03:35 AM
Hello Justin,
I have a similar requirement, Can we do this via BR's. I have to auto populate user location and i have done this via client script. But for some of my incidents in list view it is not showing location value.
Could you please guide me on this issue...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2022 03:12 AM
Seems to be not working i tried
I have change number dropdown. if I clicked any of change request it should set value for another field.Its not working in service portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2016 06:32 AM
I'm afraid the solutions provided are not really best practices. You should avoid GlideRecord and GetReference from a client script. Instead go for an asynchronous ajax call.