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
skiptomylou11skiptomylou11 

Deployment Error trigger test class

Dear all,

 

I have created a trigger in my Sandbox which automatically submits an Opportunity to an approval process once the Stage is changed to either 'Won' or 'Lost'. I have attached both code snippets below.

It is now working fine in my sandbox but when I try to deploy it to the production Org I receive the following error on the test class referring to the last line of code of the test class.

 

Many thanks for any support!

 

TestOpportunitySubmitForOrderApproval.testApprovalSuccess()Class211Failure Message: "System.AssertException: Assertion Failed: Expected: 2, Actual: 1", Failure Stack Trace: "Class.TestOpportunitySubmitForOrderApproval.testApprovalSuccess: line 21, column 1"

 

trigger OpportunitySubmitForOrderListApproval on Opportunity (after update) {
 
    for(Integer i = 0; i < Trigger.new.size(); i++) 
    
     {
        if ((Trigger.old[i].StageName <> '6 - Project won'  ||  Trigger.old[i].StageName <> '7 - Project lost')  && (Trigger.new[i].StageName == '6 - Project won'  ||  Trigger.new[i].StageName == '7 - Project lost')) 
        {
 
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted for approval. Please approve.');
            req.setObjectId(Trigger.new[i].Id);
            Approval.ProcessResult result = Approval.process(req);
            System.assert(result.isSuccess());  
 
        }
       
    }
 }

 

 

@isTest
private class TestOpportunitySubmitForOrderApproval {
 
    static testMethod void testApprovalSuccess() {
 
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opp';
        opp.Amount = 100;
        opp.CloseDate = Date.today();
        opp.StageName = '0 - New Opportunity';
        opp.Division__c= 'SYS';
        // insert the new opp
        insert opp;
        // change the probability of the opp so the trigger submits it for approval
    opp.StageName = '6 - Project won';
    // update the opp which should submit it for approval
    update opp;
 
        // ensure that the opp was submitted for approval
        List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :opp.id];
    System.assertEquals(processInstances.size(),1);
 
    }
 }

 

neophyteneophyte

This means the query you are doing :

 

List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :opp.id];

 

is returning 2 records. Could be that the record is processed twice in the processInstances. Check if you have any other approval process that could create the record in processInstance or if your approval process is getting triggered twice.