Variable number of approvals in a workflow -- approval records being overwritten

truzicki
Kilo Expert

Hello, y'all!   I'm stuck and hoping someone can help.   I've created a request item workflow that (in theory) should generate 1 to n approvals, based on options in the request.   The disposition of the approvals is not meant to approve/reject the entire request item, but only the specific options contained within.   For example, the item might contain three roles -- the user may request one, two, or all three of the roles.   The workflow should generate an approval (based on a lookup table that contains the roles) for each selected role.   I'm using a loop to accomplish this, and have built an if-statement to determine if the approval is a user or a group approval (again, based on the reference table).   I should mention this is meant to scale -- so while I could easily accomplish this by adding three approval activities and writing if-statements for each role in the workflow, I may want 20+ roles.   Adding that many approval activities would be...troublesome!   Also, the requirement I'm face with is to ensure this runs on one item -- so I can't beak them into individual items and bundle them in an order guide.  

OK, so all this is working pretty well...except for when I have multiple approvals of the same type.   In the case of my three roles, if I pick the two that generate user approvals, the second loop overwrites the first user approval that's generated.   I even put a one-minute timer in there to ensure I was building the first approval correctly...and then watched it get overwritten the next time through the loop.  

Does anyone have any suggestions about how I can fix this?   Or suggestions in general for dealing with these requirements?   Any thoughts/help would be greatly appreciated!

Thank you!

1 ACCEPTED SOLUTION

truzicki
Kilo Expert

Thanks, Adam -- I appreciate the reply.   My problem was that I needed the approvals to run in parallel, so I couldn't run script after the approval activity had completed.  



However, I did more poking around in the community, and found two phenomenal posts that helped me solve this.   The first post (Is it possible to clone a workflow activity definition with all activity variables) provided a way to clone a workflow activity definition.   A HUGE thank you to Jim Coyne, Adam Nock/Adam Nock, and rfedoruk for that solution -- truly, fellas, that was AWESOME!!!



I cloned the Approval - Group and Approval - User activity definitions.   Then I used the information found in the second post (sub workflows and approvals) to modify those custom activity definitions.   There is a function called _checkDroppedApprovals that deletes the approval records created earlier in the loop.   Turns out Julian Poyntz, Dominik Å imůnek and I ran into pretty much the same issue.   Per Dominik's suggestion, I commented out gr.deleteRecord() in the function (in my new, custom activity definitions).   It works perfectly!   And, the best part is that I didn't touch the OOTB workflow activity definitions, and now have two custom activities that I can use in future workflows.   So, also a HUGE thank you to to Julian and Dominik!!!  



My very best regards,


-Tim.


View solution in original post

3 REPLIES 3

truzicki
Kilo Expert

In one iteration of trying to get this to work, I also tried to script the approval activities.   I could create the approval records and associate them correctly to the RITM, but I couldn't get them to behave in the same way as an approval activity.   For example, if I built a group approval, and one of the group members approved, I wanted the approval to process and the remaining approvals to no longer be needed.   Unfortunately, I wasn't able to figure out how to do that last bit.


I think I had the same issue as you.   It's a known problem and will apparently be addressed in Geneva.   As a workaround, you can add a run script after the approval activity in your workflow, and have the script contain the below.



var app = new GlideRecord('sysapproval_approver');


app.addQuery('document_id',current.sys_id);    


app.query();


while(app.next())


{


app.wf_activity = '';


app.update();


}


truzicki
Kilo Expert

Thanks, Adam -- I appreciate the reply.   My problem was that I needed the approvals to run in parallel, so I couldn't run script after the approval activity had completed.  



However, I did more poking around in the community, and found two phenomenal posts that helped me solve this.   The first post (Is it possible to clone a workflow activity definition with all activity variables) provided a way to clone a workflow activity definition.   A HUGE thank you to Jim Coyne, Adam Nock/Adam Nock, and rfedoruk for that solution -- truly, fellas, that was AWESOME!!!



I cloned the Approval - Group and Approval - User activity definitions.   Then I used the information found in the second post (sub workflows and approvals) to modify those custom activity definitions.   There is a function called _checkDroppedApprovals that deletes the approval records created earlier in the loop.   Turns out Julian Poyntz, Dominik Å imůnek and I ran into pretty much the same issue.   Per Dominik's suggestion, I commented out gr.deleteRecord() in the function (in my new, custom activity definitions).   It works perfectly!   And, the best part is that I didn't touch the OOTB workflow activity definitions, and now have two custom activities that I can use in future workflows.   So, also a HUGE thank you to to Julian and Dominik!!!  



My very best regards,


-Tim.