Updating CI Relationship

Divya95
Tera Contributor

Hi  ,

I have the below script to insert the ci relationship between two tables (customer,resource)using reference field (u_customer) .

 

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

 var rel = new GlideRecord ('cmdb_rel_ci');
  rel.child = current.sys_id;
rel.parent=current.u_parentcfs;
rel.type = "9d7bae1edb1213001d49d360cf96198d"; //rel.type = 'sys_id of the Fulfilled by::Fulfilles record on the cmdb_rel_type table';
rel.update();

})(current, previous);

 

If I update the value in reference field , its creating  a new record instead of updating the current one .

Please suggest

 

3 REPLIES 3

Archana Reddy2
Tera Guru

Hi Divya,

Since you are trying to update the existing Relation, you have to query for that relation.

The below may help you.

var rel = new GlideRecord ('cmdb_rel_ci');
rel.addQuery('sys_id','**'); //Replace ** with sysID of the existing CI Relation
rel.query();
if(rel.query())
{
rel.child = current.sys_id;
rel.parent=current.u_parentcfs;
rel.type = "9d7bae1edb1213001d49d360cf96198d";
rel.update();
}

Hope this helps!

Thanks,

Archana

nayanawadhiya1
Kilo Sage

Hello Divya,

 

What do you want?

New Relationship or Updating Relationship.

As per above code, i think you to create new relationship between them.

Then use below code - 

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

var rel = new GlideRecord ('cmdb_rel_ci');
rel.initialize();
rel.child = current.sys_id;
rel.parent=current.u_parentcfs;
rel.type = "9d7bae1edb1213001d49d360cf96198d"; //rel.type = 'sys_id of the Fulfilled by::Fulfilles record on the cmdb_rel_type table';
rel.insert();

})(current, previous);

 

Rajesh M1
Giga Guru

Hi Divya,

Your business rule should be of type onAfter and use below code:

 

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

 var rel = new GlideRecord ('cmdb_rel_ci');
 

rel.addQuery('child',current.sys_id);

rel.addQuery('type','9d7bae1edb1213001d49d360cf96198d'); //rel.type = 'sys_id of the Fulfilled by::Fulfilles record on the cmdb_rel_type table';

rel.query();

if(rel.next()) //Use while statement if you want to update multiple records (while(rel.next()))

{

rel.parent=current.u_parentcfs;

rel.update();

}

})(current, previous);

Best Regards,

Rajesh M.