Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Fix script to update field value

MStritt
Tera Guru

Just looking for sample code to update a field value on all contact records.

 

Table: customer_contact

Field: Verification Status (u_verification_status)

Value: Not Verified

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @MStritt,

 

To update the "Verification Status" field on all contact records in the "customer_contact" table in ServiceNow, you can use the following sample code:

var gr = new GlideRecord('customer_contact');
gr.query();

// Loop through each record and update the field
while (gr.next()) {
  gr.setValue('u_verification_status', 'Not Verified');
  gr.update();
}

View solution in original post

5 REPLIES 5

Community Alums
Not applicable

Hi @MStritt,

 

To update the "Verification Status" field on all contact records in the "customer_contact" table in ServiceNow, you can use the following sample code:

var gr = new GlideRecord('customer_contact');
gr.query();

// Loop through each record and update the field
while (gr.next()) {
  gr.setValue('u_verification_status', 'Not Verified');
  gr.update();
}

Thanks! Can you add a line in the code where it only updates a few records? Maybe 10? For testing?

@MStritt ,

 

Just add this line before gr.query();

gr.setLimit(10);

 

Thanks,

Danish

 

Community Alums
Not applicable

You can use the setLimit() function to set a limit of 10,20, etc records. And the orderBy() function to sort by a particular column.

Here is the revised version of this script:

 

var gr = new GlideRecord('customer_contact');
gr.orderBy('sys_created_on'); // Order by created date to update the first 10 records
gr.setLimit(10); // Limit the query to the first 10 records
gr.query();

// Loop through each record and update the field
while (gr.next()) {
  gr.setValue('u_verification_status', 'Not Verified');
  gr.update();
}