Regarding setForceUpdate method in gliderecord ?

Shubham Rai
Tera Contributor

Hi ,

 

Need to know more about setForceUpdate method in glideRecord ?

 

Please help

1 ACCEPTED SOLUTION

Astik Thombare
Tera Sage

Hi @Shubham Rai ,

 

‘setForceUpdate’ is used to update records without having to change a value on that record to get the update to execute. ‘setForceUpdate’ is particularly useful in situations where you need to force the recalculation of a calculated field for all records in a table or when you need to run business rules against all records in a table but don’t want to have to change a value on the records.This method is often used with ‘setWorkflow’ and ‘autoSysFields’ as shown below.

 

 

//Force an update to all User records without changing field values
var gr = new GlideRecord('sys_user');
gr.query();
while (gr.next()) {
gr.setWorkflow(false); //Do not run business rules
gr.autoSysFields(false); //Do not update system fields
gr.setForceUpdate(true); //Force the update
gr.update();
}

 

Please mark  Correct if this resolves your issue, and also mark 👍 Helpful if you find my response valuable based on its impact.

 

Regards,

Astik Thombare

View solution in original post

2 REPLIES 2

Astik Thombare
Tera Sage

Hi @Shubham Rai ,

 

‘setForceUpdate’ is used to update records without having to change a value on that record to get the update to execute. ‘setForceUpdate’ is particularly useful in situations where you need to force the recalculation of a calculated field for all records in a table or when you need to run business rules against all records in a table but don’t want to have to change a value on the records.This method is often used with ‘setWorkflow’ and ‘autoSysFields’ as shown below.

 

 

//Force an update to all User records without changing field values
var gr = new GlideRecord('sys_user');
gr.query();
while (gr.next()) {
gr.setWorkflow(false); //Do not run business rules
gr.autoSysFields(false); //Do not update system fields
gr.setForceUpdate(true); //Force the update
gr.update();
}

 

Please mark  Correct if this resolves your issue, and also mark 👍 Helpful if you find my response valuable based on its impact.

 

Regards,

Astik Thombare

Community Alums
Not applicable

Hi @Shubham Rai ,

 

The setForceUpdate method in ServiceNow is used to update records without having to change a value on that record to get the update to execute. This method is particularly useful in situations where you need to force the recalculation of a field or perform a mass update of records without changing their values.

 

Here's a sample code snippet demonstrating the use of setForceUpdate:

var gr = new GlideRecord('incident');
gr.addQuery('category', 'software');
gr.query();

while (gr.next()) {
  gr.category = 'hardware';
  gr.setForceUpdate(true);
  gr.update();
}

In this example, the script updates the category field of all incident records with a category of 'software' to 'hardware' without changing any other values on the records. The setForceUpdate method ensures that the update is executed even though no fields have been changed.