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

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.