Approval Wait for Condition Stuck and will not move forward

Tony Landowski
Kilo Contributor

I have the below Wait for condition that I am trying to find out why it will not move forward to the next step of the workflow.  Can someone please help.  Note: the need is to wait for both approvals before moving to the next step.

 

Script:

var rec = new GlideRecord('sysapproval_approver');
rec.addQuery('sysapproval', current.sys_id);
rec.addQuery('active', true);
rec.query();
if(rec.hasNext()){
answer = false;
}
else{
answer = true;
}

 

Workflow:

find_real_file.png

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Your workflow is on RITM table but the actual update will happen on Approval table.

So workflow won't know when to process the wait for condition.

Wait for condition is processed when update happens on that record in this case RITM

So workaround is create after update BR on sysapprovap_approver table and mimic the update on RITM record so that workflow evaluates the condition and proceeds if all approvals are done

Condition: current.sysapproval.cat_item.name == 'Your Catalog Item Name Here' && (current.state == 'approved' || current.state == 'rejected')

Script:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here

var gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_id',current.sysapproval);
gr.query();
if (gr.next()) {
 new Workflow().broadcastEventToCurrentsContexts(gr, 'update', null);
}


})(current, previous);

Also update your wait for condition script as below

answer = false;

var rec = new GlideRecord('sysapproval_approver');
rec.addQuery('sysapproval', current.sys_id);
rec.addQuery('state', 'requested'); // check if any approval for this RITM is in requested state
rec.query();
if(!rec.next()){
answer = true; // if not found then proceed

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Your workflow is on RITM table but the actual update will happen on Approval table.

So workflow won't know when to process the wait for condition.

Wait for condition is processed when update happens on that record in this case RITM

So workaround is create after update BR on sysapprovap_approver table and mimic the update on RITM record so that workflow evaluates the condition and proceeds if all approvals are done

Condition: current.sysapproval.cat_item.name == 'Your Catalog Item Name Here' && (current.state == 'approved' || current.state == 'rejected')

Script:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here

var gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_id',current.sysapproval);
gr.query();
if (gr.next()) {
 new Workflow().broadcastEventToCurrentsContexts(gr, 'update', null);
}


})(current, previous);

Also update your wait for condition script as below

answer = false;

var rec = new GlideRecord('sysapproval_approver');
rec.addQuery('sysapproval', current.sys_id);
rec.addQuery('state', 'requested'); // check if any approval for this RITM is in requested state
rec.query();
if(!rec.next()){
answer = true; // if not found then proceed

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Tony Landowski 

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

 

I'm trying the similar thing for normal change WF!

In this case, there are multiple approvals that I need to check, If one of them has approved or not.

basically I'm trying to process the state to authorize, even when change is on-hold but assess approvals are completed.

I've wrote the below script to check the approvals of assess state, *it may not be the finest script you've seen* Please help me to achieve the approvals check! 

var arr = [];
var approval;
var isRequested = true;
var ac = new GlideRecord('sysapproval_approver');
ac.addQuery('sysapproval', current.sys_id);
ac.addQuery('group.assignment_group',"!=", "");//Any Approval Group
ac.addQuery('group.assignment_group', "!=", 'b85d44954a3623120004689b2d5dd60a'); //CAB Approval Group
ac.query();

while (ac.next()) {
    arr.push(ac.state);
}

for (var i=0; i < arr.length; i++) {
    if (arr[i] != 'requested') { // approved/rejected
		isRequested = false;
    }
	else{
    isRequested = true; //requested
	}
}

answer = ifScript();

function ifScript() {
    if (isRequested == true ) { //requested
        return false;
    } 
       return true;
    }

 

Let me know if any more inputs are required! This is the only useful article I saw regarding this topic, so hoping for the best!

 

Thanks,

Laukik

asifnoor
Kilo Patron

Hi,

In your workflow, when approved simply add an approval action -> Approved. Then that will update your underlying table RITM and your workflow will resume.

Mark the comment as a correct answer and also helpful if it helps to solve the problem.