1

I have two approval processes in my org for an object Grant Contract Modification

first one being Grant Contract Mod Approval process and

the second one being FinanceApprovalProcessGrantContractMod.

Here the flow is in such a way that:

Fields used picklist

Approval process picklist values Approved, Approval Pending, Rejected.

Financial Approval status values Approved, Approval Pending, Rejected

Stages In progress, Ready for Program Approval, Program Approved and Finance Approved.

Also a text field to capture the financial approval process comments and store it

In Grant Contract Mod Approval process the intial condition is Stage(Picklist field) equal to Ready for Program Approval

In the initial submission field update a picklist named Approval Process changes to Approval Pending and an GCProcess invoked checbox becomes true.

In the final approval action field updates are such that the stage value changes to Program Approved and the Approval process value becomes approved.

In the final reject action field updates are such that the stage value changes to in progress and the approval process value becomes rejected.

In FinanceApprovalProcessGrantContractMod ie the second approval process the intial condition is if the stage becomes 'Program Approved'. ie only if the first approval process becomes approved then the second approval process is triggered.

In the second approval process the initial submission action is such that a checkbox FinanceApprovalProcessInvoked is checked and the picklist finance approval status becomes approved.

In the Final Approval Action of the second approval process the stage is updated to Finance Approved and the Finance Approval Status becomes Approved.

In the Final Rejection Action the FinanceApproval Status becomes rejected and the GrantContractApprovalProcessInvoked and FinanceApprovalProcessInvoked becomes false and the stage becomes In progress.

These two approval process are called in the trigger and in the second approval process ie Finance Approval Process during the approval or rejection the comments are to be captured according to my requirement.

Issue I have done this and it is working but on continous testing I found an issue. If I approve the first process and then reject the second and again submit the first approval process for approval and reject again and in the third attempt approve both then instead of recording the comment in the third attempt the second attempt comment is recorded.

Code: Method used: helper class

public class HelperClass_ApprovalTrigger
      Public static void afterTriggerGCM(List<Grant_Contract_Modification__c> newList) {
                User gUser = new User();
                gUser=[SELECT Id FROM User WHERE Id =:UserInfo.getUserId()];    
                List<Grant_Contract_Modification__c> lstAcc= new List<Grant_Contract_Modification__c>();
                List<Id> Ids = New List <Id>();    
                For(Grant_Contract_Modification__c c:newList){
                        Ids.add (c.Id);
                        Grant_Contract_Modification__c obj=new Grant_Contract_Modification__c();
                        obj.id= c.Id;
                        If(c.Modification_Stages__c =='Ready for Program Approval' && String.isNotBlank(c.Request_Approval_From__c)  && c.GrantContarctApprovalProcessInvoked__c == false ){            
                            obj.Requested_By__c = gUser.Id;
                            Approval.ProcessSubmitRequest req= new Approval.ProcessSubmitRequest();
                            req.setComments('Request for approving');
                            req.setObjectId(c.id);
                            req.setProcessDefinitionNameOrId('Grant_Contract_Mod_Approval_Process');
                            Approval.ProcessResult res = Approval.process(req);
                            System.debug('obj.Requested_By__c'+obj.Requested_By__c);                }
                        if(c.Approval_process__c=='Approved' && String.isBlank(c.Approved_By__c) && c.GrantContarctApprovalProcessInvoked__c == true  ){
                           
                            obj.Approved_By__c = gUser.Id;
                            System.debug('obj.Approved_By__c'+obj.Approved_By__c);
                           
                        }        
                        If(c.Modification_Stages__c =='Program Approved' && c.GrantContarctApprovalProcessInvoked__c == true && c.FinanceApprovalProcessinvoked__c == false) {
                 
                            system.debug('c.Modification_Stages__c'+c.Modification_Stages__c);
                            Approval.ProcessSubmitRequest req1= new Approval.ProcessSubmitRequest();
                            req1.setComments('Request for approving from the finance team ');
                            req1.setObjectId(c.id);
                            req1.setProcessDefinitionNameOrId('Finance_ApprovalProcessGrant_ContractMod');
                            req1.setNextApproverIds(new Id[]{c.Request_Approval_From__c});
                            Approval.ProcessResult res1 = Approval.process(req1);
                            System.debug('offer submitted for approval successfully: '+res1);
                        }
                        If( (c.Modification_Stages__c =='Finance Approved' && c.GrantContarctApprovalProcessInvoked__c == true && c.FinanceApprovalProcessinvoked__c == true)||(c.Modification_Stages__c == 'In Progress' && c.Finance_Approval_Status__c == 'Rejected')) {
                            List<ProcessInstance> instances = [select Id, TargetObjectId,ProcessDefinitionId,ProcessDefinition.Name from ProcessInstance
                                                               where TargetObjectId in :Ids ];
                           
                            List<id> Ids1 = New List <Id>();
                            for(ProcessInstance pi:instances){
                               
                                if(pi.ProcessDefinition.Name =='Finance_ApprovalProcessGrant_ContractMod'){
                                    Ids1.add (pi.Id);
                                }
                               
                            }
                            List<ProcessInstanceStep> instancesSteps = [select Comments,StepStatus,ProcessInstanceId from ProcessInstanceStep where ProcessInstanceId in :Ids1];
                           
                            for(ProcessInstanceStep pis:instancesSteps){
                                if(pis.StepStatus == 'Approved' || pis.StepStatus == 'Rejected'){
                                    System.debug('#*pis.Comments'+pis.Comments);
                                    obj.Finance_Approval_Notes__c=pis.Comments;
                                }  
                               
                            }                
                           
                           
                        }
                        lstAcc.add(obj);
                   }
                   
                }
               
                update lstAcc;
                System.debug('lstAcc'+lstAcc);
            }
        }

Trigger:

trigger Approval_Trigger_for_Grant_Contract_Modification on Grant_Contract_Modification__c (after update,after insert) {
If(trigger.isafter){
        If(trigger.isinsert){
            HelperClass_ApprovalTrigger.afterTriggerGCM(trigger.new);
        }

    If(trigger.isupdate){
        HelperClass_ApprovalTrigger.afterTriggerGCM(trigger.new);
        }
}
}

What changes do I have to make in the helper class and the trigger such that every time the approval process is triggered the comment gets overwritten. Right now it is not working for bulk tries. Only works for three - four attempts and then the comments arent overwritted.

0

You must log in to answer this question.

Browse other questions tagged .