Auto approval business rule does not advance workflow

matt v_
Mega Expert

Hello,

We have a before insert/update business rule in place on the sysapproval_approver table to check if approver is the person who initiated the request (document_id.opened_by), and if so will change the state to 'approved'.  I can't imagine why this would not be default behavior, but nevertheless, this rule is working as intended.

Most of our approvals are fired from HR Case workflows, and the issue here is that even thought the approval request is updated correctly, the workflow gets stuck as though it is still waiting for that approval.

When we know of a case with this issue, I can Nudge that workflow and it moves to the next step.  We also found that if anyone makes a change and updates the case, this also triggers the workflow to move along.

I would prefer to make some small edit to the business rule if possible.  My first thought was an update() command, but a little reading showed that to be a really bad idea.  Is there anything else I can add to the script after updating the approval state?

A workaround (haven't tried yet) that seems like a poor band-aid would be a scheduled job that nudges workflows daily.  That just seems really silly to have to do.

Any suggestions?  Or is there even another, better way to accomplish an auto approval if the approver is also the requester?

 

Here's the script for reference (not created by me).  It may seem a bit convoluted, but this is due to the fact that some of our workflows are set to pre-generate tasks/approvals, so we don't want to change the state of an approval if the prior steps haven't been completed yet.

(function executeRule(current, previous /*null when async*/) {
	var autoApprove = false;
	
	if (current.operation() == 'insert' && current.approver == current.document_id.opened_by) {
		autoApprove = true;
	}
	
	var sameApproval = new GlideRecord('sysapproval_approver');
    sameApproval.addEncodedQuery('approver=' + current.approver + '^sysapproval=' + current.sysapproval + '^state=approved');
	sameApproval.query();
	
	if (sameApproval.next()) {
		if (current.operation() == 'insert') {
		  autoApprove = true;
		}
		if (current.operation() == 'update' && previous.state == 'not requested' && current.state == 'requested') {
			autoApprove = true;
		}
	}
	
	
	// Add your code here
	if (autoApprove) {
		current.state = 'approved';
	}

})(current, previous);
1 ACCEPTED SOLUTION

That didn't seem to work, and it also resulted in a 'unique key violation' for duplicate entry of the approval Sys ID.

After all the testing back and forth, and trying different ways of doing the same thing, I ended up adding a 2 second wait timer at the beginning of the workflow and this resolved the issue.  I was hoping to avoid that, but it seems that the first few steps are just happening too quickly.

Thanks for all the suggestions!

View solution in original post

15 REPLIES 15

I've tried that, but then the rule does not actually update the approval request at all and it still sits as Requested.

You need to add current.update(); after current.state = 'approved';

That was my first thought when starting this fix, but everything I've read points to that being a bad idea.  Or are you saying this isn't an issue with an After business rule?

I just tried this anyway, just to see if it made any difference, but it still does not run the script.  I get the approval created as Requested.

Chris Sanford1
Kilo Guru

If you need tasks and approvals to complete before proceeding, I think a better approach would be to branch your workflow to two separate branches, one for tasks and one for approvals. Then wait for both branches to complete before proceeding.

As to the issue at hand, there's an out of box business rule that should handle this for you after state changes to approved, called 'SNC - Run parent workflows (Approval)'. Do you have it enabled?