Dynamically Creating Workflow Approvals for Multiple Users

jmiskey
Kilo Sage

I need to dynamically create multiple approvals for multiple approvers.  Here is some background.

I have a table named "u_packages_approvers", which has the following fields:

u_package_name - unique package name

u_type - set to either "approval_needed" or "general_use"

u_approver - primary approver (all records with "u_type" set to "approval_needed" have a user entry here)

u_backup_approver - secondary approver (not required, only some records populated here)

Then, on my Catalog Item, we have a list collector variable named "packages_access" where they can select as many packages that they like.  I have come up with a Run Script action that currently loops through all the items in my list collector, looks lto see if they require approval, and if so, it creates an approval for the primary approver, and includes the package name in the approval type field ("u_approval_type").  My code is posted below:

//capture packages selected from variable
var pkgList = current.variables.packages_access.toString();

//loop through all packages
var pkgArray = pkgList.split(',');
for (var i = 0; i < pkgArray.length; i++) {
	var pkg = pkgArray[i];
	var gr = new GlideRecord('u_packages_approvers');
	gr.addQuery('sys_id', pkg);
	gr.query();

	//create approvals
	if (gr.next()){
		if (gr.u_type == 'approval_needed'){
			var appr1 = gr.u_approver;
			var appr2 = gr.u_backup_approver;
			var pkg_name = gr.u_package_name;

			//create approval
			var approval = new GlideRecord('sysapproval_approver');
			approval.initialize();
			approval.state = 'requested';
			approval.approver = appr1;
			approval.u_approval_type = pkg_name + ' Approval';
			approval.sysapproval = current.sys_id;   //Current is record that requires approval
			approval.insert();

		}
	}

}

That part all works fine.  Now, the tricky part is I want to add the "backup approvers".  So it should be a situation where if a single package has multiple approvers (primary and backup), an approval is created for each of them, but only one needs to accept/reject the approval (then the other one is unnecessary). 

I am not sure how to amend my code how to do that, especially since I am creating multiple approval records at once (for the various packages selected).  Obviously, some approvals will need to be related to each other (primary/backup for a single package), while others are not (approvals for the different packages).

Does that make sense?  Does anyone have any ideas on how to amend my code to do that?

Thanks

1 ACCEPTED SOLUTION

Coleton
Kilo Guru

You'd want to have  a BR that looks at the 'sysapproval_approver' table which runs when the state changes and the item they're approving is the item for this process (so a little dotwalking from the approval record to get the item name). When that state changes, query for records with the same 'document_id' and set the state to "No Longer Required".

That behavior will essentially be, "if one person approves this ticket, find the other approval records for this ticket and mark them as no longer needed". If you are having multiple packages selectable, which do not share approvers, this won't work. You would have to make a custom field on the approval table which allows you to relate primary/backup approvers. Then you could use that field to determine which approval records to set No Longer Required.

If you need assistance with writing the BR, just reply. Let me know if this answers your question by marking it as correct.

View solution in original post

8 REPLIES 8

Yep, if I was able to answer your question, please mark my answer as correct. Glad it got you where you needed to go.

bdsibert
Tera Contributor

Hi all,

 

This is a wonderful solution to a problem I've been having. However, I'm curious--how do you keep your workflow from going to the next task while you are waiting for your approvals to be performed. Right now, the proper approvals are being generated and an approval can be approved or rejected, but as soon as the Run Script is run, it goes to the next catalog task. This could pose an issue as the task could be provisioned before all approvals are obtained (whether approved or rejected). 

Thank you, again, for all of your insight!

What I did was add a field to the Requested Item table called u_proceed_with_workflow, and default the value to FALSE.  Then, in the Business Rule that I created (as mentioned above, to close out the unneeded approvals), I have it changing the value of this u_proceed_with_workflow field to TRUE.  Then, in my Workflow, after the Run Script action, I have a "Wait for condition", waiting for this value to be set to TRUE.

bdsibert
Tera Contributor

Thank you very, very much jmiskey--I truly appreciate your help! Is there any way you can share the business rule script that you used to change the value of the field to TRUE or how you implemented it into your other business rule?