Auto create CI relationship

John var
Giga Contributor

HI All,

I have the requirement as follows.

1. A custom filed called "Data Center" is created on cmdb_ci_server table and the field is reference to cmdb_ci_server.

From the list view user selects the data center field. So that it should create a relation in cmdb_rel_ci table with parent as Data center and child as current record name. Also the relation type should be like Connected by::Connects.

To achieve this i have created a business rule as below.

Before insert/update - Data center changes,

Advanced:

(function executeRule(current, previous /*null when async*/) {
	
var rel = new GlideRecord('cmdb_rel_ci');
	rel.initialize();
	rel.parent = current.u_data_center;
	rel.child = current.name;
	rel.update();
	
})(current, previous);

Can someone help me to achieve this..

5 REPLIES 5

emir
ServiceNow Employee
ServiceNow Employee

you are missing the rel_type, parent and child are sys ids of the 2 records

also, that is not how the relationship between server and DC is usually represented, you may be looking for the location on the CI 

John var
Giga Contributor

@emir  Child relationship is not working. Please see the ss below

find_real_file.png

 

MY BR is BELOW

(function executeRule(current, previous /*null when async*/) {
// var dc = current.u_data_center.getDisplayValue();
// 	var n = current.name.getDisplayValue();
	
var rel = new GlideRecord('cmdb_rel_ci');
	rel.initialize();
	rel.parent = current.u_data_center;
	rel.child = current.name;
	rel.type = '55c95bf6c0a8010e0118ec7056ebc54d';
	rel.insert();
	
})(current, previous);

emir
ServiceNow Employee
ServiceNow Employee

if current is the server, then rel.child = current.name; should simply be

rel.child = current.sys_id;

Rels operate on sys_id not names, Data Center works because the value in the field is a sys_id referencing the DC

D_R
Giga Guru

@John var Please add like below mentioned in 6th line - Relation type.

(function executeRule(current, previous /*null when async*/) {
var rel = new GlideRecord('cmdb_rel_ci');
rel.initialize();
rel.parent = current.u_data_center;
rel.child = current.name;

rel.type = '  '// sys_id of Connected by::Connects record --Relation type


rel.update();

})(current, previous);

 

Thanks,

Mark it helpful if it works!!