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
Marco PinderMarco Pinder 

Help with Test Method of Approval Process

Hi all,

 

I have written a trigger on the Campaign object that works as required. Basically if a field (Estimated Cost) is above £25 then the trigger fires and the Campaign is submitted for approval (process is already set up). The code for teh trigger is as follows:

 

trigger AuthSpendProcessApproval on Campaign (after insert){
    
    for (Campaign c : trigger.new){
    
        if (c.Estimated_Cost_per_head__c >= 25){
                
            // Create an approval request for the Campaign
        
            Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitrequest();
            app.setObjectId(c.Id);
        
            //Submit the approval request for the Campaign
        
            Approval.ProcessResult result = Approval.process(app);
        }
    }
}

 However I am now trying to create a Test Method, but after following some guidance online I am getting stuck. The Test Method I have written so far is:

 

@isTest
private class AuthSpendProcessApprovalTest{
    static testMethod void AuthSpendProcessApprovalTest(){
    
    //Set up Campaign record

    Campaign c = new Campaign(Name = 'Test', Estimated_Cost_per_head__c = 26);
    
    // Cause the trigger to execute
    
    insert c;
    
    //Create an approval request for the Campaign
    
    Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitrequest();
    app.setObjectId(c.Id);
    
    // Submit the approval request for the Campaign
    
    Approval.ProcessResult result = Approval.process(app);
    
    // Verify that the results are as expected
    
    System.assert(result.isSuccess());
    
    System.assertEquals(
        'Pending', result.getInstanceStatus(),
        'Instance Status'+result.getInstanceStatus());
    }
}

 The error I get is:

 

System.DmlException: Process failed. First exception on row 0; first error: ALREADY_IN_PROCESS, Cannot submit object already in process.: []

 

From this message I understand that my Test Method is trying to submit the Campaign record to the Approval Process when it has already been submitted.

 

How can I test that when I perform the 'insert c' in the test, that the record has actually been submitted for approval?

 

Thanks,

 

Marco

Best Answer chosen by Admin (Salesforce Developers) 
Marco PinderMarco Pinder

I finally resolved this myself by checking that a ProcessInstance record is created at the same time as the Campaign record using teh following code:

 

@isTest
private class AuthSpendProcessApprovalTest{
    static testMethod void AuthSpendProcessApprovalTest(){
    
    //Set up Campaign record

    Campaign c = new Campaign(Name = 'Test', Estimated_Cost_per_head__c = 26);
    
    // Cause the trigger to execute
    
    insert c;
       
    // Verify that the results are as expected
        
    Campaign c2 = [SELECT Id, CreatedDate FROM Campaign WHERE Id = :c.Id];
    ProcessInstance pi = [SELECT TargetObjectId, CreatedDate FROM ProcessInstance WHERE TargetObjectId = :c.Id];
    System.assertEquals(c2.CreatedDate,pi.CreatedDate);
    }
}

 

The test completes with 100% test coverage. Woohoo!