- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2019 08:47 AM
Hello,
I have a catalog item that sends an approval task to multiple people based on the role they selected on the form. So for example, Role A has Approver 1, 2, & 3 and Role B has Approver 4, 5. Only one person for each role needs to approve and after they approve the other approvers' state changes to No Longer Required. I have gotten this to work thanks to this post. I have taken the author's advice and added a custom column on the RITM table that when it changes to TRUE activates a wait task that I have in my workflow. This is so that the workflow does not proceed to fulfillment before all of the approvals have been answered (whether approved or rejected).
The trouble I am having, though, is getting the column to change to TRUE only after all the approvers have made a decision (meaning that their state is no longer=Requested). Right now, the column changes to TRUE after any sort of approval decision is made (meaning an approval from Role A changes the column to TRUE even though the approvers from Role B haven't responded yet).
I am using this business rule from the other post, which changes the state of the other approvers for each role to Not Required:
(function executeRule(current, previous /*null when async*/) {
var role_type=current.u_approval_type;
var ritm=current.sysapproval;
var sid=current.sys_id;
var role_gr=new GlideRecord('sysapproval_approver');
role_gr.addQuery('sys_id','!=',sid);
role_gr.addQuery('sysapproval', ritm);
role_gr.addQuery('u_approval_type',role_type);
role_gr.addQuery('state', 'requested');
role_gr.query();
while(role_gr.next()){
role_gr.state='not_required';
role_gr.update();
}
})(current, previous);
This additional code seems to change the RITM column to TRUE:
if(gr.get(ritm)){
gr.u_proceed_with_workflow=true;
gr.update();
}
My question is, do I need to create a separate business rule to update the RITM column that runs only when there are no longer any approvers with a Requested state for that particular request item? If so, how would I go about implementing that? Any other thoughts or insight is greatly appreciated.
Solved! Go to Solution.
- Labels:
-
Best Practices
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2019 01:17 PM
Hi bdsibert - You'll have to check for sibling approvals to all be !='requested', then lookup the target request and modify the TRUE value. Something like this:
var gaApproval = new GlideAggregate('sysapproval_approver'); // Best Practice is to use GlideAggregate instead of GlideRecord, since we're only returning a count
var ourRitm = current.sysapproval; // the key value for all our queries
gaApproval.addQuery('sysapproval',ourRitm); // search for approvals related to our RITM
gaApproval.addQuery('state','requested'); // checking for approvals with state 'requested'
gaApproval.addAggregate('COUNT'); // the aggregate we're running is a count
gaApproval.query(); // execute ga query
while(gaApproval.next()){ // review query results
var approvalCount = gaApproval.getAggregate('COUNT'); // retrieve our ga result
if(approvalCount == 0){ // if the count is 0, then we update the RITM
var grRITM = new GlideRecord('sc_req_item'); // query to RITM table
grRITM.addQuery('sys_id', ourRitm); // find our specific RITM
grRITM.setLimit(1); // we only need the 1 record
grRITM.query(); // execute query
while(grRITM.next()){ // when we find the record
grRITM.u_proceed_with_workflow=true; // set the value
grRITM.update(); // save the record
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2019 01:17 PM
Hi bdsibert - You'll have to check for sibling approvals to all be !='requested', then lookup the target request and modify the TRUE value. Something like this:
var gaApproval = new GlideAggregate('sysapproval_approver'); // Best Practice is to use GlideAggregate instead of GlideRecord, since we're only returning a count
var ourRitm = current.sysapproval; // the key value for all our queries
gaApproval.addQuery('sysapproval',ourRitm); // search for approvals related to our RITM
gaApproval.addQuery('state','requested'); // checking for approvals with state 'requested'
gaApproval.addAggregate('COUNT'); // the aggregate we're running is a count
gaApproval.query(); // execute ga query
while(gaApproval.next()){ // review query results
var approvalCount = gaApproval.getAggregate('COUNT'); // retrieve our ga result
if(approvalCount == 0){ // if the count is 0, then we update the RITM
var grRITM = new GlideRecord('sc_req_item'); // query to RITM table
grRITM.addQuery('sys_id', ourRitm); // find our specific RITM
grRITM.setLimit(1); // we only need the 1 record
grRITM.query(); // execute query
while(grRITM.next()){ // when we find the record
grRITM.u_proceed_with_workflow=true; // set the value
grRITM.update(); // save the record
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2019 02:00 PM
Hi Stephen,
I can't thank you and Mike enough for your help--I truly appreciate it! Your code makes a ton of sense and I appreciate you adding in all the details. Unfortunately, I'm still unable to get it to work.
Am I wrong to think that the code above only runs once instead of looping or waiting for all the approvers' states !=requested? Meaning it finds all the approvals for our particular RITM, sees that some of the approvers have state=requested, therefore does not set the u_proceed_with_workflow to TRUE and goes to the next workflow activity (which happens to be a Wait for activity that is waiting for u_proceed_with_workflow to TRUE).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2019 06:59 PM
No worries. Glad to help! That's what the community is for.
The code above runs in an update business rule on the 'sysapproval_approver' table. That way every time an approval record is updated, it does the check. This negates the need to loop within the script.
You can set it as an "After" rule to ensure the approvals are saved and updated and then the script runs After the values are in place. That avoids having the current record not yet set to "Approved".
The business rule can be set to run conditionally as well, that way it's only running on records you need, for example,
sysapproval.task_type is RITM
AND
sysapproval.u_proceed_with_workflow is FALSE
This ensures you're only applying this rule to Requested Items that are not yet marked as TRUE to proceed.
Let us know how it goes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-02-2019 08:43 AM
Thank you, Stephen, so very much! This worked brilliantly and everything appears to be working as it should.
Just for anyone else who stumbles across this who is just starting out like me, it took me a second to realize that I had to set the business rule to order 200 so that it is forced to run after the other business rule that is referenced in this post (that is set to order 100). It wasn't initially apparent to me but makes a lot of sense.
Thank you, again--I hope you have a wonderful day!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-02-2019 08:45 AM
No problem! Glad it worked out for you, and that you found the order value. See you next week at Knowledge19!