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

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 

Kalaiarasan Pus
Giga Sage

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.

DrewW
Mega Sage
Mega Sage

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.

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

}

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.