- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2025 10:04 AM
HI Team,
I am trying to update all the record in cmdb_Ci_hardware table with value as false for Exclude from SAM filed.
Following is the Background script I tried.
This table is on global scope and I am runiing script in global scope
var gr = new GlideRecord("cmdb_ci_hardware");
gr.addQuery("u_exclude_from_sam", "false");
gr.query();
var count = 0;
var batchSize = 20; // Further reduced batch size
var batch = [];
while (gr.next()) {
gr.u_exclude_from_sam = "true";
batch.push(gr.sys_id.toString());
count++;
if (batch.length >= batchSize) {
updateBatch(batch);
batch = [];
gs.sleep(1000); // Add a delay of 1 second between batches
}
}
// Update any remaining records
if (batch.length > 0) {
updateBatch(batch);
}
gs.print("Total updated: " + count);
gs.print("All data has been updated successfully.");
function updateBatch(batch) {
var grBatch = new GlideRecord("cmdb_ci_hardware");
grBatch.addQuery("sys_id", "IN", batch);
grBatch.query();
grBatch.setWorkflow(false); // Suppress business rules
while (grBatch.next()) {
try {
grBatch.u_exclude_from_sam = "true";
grBatch.updateMultiple();
} catch (e) {
gs.error("Error updating record with sys_id: " + grBatch.sys_id + ". Error: " + e.message);
}
}
grBatch.setWorkflow(true); // Reactivate business rules
}
Initially I go the following error message : slow business rule
Slow business rule 'Canonicalization Tracking' on cmdb_ci_computer:<span class = "session-log-bold-text"> HAE5CG138BJFH</span>, time was: 0:00:00.453
Slow business rule 'Virtual Computer Check' on cmdb_ci_computer:<span class = "session-log-bold-text"> HAE5CG3107X5Q</span>, time was: 0:00:00.372
Slow business rule 'Update legacy from CSDM' on cmdb_ci_computer:<span class = "session-log-bold-text"> HAE5CG2492977</span>, time was: 0:00:00.351
Slow business rule 'Reset Duplicate Discovery Source' on cmdb_ci_computer:<span class = "session-log-bold-text"> HAE5CG3070Z8V</span>, time was: 0:00:00.406
Slow business rule 'Update model category' on cmdb_ci_computer:<span class = "session-log-bold-text"> HAE5CG24506M8</span>, time was: 0:00:00.355
Time: 0:00:00.379 id: haleondev_1[glide.1] (connpid=168335) for: INSERT INTO sys_rollback_sequence (`context`, `target_class_name`, `storage_table_name`, `document_id`, `operation`,`txn_id`,`is_metadata`, `row_mod_count`, `audit_delete`, `audit_delete_path`, `recovered`) SELECT '6cee381d1b106ad4eede76e5d34bcb54', 'cmdb_ci_computer', 'cmdb', `sys_id`, 'update', 'c0f0055164146ad40039824748bfdf8f', 0, `sys_mod_count`, NULL, NULL, 0 FROM cmdb WHERE cmdb.`sys_id` = 'c95eaaba1b00e210eede76e5d34bcb8b' /* haleondev001, gs:667820591B54E6D4EEDE76E5D34BCB86, tx:ecee38d51bdc2ad4eede76e5d34bcb9b */
Slow business rule 'Clear IBM PVU columns' on cmdb_ci_computer:<span class = "session-log-bold-text"> HAE5CG307344H</span>, time was: 0:00:00.530
Slow business rule 'Only view GSK Assets' on alm_asset:<span class = "session-log-bold-text"> </span>, time was: 0:00:00.365
Slow business rule 'HLN-Intune-Assigned to for spare asset' on cmdb_ci_computer:<span class = "session-log-bold-text"> HAE5CG3172VZ3</span>, time was: 0:00:00.374
Slow business rule 'Asset query rules for Contacts' on alm_asset:<span class = "session-log-bold-text"> </span>, time was: 0:00:00.330
Slow business rule 'HLN-Intune-Assigned to for spare asset' on cmdb_ci_computer:<span class = "session-log-bold-text"> HAE5CG2158DRP</span>, time was: 0:00:00.341
AccessTerm: Slow ACL e3eaf9a51b302000aebbfbcd2c0713aa for the path record/cmdb_model_category/read , time was: 6
Cannot disable before query business rule(s) across scope boundaries (current scope: rhino.global table scope: sn_cmdb_ci_class)
Cannot disable before query business rule(s) across scope boundaries (current scope: rhino.global table scope: sn_cmdb_ci_class)
*** Script: Total updated: 16113
*** Script: All data has been updated successful"
Although my code shown as all record is updated it is not the case
Then I though like may be due to huge data it is taking more time and I processes the data batch wise after getting the same error message even reduced batch size to only 20 records
Even then I got the same message
I referred community post in once of them they have mention to temporality disable BR using grBatch.setWorkflow(false) after trying the approach got the following message
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2025 12:05 PM
Hello @Dharsaan-K
Minor changes and you will notice the performance efficiency:
1. Avoid using updateMultiple() in while loop as it should be called once and that too outside of the loop to update all records in one go. Reference: https://snprotips.com/blog/2016/12/20/pro-tip-use-updatemultiple-for-maximum-efficiency
2. setForceUpdate(true) -- bypass updates that get ignored. Reference: https://servicenowguru.com/scripting/gliderecord-query-cheat-sheet/
var gr = new GlideRecord("cmdb_ci_hardware");
gr.addQuery("u_exclude_from_sam", "false");
gr.query();
var count = 0;
var batchSize = 100; // I have increased it to 100
var batch = [];
while (gr.next()) {
batch.push(gr.sys_id.toString());
count++;
if (batch.length >= batchSize) {
updateBatch(batch);
batch = [];
gs.sleep(500); // Reduced to 0.5 seconds
}
}
// Update any remaining records
if (batch.length > 0) {
updateBatch(batch);
}
gs.print("Total updated: " + count);
gs.print("All data has been updated successfully.");
function updateBatch(batch) {
var grBatch = new GlideRecord("cmdb_ci_hardware");
grBatch.addQuery("sys_id", "IN", batch);
grBatch.setForceUpdate(true); // Ensure updates are committed
grBatch.query();
while (grBatch.next()) {
grBatch.setWorkflow(false); // Disable business rules and workflows (server-side logics)
grBatch.u_exclude_from_sam = true; // Use boolean instead of string
grBatch.update();
}
}
Results:
Hope it helps!
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2025 11:06 AM
I'm having trouble following with all of the messages, and the last screenshot is too tiny to read. You should try using
grBatch.update();
in place of updateMultiple. 16K records to update is relatively not an excessive number, but with cross-scope Business Rules running it may still take some time. I'm not sure the worth of the all of the batch code, in any event since it's a one-time or overnight process to update the records, processing time and load shouldn't be a major concern as long as the end result is the records are updated. You can also try System Data Management > Update Jobs to accomplish the same in a non-scripted approach.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2025 07:17 AM
HI Brad, I have also tried with grBatch.update();. I go the same error message. Even though the log of the script shows it have updated all the record when I check the table it haven't updated all the record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2025 12:05 PM
Hello @Dharsaan-K
Minor changes and you will notice the performance efficiency:
1. Avoid using updateMultiple() in while loop as it should be called once and that too outside of the loop to update all records in one go. Reference: https://snprotips.com/blog/2016/12/20/pro-tip-use-updatemultiple-for-maximum-efficiency
2. setForceUpdate(true) -- bypass updates that get ignored. Reference: https://servicenowguru.com/scripting/gliderecord-query-cheat-sheet/
var gr = new GlideRecord("cmdb_ci_hardware");
gr.addQuery("u_exclude_from_sam", "false");
gr.query();
var count = 0;
var batchSize = 100; // I have increased it to 100
var batch = [];
while (gr.next()) {
batch.push(gr.sys_id.toString());
count++;
if (batch.length >= batchSize) {
updateBatch(batch);
batch = [];
gs.sleep(500); // Reduced to 0.5 seconds
}
}
// Update any remaining records
if (batch.length > 0) {
updateBatch(batch);
}
gs.print("Total updated: " + count);
gs.print("All data has been updated successfully.");
function updateBatch(batch) {
var grBatch = new GlideRecord("cmdb_ci_hardware");
grBatch.addQuery("sys_id", "IN", batch);
grBatch.setForceUpdate(true); // Ensure updates are committed
grBatch.query();
while (grBatch.next()) {
grBatch.setWorkflow(false); // Disable business rules and workflows (server-side logics)
grBatch.u_exclude_from_sam = true; // Use boolean instead of string
grBatch.update();
}
}
Results:
Hope it helps!
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2025 07:04 AM
HI Vishal, Yes, the code which you shared have update all the record, but it is taking more time even for few record. Is it possible to reduce the time it takes to execute.