Script To Populate Reference Field

Nic Omaha
Tera Guru

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! 

 

5 REPLIES 5

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader