Delete Asset but not CI

JeetBaliarsingh
Giga Contributor

Hi Expert

I have requirement to delete the Asset and not CI whenever Asset state is "Retired" and substate is "Delete". I have written a script for the same. However, it is deleting Asset as well as also deleting the CI. Please suggest what modification I will do so in the below script to delete only Asset not CI.

 

Script:

var deleteAsset = new GlideRecord('alm_hardware');
deleteAsset.addEncodedQuery('install_statusIN7^substatus=Delete');
deleteAsset.query();
 
while (deleteAsset.next()) {
    deleteAsset.deleteRecord();
}
1 ACCEPTED SOLUTION

Hi @JeetBaliarsingh,

 

Please see the above updated script for you to test.

 

To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

View solution in original post

4 REPLIES 4

Robbie
Kilo Patron
Kilo Patron

Hi @JeetBaliarsingh,

 

(Updated)

Can you add the following syntax leveraging deleteAssetOnly() method available with the AssetandCI class.

Please test this in a non Prod / Dev environment first before running on live data.

 

var deleteAsset = new GlideRecord('alm_hardware');
deleteAsset.addEncodedQuery('install_statusIN7^substatus=Delete');
deleteAsset.query();
 
while (deleteAsset.next()) {
        var assetAndCI = new AssetandCI();
        assetAndCI.deleteAssetOnly(deleteAsset);
}

 

ServiceNow support article for deleteAssetOnly() - https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0823993

 

You may also want to consider using the .deleteMultiple() method for performance efficiencies as advised here:

https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/app-store/dev_portal/API_referenc...

 

To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

 

Hi Robbie

Thanks for your response. However, what you are suggesting is not my ask. See asset and CI are linked with each other. When you delete the Asset, associated CI will also get deleted. Here using script I want to delete the Asset but associated CI shouldn't be deleted.

Hi @JeetBaliarsingh,

 

Please see the above updated script for you to test.

 

To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

Hi Robbie

Thank you for your proactive response. It is working fine now.