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

Prasun
Giga Guru

Hello,



Try this



  1. var gr = new GlideRecord('sys_user');  
  2. gr.addQuery('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. }  

Chuck Tomasi
Tera Patron

Hi Joshua,



Have you tried running this in scripts-background on your dev system to test?



You can also try to place a log statement below the gr.query() to see how many records were retrieved (don't forget you limited it to 10)



gs.log(gr.getRowCount() + ' rows retrieved.'):



This video may help you with your debugging.


Faster Server Side Script Development and Test


Mihir Mohanta
Kilo Sage

1.Comment out line 3. Script looks good.


2.Also please verify the choice value of "u_employee_type" field is "Functional" or not.


(Use value not the label of the choice)



Thanks,


Mihir


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.