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

Thank you!! This seems to work..

there is only one caveat.. when I select the user using the Lookup Box, a small window opens with a bunch of numbers and letters.. once I close it, the location field gets populated as I am wanting..


you need to remove the line



alert(gr.location);


from Chetna's script


Vlad_Rozic
Kilo Contributor

Thanks again to both of you.. this is working as needed now which is great!!

I have on follow up question.. not sure if this would be too difficult, but with this leaver process, once you complete the form and "order" it, a workflow is initiated which creates a bunch of seperate tasks as part of the request.

In each task thats created it is to populate the information placed within the leaver process.. that location field is entered now automatically within the leaver form, but doesn't seem to show up properly when looking at each individual task.. I have added a location field to the task window, but its empty. Is there any easy way for the location provided in Leaver window to show in the task windows?


Hi,

For this you have to make changes in the workflow, In "catalog task activity " there is checkbox named as "advanced" check it then a window will appear in that just write single line of code that will fullfill your requirement.



task.location=current.variables.location;


Hey Chetna

task.location=current.variables.location; - with this do we need to replace variables or location with the requisite variable in the form?

I had tried to add this to one branch of a workflow but it didn't transfer the location that was selected from the leaver form.

In our case, we have a Variable set that the Leaver form uses.. it has several variables (Leaver_Who, Leaver_Where etc)
Once the form is filled out and these variables are all selected, there are quite a few workflows that are created for different task areas, (facilities, IT etc)

I understand that we would need to place this script in the workflow are as you'd described, but I'm just not sure how to write out the script IE which variables to place wher ein the script..

Does this make sense?