Need to calculate the approval count greater than 50% in the Approval group activity in workflow

sivaranjani3
Kilo Contributor

Hi All,

 

In the Change Workflow for a particular group i need to calculate the % approvals.If the % of Approvals greater than 50% then only it is supposed to be Approved.For this wrote a code and its working fine but i gave that in "APPROVAL USER ACTIVITY " in the workflow. If i change that to "APPROVAL GROUP ACTIVITY" its not working. My intention of changing the Activity from USER to GROUP is to get the Name of the Assignment group to which the Particular Approver belongs in the APPROVERS Related List in the Change Form.

 

Code used in Approval User Activity

try{

var approvePercent = 50;

if((counts.approved/counts.total)*100 >= approvePercent){

  answer = 'approved';

 

}

if((counts.rejected/counts.total)*100 > (100 - approvePercent)){

  answer = 'rejected';

}

}

catch(e){

}

Please suggest if you faced this.

 

Thanks in Advance.

Sivaranjani.

9 REPLIES 9

Mark Stanger
Giga Sage

Looks like your script (minus the try/catch block) is based off of the one I created at SNGuru.   There's an example script for groups there too that should help.



http://www.servicenowguru.com/graphical-workflow/approval-percentage/


Hi Mark,
Mark Stanger :-



We are using a script similar to the one you have posted for the precentage calculation but it is not behaving the way we expected.



Suppose there are two groups for approvals A and B and they have 2 members each.



if any member from the first group A approves the request, then that particular group approval gets approved and the rest of the approvers from that group approval become NO Longer Required.(which is behaving as expected)



But when any user from Group B rejects the approval, the rest of the members from that Group still have the approval as Requested. (which is not behaving as expected rather should behave similar to the approved one) Though one of the member has rejected the approval so we are expecting   the approvals for the rest of the members from that group(Group B) to become No longer Required.



The concept behind this is that once the approvals from the rejected approval group become no longer required after the approval is rejected by anyone from that group, it would be easier for the script to calculate the percentage(based on Group Approvals) and set the approval or rejection accordingly.



If you could help in this then it would be great!!



Thanks in Advance.


Unless it's the exact script I posted on SNGuru I can't tell you why it's not behaving as expected.   But if you want to control what happens when an individual member of a group rejects you can do that in the section like this...you can see in the SNGuru article how I've added additional logic for an approval in this scenario.   That should give you an idea how to do something similar for rejection if you want.



    if((group.rejected/group.total)*100 > (100 - approvePercent)){


          answer = 'rejected';


          break;


    }



Mark Stanger



Hi,



Thanks for the reply.



We are using the same script of yours and we have only done some modificiation as below:-



var approvePercent = 50;


var approveCount = 0;


var rejectCount = 0;


for(var id in groups){


      var group = groups[id];


      if((group.approved/group.total)*100 >= approvePercent){


if(group.approved == 1)


    {


            var groupApproval = new GlideRecord('sysapproval_group');


            groupApproval.get(id);


            groupApproval.approval = 'approved';


            groupApproval.update();


            approveCount++;


            }


      }


      if((group.rejected/group.total)*100 > (100 - approvePercent)){


            if(group.rejected == 1)


      {


            var groupApproval1 = new GlideRecord('sysapproval_group');


            groupApproval1.get(id);


            groupApproval1.approval = 'rejected';


            groupApproval1.update();


            rejectCount++;


            }


      }


}


if(approveCount == counts.total){


      answer = 'approved';


}



Please let us know where we are making the mistake.



I hope you understood our requirement.