
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2017 08:22 PM
Service Now Gurus,
I have the following issue: We have recently added our hardware assets to SN, and we have assigned them to users, however we have noticed the Location field isn't being updated and long story short, we weren't pulling across our users location via our LDAP import, this has now been fixed and each of our user records is now showing a location.
For most of our assets, there is an assigned user but an empty location field, if I manually remove the assigned to user and save the record then add in the original assigned to user the location field updates, but I don't want to do that for all our assets, I have put together the following script that looks to work in our Dev environment but I'm cautious about running in our Prod environment before others give it the ok (first script within SN) or in case there is better way to achieve what I am trying to.
Script below:
var asset = new GlideRecord('alm_asset');
asset.addNullQuery('location');
asset.addNotNullQuery('assigned_to');
asset.query();
while (asset.next())
{
var assigned = asset.getDisplayValue('assigned_to');
var user = new GlideRecord('sys_user');
user.addQuery('name',assigned);
user.query();
while (user.next())
{
var sysId = user.getUniqueValue();
var u_user = gs.getUser().getUserByID(sysId);
var u_location = u_user.getLocation();
}
gs.log("Location: " + u_location);
asset.setValue('location', u_location);
asset.update();
}
So the thinking is I select all records that are assigned but don't have a location, I then find the assigned to user for the asset and then find their user sys_id, from there I query the user table to get the users location and then I set the location field in the asset record to match that of the user and then update the record.
Please pick apart the code and let me know if there is a better way to do this.
Thanks in advance,
John.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2017 08:32 PM
Hello John,
Below is the modified script. This will fetch assigned_to user location and update those records with empty location.
var asset = new GlideRecord('alm_asset');
asset.addNullQuery('location');
asset.addNotNullQuery('assigned_to');
asset.query();
while (asset.next())
{
asset.location = asset.assigned_to.location;
asset.setWorkflow(false); //To turn off the notification or business rule on the alm_asset table.
asset.update();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2017 09:49 PM
You are very welcome. Please go through the below link for more info on dot-walking.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2017 02:32 PM
Let me know if that answered your question. If so, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list. Thank you
How To Mark Answers Correct From Community Inbox
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2020 10:47 AM
Very Helpful. Modified it to mass update assets where a building location change impacted locations of all users. Ran the code as a fix script.
var asset = new GlideRecord('alm_asset');
asset.addNotNullQuery('location');
asset.addNotNullQuery('assigned_to');
asset.query();
while (asset.next())
{
asset.location = asset.assigned_to.location;
asset.setWorkflow(false); //To turn off the notification or business rule on the alm_asset table.
asset.update();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2017 08:35 PM
- var asset = new GlideRecord('alm_asset');
- asset.addNullQuery('location');
- asset.addNotNullQuery('assigned_to');
- asset.query();
- while (asset.next())
- {
- var assigned = asset.getDisplayValue('assigned_to');
- var user = new GlideRecord('sys_user');
- user.addQuery('name',assigned);
- user.query();
- while (user.next())
- {
- var sysId = user.getUniqueValue();
- var u_user = gs.getUser().getUserByID(sysId);
- var u_location = u_user.getLocation();
- }
- gs.log("Location: " + u_location);
- asset.u_location=u_location;
- asset.update();
- }
have you created custom field u_location?we have OOTB location field in sys_user table

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2017 08:37 PM
Location can be fetched via dot-walking and also in the post-John is trying to update the location field only.