Remove CI relationships

Ni_a Sy
Tera Contributor

Requirement:
A script that will remove the CI relationships.

I worked this for a week, trial and error. I even research here at the community. I used Business rule. I read Mark Stanger answer but still negative. Maybe because I am not so sure of everything.
1. How can I test the data? How can I check if CI relationships are remove successfully?

 



Thank You.
Script:

(function executeRule(current, previous /*null when async*/) {

// Add your code here
removeCIRels();
if (install_status=='7'){

function removeCIRels(){

//Query the CI relationship table

var ciRel = new GlideRecord('cmdb_rel_ci');

ciRel.addQuery('child', current.sys_id) && ('parent', current.sys_id);

ciRel.query();

if(ciRel.getRowCount() > 0){

gs.addInfoMessage('CI relationships for ' + current.name + ' removed as it is no longer operational.' );
}

while(ciRel.next()){

ciRel.deleteRecord();

}

}
}

})(current, previous);



2 ACCEPTED SOLUTIONS

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

Please write a  after update business rule on cmdb_ci:-

 

Saurav11_0-1665296057955.png

 

Then copy the below code in the script:-

 

(function executeRule(current, previous /*null when async*/) {

	
if (current.install_status=='7'){
var gr = new GlideRecord("cmdb_rel_ci");
gr.addQuery("parent="+current.sys_id+"^ORchild="+current.sys_id);
gr.query();
while (gr.next())
{
gr.deleteRecord();
}
}
	


})(current, previous);

 

This will delete all the parent and child relationship when install status is retired.

 

To tets it just find a record in cmdb_rel_ci table which is present in both for Parent and child like below:-

 

Saurav11_1-1665296233734.png

 

In the above example you can see Email is parent for some and child for some. Then open the Email record make the install status retired and save it then you will see all the relationship deleted.(Email was just an example you can take any record of your choice)

 

Please mark my answer as correct based on Impact.

 

View solution in original post

You have to use the below:-

 

configuration item [cmdb_ci]

 

Saurav11_0-1665300217450.png

 

Please mark my answer as correct based on Impact.

 

View solution in original post

5 REPLIES 5

AnubhavRitolia
Mega Sage

Hi @Ni_a Sy 

 

Please update your code as below and try:

 

(function executeRule(current, previous /*null when async*/) {

if (install_status=='7'){
removeCIRels();
}
}
function removeCIRels(){

//Query the CI relationship table

var ciRel = new GlideRecord('cmdb_rel_ci');

ciRel.addQuery('child', current.sys_id) ; 
//ciRel.addQuery('parent', current.sys_id);
ciRel.query();

if(ciRel.hasNext()){
gs.addInfoMessage('CI relationships for ' + current.name + ' removed as it is no longer operational.' );
}
while(ciRel.next()){
ciRel.deleteRecord();
}

}

})(current, previous);

 

I did not understand the requirement fully but this code should help.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Still does nothing. When to run : Before and I check update.

Can you enlighten me how to test that it is working? Maybe I am doing it wrong in testing the data.

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

Please write a  after update business rule on cmdb_ci:-

 

Saurav11_0-1665296057955.png

 

Then copy the below code in the script:-

 

(function executeRule(current, previous /*null when async*/) {

	
if (current.install_status=='7'){
var gr = new GlideRecord("cmdb_rel_ci");
gr.addQuery("parent="+current.sys_id+"^ORchild="+current.sys_id);
gr.query();
while (gr.next())
{
gr.deleteRecord();
}
}
	


})(current, previous);

 

This will delete all the parent and child relationship when install status is retired.

 

To tets it just find a record in cmdb_rel_ci table which is present in both for Parent and child like below:-

 

Saurav11_1-1665296233734.png

 

In the above example you can see Email is parent for some and child for some. Then open the Email record make the install status retired and save it then you will see all the relationship deleted.(Email was just an example you can take any record of your choice)

 

Please mark my answer as correct based on Impact.

 

Ni_a Sy
Tera Contributor

Can you double check if I pointed the right table? because I cant find the table name: 

"cmdb_ci:-" and a lot of choices if cmdb_ci

Thank you