function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vajralavajrala 

I faced probloms while writing test class for the trigger

I wrote the trigger for dynamic approval process ,i.e sunmit,Approve and reject successfully.

But i faced problems while writing the test class for this trigger to cover the code.please give me solution for the following trigger

to cover the code.

trigger AutomateApprove on Patient__c(After insert, After update)
{
    for (Integer i = 0; i < Trigger.new.size(); i++){
        try{
            //Insure that previous value not equal to current, else if there is any field update on approval process action
            //there will be recurrence of the trigger.
            if(Trigger.new[i].Next_Step__c == 'Submit' && ((Trigger.isInsert || Trigger.old[i].Next_Step__c != 'Submit'))){
                submitForApproval(Trigger.new[i]);
            }
            else if(Trigger.new[i].Next_Step__c == 'Approve' && ((Trigger.isInsert || Trigger.old[i].Next_Step__c != 'Approve'))){
                approveRecord(Trigger.new[i]);
            }
            else if(Trigger.new[i].Next_Step__c == 'Reject' && ((Trigger.isInsert || Trigger.old[i].Next_Step__c != 'Reject'))){
                rejectRecord(Trigger.new[i]);
            }

        }catch(Exception e){
            Trigger.new[i].addError(e.getMessage());
        }
    }

    // This method will submit the opportunity automatically
    public void submitForApproval(Patient__c opp){
        // Create an approval request for the Opportunity
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval automatically using Trigger');
        req1.setObjectId(opp.id);
        req1.setNextApproverIds(new Id[] {opp.Next_Approver1__c});

        // Submit the approval request for the Opportunity
        Approval.ProcessResult result = Approval.process(req1);
    }

    //Get ProcessInstanceWorkItemId using SOQL
    public Id getWorkItemId(Id targetObjectId){
        Id retVal = null;
        for(ProcessInstanceWorkitem workItem  : [Select p.Id from ProcessInstanceWorkitem p where p.ProcessInstance.TargetObjectId =: targetObjectId]){
            retVal  =  workItem.Id;
        }

        return retVal;
    }

    // This method will Approve the opportunity
    public void approveRecord(Patient__c opp){
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setComments('Approving request using Trigger');
        req.setAction('Approve');
        req.setNextApproverIds(new Id[] {opp.Next_Approver1__c});
        Id workItemId = getWorkItemId(opp.id);
        //opp.addError(workItemId);
        if(workItemId == null){
            opp.addError('Error Occured in Trigger');
            //opp.addError(workItemId);
        }
        else{
            req.setWorkitemId(workItemId);
            // Submit the request for approval
            Approval.ProcessResult result =  Approval.process(req);
        }
    }

    // This method will Reject the opportunity
    public void rejectRecord(Patient__c opp){
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setComments('Rejected request using Trigger');
        req.setAction('Reject');
        //req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
        Id workItemId = getWorkItemId(opp.id);
        //opp.addError(workItemId);

        if(workItemId == null){
            opp.addError('Error Occured in Trigger');
            //opp.addError(workItemId);
        }
        else{
            req.setWorkitemId(workItemId);
            // Submit the request for approval
            Approval.ProcessResult result =  Approval.process(req);
        }
    }

}
S2S2

Hi,

 

Even i am facing the same problem and if you have a code can you please share the code.