1

I am trying to set up a test class to test a Visualforce page button which, when clicked, submits a custom object record to an approval process using a controller method. When I try the test below, I get an error:

System.DmlException: Process failed. First exception on row 0; first error: NO_APPLICABLE_PROCESS, No applicable approval process was found.:

My only entry criteria is that the Region on the Opportunity starts with "NA", which is does, adn the approval process is active. Does anyone have any idea why I get this error?

Class for Creating Account and Opp Records:

public class TestCreateRecords {

    // create and insert a new Account record.
        public static Account createAcct(Integer i){ 
        Account acct = new Account();
            acct.Name = 'Test' + i;
            acct.Language__c = 'English';
            acct.Industry = 'Automotive';
            acct.CurrencyIsoCode = 'USD';
            acct.Region__c = 'NA-US-NE';
            acct.BillingCity = 'New York';
            acct.BillingState = 'New York';
            acct.BillingCountry = 'United States';
        return acct;
        }

        public static Opportunity createOppNew (Id acctId){ 
            Opportunity opp2 = new Opportunity();
                opp2.AccountId = acctId;
                opp2.Name = 'Test Opportunity - ';
                opp2.StageName = System.Label.Eng_OppStage3;
                opp2.CloseDate = date.newinstance(2020,1,31);
                opp2.Amount = 1000;
                opp2.Region__c = 'NA-US-NE';
            return opp2;
        }
    }

Test Class:

    @Istest (SeeAllData=true)
private class TestControllerDealSumm
{
        Account acct3 = TestCreateRecords.createAcct(0);
        insert acct3;

        Opportunity opp3 = TestCreateRecords.createOppNew(acct3.Id);
        insert opp3;

        Summary__c DS3 = new Summary__c();
            DS3.Opportunity__c = opp3.Id;
            DS3.Agreement_Effective_Date__c = date.newinstance(2025,1,31);
            DS3.Net_Payment_Term_Days__c = 60;
            DS3.Auto_Renewal__c = TRUE;
        Insert DS3;

        ApexPages.StandardController Summ1a = new ApexPages.standardController(opp3);
        VF_SummaryController SummCont1a = new VF_SummaryController(Summ1a);
        SummCont1a.DS.add(DS3);
        SummCont1a.Summary();
        SummCont1a.save();


        ApexPages.StandardController Summ3 = new ApexPages.standardController(DS3);
        DSReviewController SummCont3 = new DSReviewController(Summ3);
        SummCont3.myDS = DS3;
        SummCont3.approve();

        // Create an approval request for the account
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
            req1.setComments('Submitting request for approval.');
            req1.setObjectId(acct3.Id);

        // Submit the approval request for the account
        Approval.ProcessResult result = Approval.Process(req1);

        // Verify the result
        System.assert(result.isSuccess());

        System.assertEquals(
            'Pending', result.getInstanceStatus(), 
            'Instance Status'+result.getInstanceStatus());

    }

Controller:

public class DSReviewController {

    public Summary__c myDS;

    public DSReviewController(ApexPages.StandardController stdController){
        this.myDS = (Summary__c)stdController.getRecord();
    }

    public pageReference approve() {

        Approval.ProcessSubmitRequest approve1 = new Approval.ProcessSubmitRequest();
        approve1.setComments('Submitting request for approval.');
        approve1.setObjectId(myDS.id);

        approve1.setProcessDefinitionNameOrId(null);
        approve1.setSkipEntryCriteria(true);

        Approval.ProcessResult result = Approval.process(approve1);

        PageReference RetPage = ApexPages.currentPage();
        RetPage.setRedirect(true);
        return RetPage; 
    }
}
  • 1
    (1) the debug output will show the values for the entering object to each approval process (2) seeAlldata=true shouldn't be needed here – cropredy Feb 2 '16 at 2:01
  • @crop1645 -- Thanks. 1) I look at the debug log and on 1 line it says it finds an approval process, inputs all the data, and then throws the error. I'm not sure what to look for? 2) I need the seeAlldata = true because there is a custom setting in my instance that throws an error unless I have that line of code. I couldn't figure out another way of covering the custom setting. – J. Neilan Feb 2 '16 at 15:12
  • mock the custom setting just like any other sobject. I tend to do this in a Util.mockEnv() method that every testmethod calls – cropredy Feb 2 '16 at 18:39
2

For anyone interested, I figured out my issue. My Test Class was looking at the Account ID instead of the ID of my newly created custom object (line 32). I changed that line to req1.setObjectId(DS3.Id); and then got an error that the process was already in progress. This mad me realize that I didn't need the approval process creation from lines 29 - 42, since the process was being triggered by my "approve" controller on lines 17 - 21. So I removed lines 29 - 42 and now get 100% coverage. Thanks for the help everyone. Updated Test class is below:

@Istest (SeeAllData=true)
private class TestControllerDealSumm
{
        Account acct3 = TestCreateRecords.createAcct(0);
        insert acct3;

        Opportunity opp3 = TestCreateRecords.createOppNew(acct3.Id);
        insert opp3;

        Summary__c DS3 = new Summary__c();
            DS3.Opportunity__c = opp3.Id;
            DS3.Agreement_Effective_Date__c = date.newinstance(2025,1,31);
            DS3.Net_Payment_Term_Days__c = 60;
            DS3.Auto_Renewal__c = TRUE;
        Insert DS3;

        ApexPages.StandardController Summ1a = new ApexPages.standardController(opp3);
        VF_SummaryController SummCont1a = new VF_SummaryController(Summ1a);
        SummCont1a.DS.add(DS3);
        SummCont1a.Summary();
        SummCont1a.save();


        ApexPages.StandardController Summ3 = new ApexPages.standardController(DS3);
        DSReviewController SummCont3 = new DSReviewController(Summ3);
        SummCont3.myDS = DS3;
        SummCont3.approve();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.