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

Hi Stephen,

 

Sorry to bother you again, but I'm hoping you can point me in the right direction. 

As  you know, I am using a couple business rules and workflow scripts to add multiple approvers based on roles selected on the catalog item. On my sysapproval_approver table I have created a custom field called approval_type that is populated based on the role that is selected by the customer. On the TASK of the request item, I have a multi-line text field called Approved Roles. What I am trying to do with little success is write the value of what is in the approval_type column to the multi-line text field if that person approves the role associated with them. 

So, for example, on this ticket, I have approved:

 

I would then like to take the value in the Approval Type column and put it into the Approved Roles field on the task:

I've tried using the code below with no avail: 

When: after

Update

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

var psApproval = new GlideRecord('sysapproval_approver'); 
var psRitm = current.sysapproval; // the key value for all our queries
var role_type_ps=current.u_approval_type;
psApproval.addQuery('sysapproval',psRitm); // search for approvals related to our RITM
psApproval.query(); // execute ga query
while(psApproval.next()){ // review query results
if(psApproval.state=='approved'){ 
var psfRITM = new GlideRecord('sc_task'); // query to TASK table
psfRITM.addQuery('sys_id', psRitm); // find our specific RITM
psfRITM.setLimit(1); // we only need the 1 record
psfRITM.query(); // execute query
while(psfRITM.next()){ // when we find the record
psfRITM.variables.ps_financials_approved_roles="Hello"; //would set this to the Approval Type column but can't even get Hello to work
psfRITM.update(); // save the record
}
}

}
})(current, previous);

 

Can you please tell me what I'm doing wrong with my code or if this should just be a client side script and not a business rule? Thank you so much!

Help me understand the requirement better. What I'm getting at the moment is:

  1. After approvals complete, a task is created
  2. The task should be populated with the type values from the approval records

is that right? Or is it:

  1. When an approval completes, the RITM should be populated with the type from each approval record

Just trying to figure out what is needed at which junction from which record type(s).

-Stephen

Hi Stephen,

 

Thank you so much for your reply and again for all of your help with everything. I was essentially trying to accomplish #2 from above--adding a field value from the sysapproval table to a multi-text field on the corresponding task. I accomplished this by using the following Run Script:

 

(function(){
var addresses = "";
var rejected ="";
var gr = new GlideRecord('sysapproval_approver');
var sid=current.sys_id;
gr.addQuery('sysapproval',sid);
gr.query();
while (gr.next()) {
if(gr.state=='approved'){
addresses += gr.getDisplayValue("u_approval_type") + '\n';
}
else if (gr.state=='rejected'){
rejected += gr.getDisplayValue("u_approval_type") +'\n';
}
}
current.variables.ps_financials_approved_roles=addresses;
current.variables.ps_financials_rejected_roles=rejected;
current.update();
})();

That script looks good.

To apply it automatically, the business rule should be an "insert" rule on the 'sc_task' table (add conditions as you see fit, such as ['task.requested_item.item' is 'ourCatalogItem']. I would probably do an "after" rule.

This assumes that all approvals are complete before the task is created. If there are still approvals running that need to update the task, we have another business rule to add.

-Stephen