Need to fix the script in Business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 02:07 AM
Hi All ,
I have written the below business rule for the condition :
1) Check if the AG of the assignment group and the CI assignment group is same in the change request
2) If same check the member who has approved the change request at the approval from assignment group don't allow the same user to approve at CI assignment group approval
3) Update the current state to 'Not Valid' for the same user in the CI assignment group approval level
Script
Business rule is written on change_request table , with in When to run condition , it should run before , Insert and Update
Script :
But the script is not working , can anyone please help where this script is wrong ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 03:07 AM
I understood that,
- Your change module has First level approval as Change Assignment group
- Second level approval is by CI Assignment Group.
And, your requirement is, if your CR assignment group and the CI Assignment Group are same, the same person who approved at First Level (CR Assignment group) should not be able to approve at Second Level (CI Assignment Group).
The below script will help you to achieve your requirement.
(function executeRule(current, previous /*null when async*/ ) {
var assignmentgroup = current.assignment_group.sys_id + '';
var cmdbcigroup = current.cmdb_ci.assignment_group.sys_id + '';
if (assignmentgroup == cmdbcigroup) {
var approval = new GlideRecord('sysapproval_approver');
approval.addQuery('sysapproval', current.sys_id);
approval.addQuery('state', 'approved');
approval.query();
var approvers = [];
while (approval.next()) {
approvers.push(approval.getValue("approver"));
}
if (approvers.length > 0) {
var grpApproval = new GlideRecord("sysapproval_group");
grpApproval.addQuery("parent", current.sys_id);
grpApproval.addQuery("assignment_group", cmdbcigroup);
grpApproval.addQuery("approval", "requested");
grpApproval.query();
if (grpApproval._next()) {
var ciapproval = new GlideRecord('sysapproval_approver');
ciapproval.addQuery('sysapproval', current.sys_id);
ciapproval.addQuery('group', grpApproval.getUniqueValue());
ciapproval.addQuery('state', 'requested');
ciapproval.query();
var arrayUtil = new ArrayUtil();
while (ciapproval.next()) {
if (arrayUtil.contains(approvers, ciapproval.getValue('approver'))) {
ciapproval.setValue("state", "not_required");
ciapproval.update();
}
}
}
}
}
})(current, previous);
Please mark my answer helpful and accept as a solution if it helped 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 08:16 AM
Hi @AnveshKumar M - I tried with the above script but it is not working out , it still shows as requested for the same person in second level of approval
Please find the attached screenshot .
How can I fix it ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 10:42 AM
Let me know, both these approvals trigger at same time or different times? I mean the second approval triggers after the first approval set is approved?
Are you using any work flow OR flow to trigger these approvals or approval policies?
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 10:21 PM
Hi @AnveshKumar M - I am using workflow for the approvals , and the approvals are triggering one after other .. the second approval triggers after the first approval .
How can I fix it ?