2

(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)
  • 1
    you mention your mistake in your question :-) your method expects 2 parameters but you only pass 1 – Samuel De Rycke Mar 19 '13 at 18:11
  • Yes, I agree.......... – AMM Mar 19 '13 at 18:12
  • Working with possible solution... when passing Trigger.newMap in the Helper, and REMOVING Static from the PUBLIC, it's compiling........... – AMM Mar 19 '13 at 18:16
  • AMM i know that you're trying to familiarize yourself with programming which isn't always easy. The apex developer guide may seem a lot to read, but specially this section should bring some clarity related to this question: salesforce.com/us/developer/docs/apexcode/Content/… Try to not only get it to work, but fully understand why it is working with the changes you make in the code. – Samuel De Rycke Mar 19 '13 at 21:46
2

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.

  • This is gelling with what I just discovered and am testing, thanks a lot. Null on insert for oldMap makes sense too, but thanks for pointing the fact out so it's in my head for the future. – AMM Mar 19 '13 at 18:24
0

Removing STATIC from the Public call (and putting Trigger.oldMap back into the Helper call) seems to fix it. Fully testing to be sure.

  • Removing static from the method declaration would solve the problem if you absolutely need to call it the way you're doing in the trigger...but I would argue that it's unnecessary to create an instance of your helper class in this case. Are there any member variables that need to persist between method calls that aren't posted in your sample code? – JCD Mar 19 '13 at 18:24
  • Nope. None..... – AMM Mar 19 '13 at 18:37

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.