How do I change data using a script to update a user field.

philkindrick
Kilo Contributor

We have changed our email domain and now I need to update all of my users. I would like to run a script that would find all of the @olddomain.com and replace it with @newdomain.

 

Can anyone assist me with this?

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Phil,



Here you go. Please run this from background script. Try this on your dev instance first.


var gr = new GlideRecord('sys_user');


gr.query();


while(gr.next())


{


var str = gr.email;


var res = str.replace("@olddomain.com", "@newdomain.com");


gr.email = res;


gr.setWorkflow(false);


gr.update();


}


View solution in original post

8 REPLIES 8

Chuck Tomasi
Tera Patron

Hi Phil,



Pretty easy. Obviously, test this in dev and test before production. Try this in either Scripts Background or a Fix Script. A Fix Script will allow you to capture it in an update set and migrated to test and prod!



Standard disclaimer: The following code is untested, requires review and potential modifications.




(function () {



      var rec = new GlideRecord('sys_user');


      rec.addQuery('email', "ENDSWITH", '@olddomain.com');


      rec.query();



      while (rec.next()) {


          var email = rec.getValue('email');


          var newEmail = email.replace('@olddomain', '@newDomain');


          rec.email = newEmail;


          rec.setWorkflow(false);


          rec.update();


      }


})();


Thanks


Thanks Pradeep worked like a charm.


Thanks Phil for the update.


Let me know if that answered your question. If so, please mark it 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