- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2020 10:48 AM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2020 11:30 AM
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);
Muhammad

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2020 11:30 AM
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);
Muhammad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2020 11:34 AM
That works!
Thank You!