- 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-17-2025 07:55 AM
Hello @Dharsaan-K
If you aren't concerned with autoSysFields, then you can set them false as shown in the code below to enhance the script performance:
var gr = new GlideRecord("cmdb_ci_hardware");
gr.addQuery("u_exclude_from_sam", "false");
gr.query();
var count = 0;
var batchSize = 100; // Batch size
var batch = [];
while (gr.next()) {
batch.push(gr.sys_id.toString());
count++;
if (batch.length >= batchSize) {
updateBatch(batch);
batch = [];
}
}
// 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();
if (grBatch.next()) {
grBatch.setWorkflow(false); // Disable server side logic
grBatch.autoSysFields(false); // Disable auto field updates
grBatch.u_exclude_from_sam = true;
grBatch.updateMultiple(); // Bulk update
}
}
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2025 03:29 AM
HI Vishal, The script is updating the record in hardware table. When I group the record based on Exclude from sam field it is grouping as empty and true but when I expand the empty records it is having value as false.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2025 03:30 AM
I also have another doubt don't we want to set it to true post execution of the script.
grBatch.setWorkflow(false); // Disable server side logic grBatch.autoSysFields(false); // Disable auto field updates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2025 07:40 AM
Hello @Dharsaan-K
1. If you look at the sys_execution_history table, you can identify how many records were updated in the cmdb_ci_hardware table. If the count of cmdb_ci_hardware list view mismatches with your background script then you have the answer however if the match is same then it means you can updated the .addQuery to .addEncodedQuery which you can create from list vew.
2. Yes, it is recommended to use .setWorkflow(true) after update.
Hope that helps!
Hope that helps!