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

You have to use the below:-

 

configuration item [cmdb_ci]

 

Saurav11_0-1665300217450.png

 

Please mark my answer as correct based on Impact.