Script To Populate Reference Field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-24-2021 07:35 AM
I have a table which has a string field called "u_created_by" which currently has "joe.smith" which is the users ID. I have since created a reference field for reporting purposes that is populating the "requestor" going forward. I am having trouble writing a script to do a one time update that would take the users ID from the string field, query the user table and populate the new reference field "requestor".
Any help is appreciated!
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-24-2021 09:47 PM
Hi,
Can you try to update 10 records 1st and check if it works fine
updateRecords();
function updateRecords(){
var rec = new GlideRecord('tableName');
rec.addQuery('u_created_byISNOTEMPTY');
rec.setLimit(10);
rec.query();
while(rec.next()){
var userRec = new GlideRecord('sys_user');
if(userRec.get('user_name', rec.u_created_by)){
rec.u_requestor = userRec.sys_id;
rec.update();
}
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader