Change a Request Item Field Based on Approvals

bdsibert
Tera Contributor

Hello,

I have a catalog item that sends an approval task to multiple people based on the role they selected on the form. So for example, Role A has Approver 1, 2, & 3 and Role B has Approver 4, 5. Only one person for each role needs to approve and after they approve the other approvers' state changes to No Longer Required. I have gotten this to work thanks to this post. I have taken the author's advice and added a custom column on the RITM table that when it changes to TRUE activates a wait task that I have in my workflow. This is so that the workflow does not proceed to fulfillment before all of the approvals have been answered (whether approved or rejected). 

The trouble I am having, though, is getting the column to change to TRUE only after all the approvers have made a decision (meaning that their state is no longer=Requested). Right now, the column changes to TRUE after any sort of approval decision is made (meaning an approval from Role A changes the column to TRUE even though the approvers from Role B haven't responded yet). 

I am using this business rule from the other post, which changes the state of the other approvers for each role to Not Required: 

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

var role_type=current.u_approval_type;
var ritm=current.sysapproval;
var sid=current.sys_id;
var role_gr=new GlideRecord('sysapproval_approver');
role_gr.addQuery('sys_id','!=',sid);
role_gr.addQuery('sysapproval', ritm);
role_gr.addQuery('u_approval_type',role_type);
role_gr.addQuery('state', 'requested');
role_gr.query();
while(role_gr.next()){
role_gr.state='not_required';
role_gr.update();
}
})(current, previous);

This additional code seems to change the RITM column to TRUE:

if(gr.get(ritm)){
gr.u_proceed_with_workflow=true;
gr.update();
}

My question is, do I need to create a separate business rule to update the RITM column that runs only when there are no longer any approvers with a Requested state for that particular request item? If so, how would I go about implementing that? Any other thoughts or insight is greatly appreciated. 

1 ACCEPTED SOLUTION

Hi bdsibert - You'll have to check for sibling approvals to all be !='requested', then lookup the target request and modify the TRUE value. Something like this:

 

var gaApproval = new GlideAggregate('sysapproval_approver'); // Best Practice is to use GlideAggregate instead of GlideRecord, since we're only returning a count
	var ourRitm = current.sysapproval; // the key value for all our queries
	gaApproval.addQuery('sysapproval',ourRitm); // search for approvals related to our RITM
	gaApproval.addQuery('state','requested'); // checking for approvals with state 'requested'
	gaApproval.addAggregate('COUNT'); // the aggregate we're running is a count
	gaApproval.query(); // execute ga query
	while(gaApproval.next()){ // review query results
		var approvalCount = gaApproval.getAggregate('COUNT'); // retrieve our ga result
		if(approvalCount == 0){ // if the count is 0, then we update the RITM
			var grRITM = new GlideRecord('sc_req_item'); // query to RITM table
			grRITM.addQuery('sys_id', ourRitm); // find our specific RITM
			grRITM.setLimit(1); // we only need the 1 record
			grRITM.query(); // execute query
			while(grRITM.next()){ // when we find the record
				grRITM.u_proceed_with_workflow=true; // set the value
				grRITM.update(); // save the record
			}
		}
	}
-Stephen

View solution in original post

13 REPLIES 13

Mike Allen
Mega Sage

I would do a script that says, after update business rule after the approval, query the approval table to ensure no other approvals are waiting (which will take into account this second level of approval), and if they are not, then flip the flag.

 

Your code just says if there is a request item, flip the flag, which will always be true.

Hi Mike,

Thank you so much for your response! Just to make sure I'm understanding you correctly, would you just create a Run Script in your workflow that runs directly before my Wait For activity?

That'll certainly work.

Sorry to bother you again, Mike--I have to be honest, I'm just starting out with queries and ServiceNow development. I have tried putting together a script, but all it is doing is creating a new record in the sc_req_item table and setting it's u_proceed_with_workflow column to true. Therefore it isn't adjusting the current RITM after all the approvers have responded. Any chance you can tell me what I'm doing wrong (or how far off I sure I am)?

var approval_flag=new GlideRecord('sysapproval_approver');
var gr_ritm=current.sysapproval;
approval_flag.addQuery('sysapproval',gr_ritm);
approval_flag.query();
if(approval_flag.state!='requested')
{
var gr=new GlideRecord('sc_req_item');
gr.u_proceed_with_workflow=true;
gr.update();
}