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-28-2023 12:50 AM
I have build a workflow to simulate this scenario and found the issue. Hope you are using Approval - Group activity in workflow like the one below.
This will not trigger the business rule as it won't set the approval state in change record. Hence we need to create a business rule on sysapproval_group table like the one below. Try the below steps and let me know.
1. Create After - Insert business rule on sysapproval_group table
2. Use the below script
(function executeRule(current, previous /*null when async*/ ) {
var assignmentgroup = current.parent.assignment_group.sys_id + '';
var cmdbcigroup = current.parent.cmdb_ci.assignment_group.sys_id + '';
if (assignmentgroup == cmdbcigroup) {
var approval = new GlideRecord('sysapproval_approver');
approval.addQuery('sysapproval', current.parent);
approval.addQuery('state', 'approved');
approval.query();
var approvers = [];
while (approval.next()) {
approvers.push(approval.getValue("approver"));
}
if (approvers.length > 0) {
var grpApproval = new GlideRecord(" x");
grpApproval.addQuery("parent", current.parent.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.parent);
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-28-2023 03:52 AM - edited 11-28-2023 03:54 AM
Hi @AnveshKumar M - I tried with the above code made change in this part of code :
if (approvers.length > 0)
{ var grpApproval = new GlideRecord(" x");
to
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 11:14 PM
HI @Pooja Khatri ,
I trust you are doing great.
(function executeRule(current, previous /*null when async*/) {
var assignmentGroup = current.assignment_group;
var cmdbCiGroup = current.cmdb_ci.assignment_group;
if (assignmentGroup == cmdbCiGroup) {
var approval = new GlideRecord('sysapproval_approver');
approval.addQuery('sysapproval', current.sys_id); // Ensure correct field name
approval.query();
while (approval.next()) {
var approver = approval.approver;
var ciApproval = new GlideRecord('sysapproval_approver');
ciApproval.addQuery('sysapproval', current.sys_id);
ciApproval.addQuery('group', cmdbCiGroup);
ciApproval.query();
while (ciApproval.next()) {
if (ciApproval.approver == approver) {
current.state = 'not_valid'; // Ensure 'not_valid' is a valid choice
current.update();
return; // Exit the script after updating
}
}
}
}
})(current, previous);
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi