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

Lucy10
Tera Contributor

Stuck Help!

Would I need to create another function?

 

 script:

var gr = new GlideRecord(cmdb table);
gr.addEncodedQuery(Filter condition if applicable);;
gr.query();
while (gr.next()) {
var check = Deleterelationship(gr.sys_id);
var check1 = Checkrelationship(gr.sys_id);

if(check == true)


{
gr.u_lifecycle_state = 'Retired';
gr.u_active=false;

 

gr.update();
}
if(check1 == true)


{
gr.u_lifecycle_state = 'Retired';
gr.u_active=false;

gr.update();
}
}

function Deleterelationship(ciSYSID){
var relgrDate ='child.sys_updated_onRELATIVELE@dayofweek@ago@7^ORparent.sys_updated_onRELATIVELE@dayofweek@ago@7';
var relgr = new GlideRecord("cmdb_rel_ci");
relgr.addEncodedQuery('parent='+ciSYSID+'^ORchild='+ciSYSID);
relgr.addEncodedQuery(relgrDate);
relgr.query();
if(relgr.getRowCount() > 0)
{

return false;

}else


{


while(relgr.next()) {
relgr.deleteMultiple();
}
return true;
}
}

function Checkrelationship(ciSYSID){
var relgr1= new GlideRecord("cmdb_rel_ci");
relgr1.addEncodedQuery('parent='+ciSYSID+'^ORchild='+ciSYSID);
relgr1.query();
if(relgr1.getRowCount() == 0 )
{

return true;

}else


{

return false;
}
}