Deleted CI relationship if CI is retired or decommissioned

jyoti29208
Giga Expert

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

1 ACCEPTED SOLUTION

Sateesh Kumar 2
Kilo Guru

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

}

View solution in original post

10 REPLIES 10

Chuck Tomasi
Tera Patron

It would seem that a business rule might be more appropriate here. Why not clean up the relationships immediately when a CI is decommissioned or retired rather than waiting up to 24 hours?

They key to making this work is understanding that CI relationships are kept in the cmdb_rel_ci table. There is a parent field and a child field. If you query the table to look for records where parent or child is the CI's sys_id, you can then remove it.

Be aware that this may leave your CI relationships in a bit of a mess however. If you have A-->B-->C-->D and you retire B, then A doesn't have a child CI and C doesn't have a parent. This could mean your dependencies become "unattached". If that's what you want, then go ahead, but I have to mention it to prevent you from getting data consistency issues.

Akash4
Kilo Sage
Kilo Sage

Hi Jyoti,

You can do this with a Business Rule to run when a CI state changes to retired/decommission. You may Glide record the relationship table [cmdb_rel_ci] and delete that particular record. 

For history of record you can run a background script which deletes past CIs with these states. I can help you with the code if needed.

Regards, Akash

Regards, Akash
If my response proves useful, please mark it "Accept as Solution" and "Helpful". This action benefits both the community and me.

Hello, can you provide the code? I also need it since I am working on a requirement right now. Thank you in advance.

Ni_a Sy
Tera Contributor

Can i also have the script?