Limit a list field to only unique values

aj_becker
Tera Guru

Hello, 

We are working on creating a script that will be trigger when a new record is created on the impacted Services Table (task_cmdb_ci_service).  If the new record has a change request under the task field, we want to update a list field on the change record for approval purposes.  The problem we are running into is that this is creating duplicates.  We dont want duplicates as this will cause duplicate approvals in our change process.  We create a After Business Rule with the following code

(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('change_request');
gr.addQuery('sys_id', current.task);
gr.query();
while (gr.next()) {
var value = current.task.u_approval_group.split(', ');
value.push(current.cmdb_ci_service.u_application_approval_group.toString());
value = new ArrayUtil().unique(value);
gr.u_approval_group = value.toString();
gr.update();
}

gs.log("AB - Change Request Unique" + value);
})(current, previous);

 

What can we improve in this code to no allow duplicate values in the change list field?

Thank You

1 ACCEPTED SOLUTION

Happy to help.

If I understand you correctly, then you want to add the groups to the u_approval_group field even if it is empty. If so, then please try this.

(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('change_request');
gr.addQuery('sys_id', current.task);
gr.query();

while (gr.next()) {

var value = [];

if (current.task.u_approval_group)
  value = current.task.u_approval_group.split(',');

value.push(current.cmdb_ci_service.u_application_approval_group.toString());
var uniqueValue = new ArrayUtil().unique(value);
gr.u_approval_group = uniqueValue.toString();
gr.update();

}

gs.log("AB - Change Request Unique" + value);
})(current, previous);
Regards,
Muhammad

View solution in original post

6 REPLIES 6

Happy to help.

If I understand you correctly, then you want to add the groups to the u_approval_group field even if it is empty. If so, then please try this.

(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('change_request');
gr.addQuery('sys_id', current.task);
gr.query();

while (gr.next()) {

var value = [];

if (current.task.u_approval_group)
  value = current.task.u_approval_group.split(',');

value.push(current.cmdb_ci_service.u_application_approval_group.toString());
var uniqueValue = new ArrayUtil().unique(value);
gr.u_approval_group = uniqueValue.toString();
gr.update();

}

gs.log("AB - Change Request Unique" + value);
})(current, previous);
Regards,
Muhammad

That works!  

 

Thank You!