[SalesForce] Apex – Calling Class Fails After Altering PUBLIC statement in Class

(thanks to @JCD for helping me clean up my Class for Trigger.old vs .new checking…)

My call to the class/function below now fails. I presume that now that my PUBLIC statement is looking for two parameters, that's my failing…? I would appreciate some insight. I tried simply adding (comma)Trigger.old and (comma)Trigger.oldMap to the helper call.

TRIGGER

trigger AccountTriggerMakeCase1 on Account (after insert, after update) {

    // Create an instance of your helper class
    AccountTriggerHelper1 helper = new AccountTriggerHelper1();

    if( (Trigger.isAfter && Trigger.isInsert) || 
        (Trigger.isAfter && Trigger.isUpdate) ) {

        // Now call your helper method
        helper.createCaseWhenNeeded(Trigger.new);
    }
}

CLASS

public class AccountTriggerHelper1 {

   public static void createCaseWhenNeeded(List<Account> accounts, Map<Id, Account> oldMap){

       // We need to store a List of Cases to create.
        List<Case> casesToCreate = new List<Case>();

       // Loop over the accounts.
        for(Account acc:accounts){

          // Condition for creating Case.
           Account beforeUpdate = oldMap.get(acc.Id);
           if( beforeUpdate.LastName != 'CreateCase' && acc.LastName == 'CreateCase' ) {
               // Create case and populate.
                Case caseToAdd = new Case();
                caseToAdd.AccountId = acc.Id;
                caseToAdd.RecordTypeId = '012600000005DYN';
                caseToAdd.Origin = 'Receptionist';
                caseToAdd.Products__c = 'Other';
                casesToCreate.add(caseToAdd);
            }

        }
       // Commit all cases created.
        insert casesToCreate;

   }
}

ERROR ON TRIGGER

Method does not exist or incorrect signature:
[AccountTriggerHelper1].createCaseWhenNeeded(LIST<Account>) 
at line 10 column 9       (LAST LINE OF THE TRIGGER)

Best Answer

Check out the method declaration:

public static void createCaseWhenNeeded(List<Account> accounts, Map<Id, Account> oldMap)

First, this is static, meaning you don't need to create an instance of the AccountTriggerHelper1 class (or you could make it non-static, although in this case there really isn't a reason to).

Second, the declaration is looking for a Map<Id, Account> as the second parameter. In your example this is the type of Trigger.oldMap, which is what you want to pass in there. So your trigger ends up looking like:

trigger AccountTriggerMakeCase1 on Account (after insert, after update) {
    if( (Trigger.isAfter && Trigger.isInsert) || 
        (Trigger.isAfter && Trigger.isUpdate) ) {
        AccountTriggerHelper1.createCaseWhenNeeded(Trigger.new, Trigger.oldMap);
    }
}

Keep in mind though, that original record values aren't going to be available after an insert (only in update triggers). So in that case, Trigger.oldMap would be null.

Related Topic