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.

How to do one time clean up of the asset tags mapped to retired CIs ?

1_DipikaD
Kilo Sage

Hi All,

 

I want one time clean up of the asset tags mapped to retired CIs . My approach is to use fix script . Could you please suggest what should be the correct approach and Script ? I tried through fix script but it made the system slow and did not work as expected .

 

Please let me know if you need more info.

 

Thank You

 

7 REPLIES 7

GlideFather
Tera Patron

Hi @1_DipikaD,

 

can you please provide more details about the nature of your cleanup?

 

Is it deactivation, deletion or what exactly?

 

You can also use Update job (no-code method) but it's valid to update all the records the same way - for example changing their state or any other value.

 

Fix script is also good option, but please provide details, this is too abstract and perhaps it could be done alternatively..

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


Me Being Mustaq
Tera Guru

Hi @1_DipikaD ,

 

For a one-time cleanup of asset tags mapped to retired CIs, using a fix script is a good approach but requires careful implementation to avoid performance issues. Here’s a recommended approach with best practices and a sample fix script outline.

Recommended Approach for Cleanup Fix Script

  1. Batch Processing:

    • Avoid processing all records at once; use batching to reduce locking and improve performance.

    • Process records in chunks (e.g., 100 or 200 at a time).

  2. Efficient Querying:

    • Query only CIs that are in Retired state and have a non-empty asset tag.

    • Use indexed fields (like state and asset_tag) for filtering.

  3. Update Minimal Fields:

    • Only update the asset tag field or another relevant mapping field.

    • Use setWorkflow(false) to prevent unnecessary business rule triggers.

  4. Scheduled Script Execution (Optional):

    • For very large data, consider scheduled script execution or background jobs instead of fix script.

Example Fix Script

(function() {
var batchSize = 100;
var gr = new GlideRecord('cmdb_ci');
gr.addQuery('state', 'Retired'); // adjust based on your state field values
gr.addNotNullQuery('asset_tag'); // ensure asset tag exists
gr.setLimit(batchSize);
gr.query();

var updatedCount = 0;
while (gr.next()) {
gr.setWorkflow(false); // avoid triggering workflow/business rules
gr.asset_tag = ''; // clear asset tag
gr.update();
updatedCount++;
}

gs.print('Retired CIs asset tag cleanup completed. Total records updated: ' + updatedCount);
})();

 

Additional Tips

  • Test the script in a development or sub-prod instance first.

  • Monitor for performance and splitting execution into multiple runs if too many records exist.

  • Consider logging progress in a table for large cleanups.

  • If asset tags are on a related table (like an asset table), adjust the GlideRecord query accordingly to unlink or clear those tags.

StepAction

Query Retired CIs

Use indexed state field and asset tag presence

Batch Processing

Limit fixes to manageable chunks

Disable Workflows

setWorkflow(false)to reduce overhead

Clear Asset Tags

Set asset_tag field to empty or null

Test & Monitor

Test in test environment and monitor performance

 

This approach will efficiently clean asset tags mapped to retired CIs without overloading the system, unlike an unbatched full fix script run.

 

If my response was helpful, please consider clicking the “Accept as Solution” button below my answer. Your feedback encourages me to continue assisting others.

 

Regards,
Mohammed Mustaq Shaik

Hi @1_DipikaD ,

 

Please appreciate my efforts, help and support extended to you by clicking on – “Accept as Solution”; button under my answer. It will motivate me to help others as well.

 

Thanks & Regards,

Mohammed Mustaq Shaik

@Me Being Mustaq 

I was using sysids to delete approx 60 records as slight change in requirement . Is it best practice to do this ? I am using the below script please suggest me if both encoded query and array can be kept or should I go for any other method ?

 

Thank You


var tableName = 'my_table_name';var sysIdArray = [
'sys_id_1',
'sys_id_2',
'sys_id_3',
'sys_id_4',
];

var gr = new GlideRecord(tableName);gr.addQuery('sys_id', 'IN', sysIdArray);

gr.query();

var deleteCount = 0; 

gs.print('--- SCRIPT START: Targeted Deletion on Table: ' + tableName + ' ---');
gs.print('Total Sys IDs provided: ' + sysIdArray.length);

while (gr.next()) {
gs.print(' Deleting record: ' + (gr.number || gr.sys_id) + ' (' + gr.sys_id + ')');

gr.deleteRecord();
deleteCount++;
}

gs.print('Total records FOUND and DELETED: ' + deleteCount);