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
Basil ChoudhryBasil Choudhry 

Apex Approval Trigger Test

I'm trying to get an opportunity to be sent to be approved when a checkbox is ticked (the checkbox will be ticked by various workflows).

This is the code I've cobbled together from various sources:

trigger OpportunitySubmitForApproval1 on Opportunity (after update) {

for (Opportunity a : trigger.new) {
if (a.Send_for_Approval__c == TRUE) {
    Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
    ID addendumId = a.Id;

      Approval.ProcessSubmitRequest request = new Approval.ProcessSubmitRequest();
            request.setObjectId(addendumId);
            Approval.ProcessResult requestResult = Approval.process(request);
  }
}
}


This is working fine for me in Sandbox. However, I'm having trouble making a test class to bring it over to production. The test class I've cobbled together is 
@isTest
public class OpportunitySubmitForApproval1Test {
    static testMethod void testApprovalSuccess() {

        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opp';
        opp.Amount = 100;
        opp.CloseDate = Date.today();
        opp.Probability = 10;
        opp.StageName = 'Prospecting';
        opp.Send_for_Approval__c = FALSE;
        // insert the new opp
        insert opp;
        // change the probability of the opp so the trigger submits it for approval
    opp.Probability = 40;
        opp.Send_for_Approval__c = TRUE;
    // 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);

    }


}
logontokartiklogontokartik
It might be issue with your actual approval in production. Can you check if the all the approvers are assigned properly in the Production and its similar to the one in Sandbox.? 

Also, when the test class fails in Production can you check your debug logs to verify where exactly it failed? 

Hope this helps.
Basil ChoudhryBasil Choudhry
Hi, I'm having trouble actually getting the trigger to deploy, I get the error about not having any code coverage (0%) Basil
logontokartiklogontokartik
Arent you deploying both your trigger and test class at the same time? Please try that and verify your debug logs to see if there are any failures. Actually if test class fails you will receive error as well.
Basil ChoudhryBasil Choudhry
Hi, I am deploying both at the same time, the test runs fine, but the trigger fails saying it has 0% coverage