- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2019 03:27 AM
I got a script that was provided by a member in the community to retires CI's and remove relationship if the CI's hasnt been updated in the pas 7 days.
But I need to factor in If either child or parent in the relationship has been updated in last 7 days then leave the status for both CIs and all relation in place. This part im stuck which I need assistance on. Should query the relationship table first?
The current script:
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();
}
}
Thanks,
Lucy
Solved! Go to Solution.
- Labels:
-
Best Practices
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2019 04:02 AM
Hi,
Updating your existing code
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))) {
var check = Deleterelationship(gr.sys_id);
if(check == true)
{
gr.install_status = 7;
gr.u_active=false;
gr.update();
}
}
function Deleterelationship(ciSYSID){
var relgr = new GlideRecord("cmdb_rel_ci");
relgr.addEncodedQuery('parent='+ciSYSID+'^ORchild='+ciSYSID^sys_updated_onONLast 7 days@javascript:gs.beginningOfLast7Days()@javascript:gs.endOfLast7Days()');
relgr.query();
if(relgr.getRowCount() > 0)
return false;
else
{
while(relgr.next()) {
relgr.deleteMultiple();
}
return true;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2019 04:02 AM
Hi,
Updating your existing code
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))) {
var check = Deleterelationship(gr.sys_id);
if(check == true)
{
gr.install_status = 7;
gr.u_active=false;
gr.update();
}
}
function Deleterelationship(ciSYSID){
var relgr = new GlideRecord("cmdb_rel_ci");
relgr.addEncodedQuery('parent='+ciSYSID+'^ORchild='+ciSYSID^sys_updated_onONLast 7 days@javascript:gs.beginningOfLast7Days()@javascript:gs.endOfLast7Days()');
relgr.query();
if(relgr.getRowCount() > 0)
return false;
else
{
while(relgr.next()) {
relgr.deleteMultiple();
}
return true;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2019 05:23 AM
Thank you, I keep getting the following error when testin: “missing ) after argument”
Its linked to the encoded query
“relgr.addEncodedQuery('parent='+ciSYSID+'^ORchild='+ciSYSID^sys_updated_onONLast 7 days@javascript:gs.beginningOfLast7Days()@javascript:gs.endOfLast7Days()');”
I cant spot what wrong.
Thanks,
L
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2019 08:43 AM
Any ideas on whats wrong with the encoded query?
Im missing an apostrophe and Im not sure where is should go?
relgr.addEncodedQuery('parent='+ciSYSID+'^ORchild='+ciSYSID^sys_updated_onONLast 7 days@javascript:gs.beginningOfLast7Days()@javascript:gs.endOfLast7Days()');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2019 05:57 AM
Help!
Its retiring the CIs if the child or parents has been updated in the last 7 days but I also need to retire the CI if it hasn’t got any relationships and hasn’t been updated in the last 7 days.
I just need some assistance to put in additional step in the script if it hasn’t got any relationship and the CI hasn’t been updated for the past 7 days make the CI retire.
Not sure where to put this steps?
Thanks,
L