Autofill Location based on User

Vlad_Rozic
Kilo Contributor

Hey all.

I am very green when it comes to Service-Now and particularly Java Script (I'm an IT Admin, not a programmer 🙂 but my company has tasked me with setting up some portions of our new Service Now setup.

What I am trying to do is setup a script that will pull a user's location (as we have several offices around the world) once that user is selected.

To be more clear, we have a "leaver" app we are setting up for HR so that when a user leaves the company, they are able to go through a record producer window and select the username (which pulls from sys_user). I have created a Location field within the main "Leaver" window, and am wondering what type of client scripting would be needed to make it so that when the user is selected, it will autopopulate the location they are at within the location field.

We do have this setup for our "incident" request app, but the diffrerence there is that it autopopulates your own user account automatically on opening the app.. so you open the app, and whoever you're logged in as it will autopopulate that user name and at the same time populate their email address and location in lower fields. I looked to see how this functionality works in the client scripting, but again, I'm no programmer so I can't really figure out how to alter that code to make it work for the leaver process.

Would anyone be able to give me a couple pointers? If I'm unclear please let me know and I will try to give more details.

Thanks!

22 REPLIES 22

Not sure of what is happening there, but i'm suspecting an access control issue.

Are you doing this test with an admin account?


Vlad_Rozic
Kilo Contributor

I am yes.. I have full access from what I can see within Dev (have all options available within the left tool panel)




chetnapandey
Kilo Explorer

Hi Vlad,

I have tried the same and i used this script may be this will help you.



//Catalog Client Script
//Type:On change
//Field:who
function onChange(control, oldValue, newValue, isLoading)
{
var a =g_form.getValue('who');
var gr = new GlideRecord('sys_user');
gr.addQuery('name',a);
gr.query();
if(gr.next())
{
alert(gr.location);
g_form.setValue('location',gr.location);
}
}


Regards
Chetna


I appreciate the help Chetna.. I have tried this but made a change to the Who and Location portion of the script to reflect the actual variables, which in this case are Leaver_Who and Leaver_Location.

Before I had made standalone variables, however the Leaver Catalog Item was compiled with Variables that are part of a Variable Set. All the variables in that set are Leaver_****, and so I went in and made Who and Location to be part of that variable set for continuity's sake.

Even after adjusting in the script it still doesn't want to pull the location. Could it be that there is some other area that needs to be configured? As I had shown in my original posts, in the Incident window it pulls it properly, with the main difference being that it pulls the user who is logged in and populates the field automatically, so when I try, it pulls up my user record, and auto populates my location and email, where with Leaver, I have it set as a reference path, so you can click on the magnifying glass icon and it will show the list of our users in service now. Might this work better if this was not a reference but rather some other type? like a drop down etc?

I have attached the windows I have setup right now for reference.

Thank you for your help (and Marc as well).. I'm sorry this is such a troublesome issue! I'm a little disheartened that something that should seemingly be simple to achieve is turning out to be so difficult (hence my sad face in the image)


Hi Vlad,

As I have seen in the screen shot you provided today you have use the who variable as reference ... and previously the screen shot you provided in that the who variable was look up select box...


So, the script I wrote is based on look up select box .... So if you make your who variable to look up select box then it will be working fine.....