- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2018 11:24 PM
Hi All ,
I have a customer table with 3 fields ,Customer Id , Producer Id,Department ID
I need to write a business rule as when values are provided in Producer Id or Department Id or both of them , they should create relationship with Customer Id (Child)
In the below query , if only department id changes , its not inserting the record .But it inserts if producer id is changed or both of them .
I have tried for below ,
(function executeRule(current, previous /*null when async*/)
{
var rel = new GlideRecord ('cmdb_rel_ci');
if (current.u_producer_id != null)
{
rel.initialize();
rel.child = current.sys_id;
rel.parent=current.u_producer_id;
rel.type = "9d7bae1edb1213001d49d360cf96198d"; //sys_id of the Parent::Child record on the cmdb_rel_type table';
}
if (current.u_department_id != null)
{
rel.initialize();
rel.child = current.sys_id;
rel.parent=current.u_department_id;
rel.type = "9d7bae1edb1213001d49d360cf96198d";
}
rel.insert();
})(current, previous);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2018 12:37 AM
if you put both conditions in OR statement then it should have to work.
Lets try with below code. Remove all the conditions from "when to run" tab, keep after insert and update only.
(function executeRule(current, previous /*null when async*/)
{
var rel = new GlideRecord ('cmdb_rel_ci');
if (current.u_producer_id.changes() && current.u_producer_id != "")
{
rel.initialize();
rel.child = current.sys_id;
rel.parent=current.u_producer_id;
rel.type = "9d7bae1edb1213001d49d360cf96198d";
rel.insert();
}
if (current.u_department_id.changes() && current.u_department_id != "")
{
rel.initialize();
rel.child = current.sys_id;
rel.parent=current.u_department_id;
rel.type = "9d7bae1edb1213001d49d360cf96198d";
rel.insert();
}
})(current, previous);
Hit Helpful or Correct on the impact of response.
Regards,
Tushar
www.DxSherpa.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2018 01:45 AM
Thanks Tushar ,this has worked .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2018 12:21 AM
Hi,
Try with below code,
(function executeRule(current, previous /*null when async*/)
{
var rel = new GlideRecord ('cmdb_rel_ci');
if (current.u_producer_id != null)
{
rel.initialize();
rel.child = current.sys_id;
rel.parent=current.u_producer_id;
rel.type = "9d7bae1edb1213001d49d360cf96198d";
rel.insert();
}
if (current.u_department_id != null)
{
rel.initialize();
rel.child = current.sys_id;
rel.parent=current.u_department_id;
rel.type = "9d7bae1edb1213001d49d360cf96198d";
rel.insert();
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2018 12:41 AM
Thanks Shweta .
If only department id is changed ,relationship is created for customer with department id and also for producer id ,but producer id value is blank in cmdb_rel_ci table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2018 01:44 AM
Remove condition from 'when to run' and try with below code,
(function executeRule(current, previous /*null when async*/)
{
var rel = new GlideRecord ('cmdb_rel_ci');
if (current.u_producer_id != null && current.u_producer_id !='')
{
rel.initialize();
rel.child = current.sys_id;
rel.parent=current.u_producer_id;
rel.type = "9d7bae1edb1213001d49d360cf96198d";
rel.insert();
}
if (current.u_department_id != null && current.u_department_id !='')
{
rel.initialize();
rel.child = current.sys_id;
rel.parent=current.u_department_id;
rel.type = "9d7bae1edb1213001d49d360cf96198d";
rel.insert();
}
})(current, previous);