Autofill Location based on User
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-22-2013 12:15 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2013 03:56 AM
ok i better understand now.
First i would adapt the variable "who" to have it set as a reference to the user table (this will also improve performance as with the current setup, when you open the catalog items, it loads the user database in the drop down).
See who_variable.jpg image.
Then i will create a Catalog Client script with the following values:
- Type: onChange
- variable name: who
- script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var leaver = g_form.getReference('who');
g_form.setValue('location', leaver.location);
}
I did a test and it worked for me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2013 09:28 AM
Marc thanks for your help with this.. its much appreciated!~
I got in and was eager to try this yet it still doesn't seem to work on my end.
I have attached screenies of how its configured in my instance.
One other thing I noticed is that this Catalog Item leaver is utilizing a Varible Set as opposed to just varibles.. could this be causing trouble with the Client Script on the Catalog?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2013 09:40 AM
Quick update.. I changed the Variable in the Client Script to "who" and now it appears to be working as it shows "green" next to location.. however the Location is blank.. This, and the fact that when Location turns "green" there is a little expand icon that appears.. if I click it it opens another window that says "no record found" as if there are no locations on file. this is making me think that perhaps the users within the Table do not have a location value set yet or that the locations table is empty, but that seems unlikely.
I will go look if this is the case.. if not I'm going to try to update these records, however all of our users are pulled directly from our Active Directory and should have this info in the record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2013 12:26 PM
Vlad,
Variable name are case sensitive. Seeing you use capital for the first letter in the variable name, you should adapt the script. Hopefully this will solve your problem.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var leaver = g_form.getReference('Who');
g_form.setValue('Location', leaver.location);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2013 02:11 PM
Marc, I actually tested this as well earlier as I remembered the bit about it being case sensitive. It still seems like its not pulling the location.
The syntax appears correct, I have double checked that the letters that need to be capital are capital.. it still seems however that when you select a user in the Who field, the location field doesn't get populated, however the icon turns green and if you hit the magnify icon it will populate a list of the different locations being pulled from cmn_location table.
I wonder what is causing it to error here?