Retire CI's

Lucy10
Tera Contributor

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

1 ACCEPTED SOLUTION

Community Alums
Not applicable

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;

}
}

View solution in original post

5 REPLIES 5

Community Alums
Not applicable

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;

}
}

Lucy10
Tera Contributor

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

Lucy10
Tera Contributor

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()');

 

 

Lucy10
Tera Contributor

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