The CreatorCon Call for Content is officially open! Get started here.

Script to retire CI's and remove relationship

Lucy10
Tera Contributor

I want to create a script that will retire a CI if it has not been discovered/updated after 7 days, and change the sate to Retired,and remove the relationships attached to the Retired CI.

 

Should I use a business rule or a scheduled job?

has anyone got a script I can use as a guide.

 

Thanks,

 

L

3 REPLIES 3

Harshinya1
Mega Guru

Lucyday,

 

You can try something like this. I would choose a scheduled job over business rul

var gr = new GlideRecord(cmdb table);
gr.addEncodedQuery(Filter condition if applicable);
gr.query();
while (gr.next()) {
if ((gr.sys_updated_on < gs.daysAgoStart(7))) {
gr.install_status = 7;
gr.u_active=false;
Deleterelationship(gr.sys_id);
}

gr.update();
}
function Deleterelationship(ciSYSID){
var relgr = new GlideRecord("cmdb_rel_ci");
relgr.addEncodedQuery('parent='+ciSYSID+'^ORchild='+ciSYSID);
relgr.query();
while(relgr.next()) {
relgr.deleteMultiple();

}
}

Lucy10
Tera Contributor

Thank you fo your assistance.

I need to add one more step which im stuck on.

If either child or parent in the relationship has been updated in last 7 days then leave the status for both CIs and all rels in place.

 

So will that query will go first?

Thanks,

 

L

 

Hi Lucy,

Where you able to figure out how to update the script to evaluate both the child and parent records? If yes, would you be willing to share your solution?

Thanks in advance!