
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2019 10:42 AM
I want to update all of our sys_user records 'name' fields to remove the middle name. By default, the field calculation on our instance adds the middle name if one exists. Users don't prefer this so I commented that out of the calculated field. But I want to update all existing 200K records to last name, first name. I wanted to know if I can do a fix script to like this:
updateUserDisplayNames();
function updateUserDisplayNames() {
var query = 'middle_nameISNOTEMPTY';
var gr = new GlideRecord('sys_user');
gr.addEncodedQuery(query);
gr.setValue('name', gr.last_name + ', ' + gr.first_name);
gr.updateMultiple();
}
I haven't seen any examples of updateMultiple() doing this type of update, so I wanted to validate. I assume I can't because I bet updateMultiple() is not querying each record but just setting specific values like 'false' or specific integer or string.
Any Ideas on how I might speed up this operation?
Solved! Go to Solution.
- Labels:
-
Best Practices
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2019 11:13 AM
So it turns out when I updated the calculated field 'sys_user.name' it automatically updated all existing records. I tried it in both my dev and sandbox instances. So there is no need for a Fix Script after all.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2019 11:02 AM
ServiceNow allows to set only the same value in all records, which you update with respect of updateMultiple method. In other words, your script will not work.
The only way to update name field of all records of sys_user table will be enumeration of the records and the usage of gr.update() in the loop. What you can do to improve the performance is
- testing whether name is already correct and you skipping changes for the item.
- usage of gr.setWorkflow(true) and gr.autoSysFields(false); before gr.update(). It can improve the performance of update. See the article of documentation.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2019 11:13 AM
So it turns out when I updated the calculated field 'sys_user.name' it automatically updated all existing records. I tried it in both my dev and sandbox instances. So there is no need for a Fix Script after all.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2019 11:19 AM
ServiceNow allows to set only the same values on all records in case of usage of updateMultiple method. In other words, your current script will not work. So you will have to enumerate all users and call update in the loop. It will take really long time.
Nevertheless you can improve the performance using the following additional steps:
- test existing value of the name and skip call of update method on records, which already have correct name.
- consider to make the changes only on active and not locked user accounts (locked_out=false, active=true). If it's required you can clean-up inactive accounts later.
- test the performance of the changes by adding setLimit with some not large value like 100. You can run the script multiple times to change the name for the next 100 users. In the way you will know relatively good how long time take changes of all records
- you can try to include calls of gr.autoSysFields(false); and gr.setWorkflow(false); before calling of gr.update(). It can improve the performance of updates. See here for more details.