- 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 10:54 AM
The code looks fine mostly. Can you try this code and post a screenshot of your BR if it doesn't work?
(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 + "");
value = new ArrayUtil().unique(value);
gr.u_approval_group = value + "";
gr.update();
}
gs.log("AB - Change Request Unique" + value);
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2020 10:54 AM
Try with below script
(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 = []
value = current.task.u_approval_group.split(', ');
if (value.length == 0)
value.push(current.cmdb_ci_service.u_application_approval_group.toString());
else if (value.indexOf(current.cmdb_ci_service.u_application_approval_group.toString())==-1)
value.push(current.cmdb_ci_service.u_application_approval_group.toString());
gr.u_approval_group = value.toString();
gr.update();
}
gs.log("AB - Change Request Unique" + value);
})(current, previous);
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2020 10:54 AM
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 = 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:08 AM
Thank You Muhammad,
This code looks to have fixed the issue. Thank You.
Can I bounce another question off of you? The "u_approval_group" field we are updating on the change request. It only adds a new group if there is some existing group in there, if that approval group field is empty, it wont add a new group to it? Any Ideas?