Updating CI Relationship
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2018 04:39 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2018 09:02 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2018 09:56 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2018 10:34 PM
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.