Updating records in the sys_user table

jna2756
Tera Expert

Hi,

I am trying to update about a 900 records (3 fields are being updated) in the user table and based on a condition.

I am thinking I will run a fix script (below) but since this I am fairly new to updating records via GlideRecord queries I wanted to see if the below looked right?

var gr = new GlideRecord('sys_user');

gr.addEncodedQuery('u_employee_type=Functional');

gr.setLimit(10);

gr.query();

while (gr.next()){

  gr.active = true;                 //true-false field

  gr.locked_out = false;       //true-false field

  gr.u_exit_date = '';           //date field

  gr.update();

}

Thanks in advance,

Joshua Anderson

1 ACCEPTED SOLUTION

Manik
ServiceNow Employee
ServiceNow Employee

Hi Joshua,



gr.setLimit basically limits the number of records that would be returned by glide record so in this case on 10 records would be returned and updated so that line is not required. Please use the code below:



  1. var gr = new GlideRecord('sys_user');  
  2. gr.addEncodedQuery('u_employee_type=Functional');  
  3. gr.query();  
  4. while (gr.next()){  
  5.   gr.active = true;                 //true-false field  
  6.   gr.locked_out = false;       //true-false field  
  7.   gr.u_exit_date = '';           //date field  
  8.   gr.update();  
  9. }  


The link below would be helpful in case plan to make any changes in your query:




GlideRecord - ServiceNow Wiki



Thanks,


Manik



PS - Please mark correct, helpful or like if applicable.


View solution in original post

7 REPLIES 7

FYI Joshua, setLimit() is a good idea for testing here as it keeps your script from running away if the query is incorrect in some way. For the production release, you won't need/want it.


Thanks for all the help everyone, it worked.



-Joshua Anderson


I'm trying to do basically the same thing.   We created a new List type field in the incident table so that we can add multiple Business Units to an Incident.   So now I need to just update all the records and set the new List field on the old Incidents with whatever was in the Business Unit field.   I'm testing in my Dev environment and I only have 359 records, but when I loop through each record it only updates one record each time I run it.   If I don't perform the update and just loop through, it will go through every record.



var gr = new GlideRecord('incident');


gr.query();


while (gr.next()) {


      var aff_bu = gr.u_business_unit.getDisplayValue();


      gr.u_affected_business_units = aff_bu;


      gr.update();


}