How to test a trigger with an approval process

apex-codesalesforcetriggers

I have a trigger which initiates an approval process when certain criteria are met:

trigger AddendumAfterIHMS on Addendum__c (after update) {

  for (integer i = 0; i<Trigger.new.size(); i++){

    if(Trigger.new[i].RecordTypeId != '012V0000000CkQA'){

        if(Trigger.new[i].From_IHMS__c != null && Trigger.old[i].From_IHMS__c == null){

            ID addendumId = Trigger.new[i].Id;

            // Start next approval process
            Approval.ProcessSubmitRequest request = new Approval.ProcessSubmitRequest();
            request.setObjectId(addendumId);
            Approval.ProcessResult requestResult = Approval.process(request);

        }
    }
  }
}

It works perfectly, but now i need to create a test class for it. I have created a class which brings the code up to 75% coverage, which is the minimum, but I'm picky and like to have 100% coverage on my code. The test class I have now gets stuck on the line request.setObjectId(addendumId); and doesn't move past it. The error I receive is:

System.DmlException: Update failed. First exception on row 0 with id a0CV0000000B8cgMAC; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AddendumAfterIHMS: execution of AfterUpdate

Here is the test class that I have written so far, most of the class actually tests some other triggers, but the important line which is throwing the error is the very last line update addendumTierFeature;

@isTest
private class AddendumTest {

static testMethod void myUnitTest() {

    // Query Testing Account, will need ID changed before testing to place into production
    Account existingAccount = [SELECT Id FROM Account LIMIT 1];
    Model__c existingModel = [SELECT Id FROM Model__c WHERE Active__c = TRUE LIMIT 1];
    Pricebook2 existingPricebook = [SELECT Id,Name FROM Pricebook2 WHERE IsActive = TRUE LIMIT 1];
    List<Contact> existingContacts = [SELECT Id,Name FROM Contact LIMIT 2];

    Contact existingContactPrimary = existingContacts[0];
    Contact existingContactSecondary = existingContacts[1];

    Opportunity newOpportunity = new Opportunity(

        Name = 'New Opportunity',
        Account = existingAccount,
        CloseDate = Date.today(),
        Order_Proposed__c = Date.today(),
        StageName = 'Branch Visit - Not Responding',
        Opportunity_Follow_Up__c = 'Every 120 Days',
        LeadSource = 'Farm Lists',
        Source_Detail__c = 'FSBO',
        Model_Name__c = existingModel.Id,
        Processing_Fee__c = 100.50,
        Site_State__c = 'OR',
        base_Build_Zone__c = 'OR',
        Pricebook_from_Lead__c = existingPricebook.Name

    );

    insert newOpportunity;
    //system.assert(newOpportunity.Id != null);

    ID newOppId = newOpportunity.Id;

    OpportunityContactRole contactPrimary = new OpportunityContactRole(
        Role = 'Primary',
        IsPrimary = true,
        OpportunityId = newOppId,
        ContactId = existingContactPrimary.Id
    );

    OpportunityContactRole contactSecondary = new OpportunityContactRole(
        Role = 'Primary',
        IsPrimary = false,
        OpportunityId = newOppId,
        ContactId = existingContactPrimary.Id
    );

    insert contactPrimary;
    insert contactSecondary;

    newOpportunity.Name = 'Different - Updating';
    newOpportunity.Order_Accepted__c = Datetime.now();

    update newOpportunity;

    Addendum__c addendumCustomOption = new Addendum__c(
        RecordTypeId = '012V0000000CkQA', //Pre Priced Custom Option
        Opportunity__c = newOppId,
        Item_Pre_Priced_Description__c = 'a1eV00000004DNu',
        Reason__c = 'This is a reason',
        Item__c = 'This is an Item',
        Quantity__c = 1
    );

    Addendum__c addendumTierFeature = new Addendum__c(
        RecordTypeId = '012V0000000Cjks', //Tier Feature
        Opportunity__c = newOppId,
        Category__c = 'Countertops',
        Reason__c = 'This is a reason',
        Item__c = 'This is an Item',
        Quantity__c = 1
    );

    insert addendumCustomOption;
    insert addendumTierFeature;

    addendumCustomOption.Quantity__c = 2;
    addendumTierFeature.Quantity__c = 2;

    update addendumCustomOption;
    update addendumTierFeature;
    update newOpportunity;

    addendumTierFeature.To_IHMS__c = system.now();

    update addendumTierFeature;

    addendumTierFeature.From_IHMS__c = system.now();


    update addendumTierFeature;

  }
}

Any help on this matter would be greatly appreciated. I believe the problem is in the way I am testing the approval process start. Is there by chance a special testing function for this?

Best Answer

After fiddling around for a little while I discovered that the error was actually tied into my approval process. I kept digging into the error logs until I got to the error: caused by: System.DmlException: Process failed. First exception on row 0; first error: MANAGER_NOT_DEFINED, Manager undefined.: []. This phrase indicates that there is no one defined for the next step in my approval process.

When I created the opportunity, I did not set the owner and somehow this created an opportunity which had an owner without a manager. The addendum was also created without an owner/manager. So when I tried to launch the next approval process, there was no manager to send the approval to and an error was thrown.

Related Topic