Check if approval records exist. If not wait for one to exist before proceeding.

s_sterner
Mega Guru

Hi All,

Have a requirement for a Minor Change Request to have manual approvers before proceeding with the workflow. As we all may or may not know the Manual Approvals workflow item will bypass "skip" and say its approved when no records exist in the sysapproval table for the change request.

My thoughts were to query the sysapproval_approvers table to see if an approver record existed and if not either a BR onSubmit that says one doesnt exist please add an approver or build it into the workflow by running a script in the workflow to check or inserting a Wait for Condition workflow item. Tried all the above and was getting mixed results but the one I landed on was the Wait for Condition workflow item with the condition written as such -

// Set the variable 'answer' to true or false to indicate if the condition has been met or not.

var app_count = 0;

var approval = new GlideRecord("sysapproval_approver") ;

approval.addQuery("parent" , current.sys_id);

approval.addQuery("state" ,"requested");

approval.query();

var app_count = approval.getRowCount();

if(app_count !=0) {

  answer = false;

}

else {

  answer = true;

}

So with it as is the workflow will pause on the wait for condition when no approver is added.... great.... however when one is added it is still waiting on the condition which should be met now being that an approver record exists and the state of the record is requested.

Can someone help me identify why when an approval record exists and the state is requested that my Wait for Condition is not saying the condition is met?

find_real_file.png

4 REPLIES 4

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Sean,



Wait for condition activity will be triggered only when there is an update on the current record. In this case you may have to trigger the workflow via script from approval table.


Workflow Script - ServiceNow Wiki


Hi Pradeep and SNC,



Thank you for your reply Pradeep!
This is what I landed on and thought I would share in case anyone else needed something similar for manual approvals!
I put this into a separate Request Approval UI action that shows up under certain conditions being met -



//Minor Approver Check


if(current.type == 'minor'){


var approval = new GlideRecord("sysapproval_approver");


var count = approval.getRowCount();


approval.addQuery("sysapproval" , current.sys_id);


approval.addQuery("state", "not requested");


approval.query();


while (approval.next()){


if (count == '0'){


gs.addErrorMessage('There are currently ' + count + ' approvers in your change. Please add approvers before proceeding.');


current.setAbortAction(true);


} else if (count > '0'){


approval.setValue("state", "requested");


}


}


}


Hi Sean,



I'm looking to do exactly what you're taking about but having trouble implementing the UI action part to get the workflow to recognize when the approval record is inserted.   I'm using this for the change_request table and need it to pause at this step if a certain field is not yet filled in so that the approval step doesn't get skipped.   Once the field is filled in, it should trigger the workflow to generate the approval record.   So I'm not quite sure how the UI action works to accomplish that.   Any further detail you can provide would be greatly appreciated.


Hi Eric,



I would be glad to show you what I landed on for this from a workflow and UI action stand point. Sorry it has been so long im sure you figured it out by now but in case anyone else has the same issue .



UI Action -
Name - Request Approval
Action name - minor_pending_project_signoff
Condition - gs.hasRole('itil') && !new ChangeRequestStateHandler(current).isOnHold() && new ChangeRequestStateHandler(current).isNext("pending_project_signoff") && current.type == 'minor'


Client - Checked


Form Button - Checked


Onclick - moveToPendingProjectSignoff();


Script -


function moveToPendingProjectSignoff(){


//State Handler


var ga = new GlideAjax("ChangeRequestStateHandlerAjax");


ga.addParam("sysparm_name", "getStateValue");


ga.addParam("sysparm_state_name", "pending_project_signoff");


ga.getXMLAnswer(function(stateValue) {


g_form.setValue("state", stateValue);


gsftSubmit(null, g_form.getFormElement(), "minor_pending_project_signoff");


});


}


//Minor Approver Check


if(current.type == 'minor');{


var approval = new GlideRecord("sysapproval_approver") ;


approval.addQuery("sysapproval" , current.sys_id);


approval.addQuery("state", "IN", "not requested, not_required");


approval.query();


while (approval.next());{


var count = approval.getRowCount();


if (count == '0'){


gs.addErrorMessage('There are currently ' + count + ' approvers in your change. Please add approvers before proceeding.');


current.setAbortAction(true);


} else if (count > '0'){


approval.setValue("state", "requested");


}


}


}




if (typeof window == 'undefined')


setRedirect();




function setRedirect() {


current.update();


action.setRedirectURL(current);


}




Workflow - The highlighted path is for our Minor tickets which don't automate approvers so we rely on manual approvers being added. The magic happens at the Pending Project Signoff section since its the UI action that moves us to the pending project signoff and the UI Action checks if there are any approvers and if not it will do an invalid update.


find_real_file.png