- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2018 05:10 AM
Hi All,
I want a schedule job which will check CI on daily basis to deleted relationships if Ci is retired or decommissioned.
Please assist
Regards,
Jyoti
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2018 09:56 PM
Hello,
Technically I agree with other saying that it should be in business rule.
But if you want it to be on scheduled job that is one way good, in case user changes the state of a CI accidentally to retire or decommissioned the relationships would vanish even if he switched the state back to original one if the script is on business rule but if it is on scheduled job you give them time to change their mind.
var ci = new GlideRecord("cmdb_ci");
ci.addEncodedQuery("install_statusIN7,8"); // I have used retired and stolen state as decommissioned is not available in my instance change it accordingly
ci.query();
while(ci.next())
{
var rel = new GlideRecord("cmdb_rel_ci");
rel.addEncodedQuery("parent="+ci.sys_id+"^ORchild="+ci.sys_id);
rel.deleteMultiple() // be very careful when you use this function as it has a risk of deleting all record when used in wrong place, better try it in sandbox or lower instance first
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2023 08:50 AM
Akash,
If you can share your code it would be greatly appreciated. Attempting to implement in our environment but am looking for a means to recall the relationships if needed.
Thanks,
Eric

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2018 05:40 AM
Before you decide to get rid of relationship, remember that you are getting rid of historical background. Suppose an audit comes and checks a change and see a random list of CI's being affected, you would not be able to provide any historical evidence to support the question 'WHY'.
So my advise before deleting anything would be to consider the consequences.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2018 09:40 AM
My only thought is while a Business Rule would work for this it does not sound like a good plane because it means that the moment you retire/Decom something all your relationships go away. We did something similar with Users and the group membership when they were deactivated and then immediately found that people where not being as careful as maybe they should have and disabling the wrong user. In your case I can see someone accidently editing the wrong CI or asset and next thing all the relationships are gone.
I would consider doing this as a job and putting a delay on it, like 30 days after retirement/decom remove this information.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2018 09:56 PM
Hello,
Technically I agree with other saying that it should be in business rule.
But if you want it to be on scheduled job that is one way good, in case user changes the state of a CI accidentally to retire or decommissioned the relationships would vanish even if he switched the state back to original one if the script is on business rule but if it is on scheduled job you give them time to change their mind.
var ci = new GlideRecord("cmdb_ci");
ci.addEncodedQuery("install_statusIN7,8"); // I have used retired and stolen state as decommissioned is not available in my instance change it accordingly
ci.query();
while(ci.next())
{
var rel = new GlideRecord("cmdb_rel_ci");
rel.addEncodedQuery("parent="+ci.sys_id+"^ORchild="+ci.sys_id);
rel.deleteMultiple() // be very careful when you use this function as it has a risk of deleting all record when used in wrong place, better try it in sandbox or lower instance first
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2022 12:46 AM
Hello,
I've tried using the code you provided but unfortunately it deleted all the ci relationships.
Is there a way for it to only delete ci relationships of cis whose operational status have been set to retire?
Thank you for the help.