Script to retire CI's and remove relationship
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2019 07:52 AM
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
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2019 08:11 AM
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();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2019 05:16 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2020 07:22 AM
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!