0

(thanks to @JesseAltman for help with the initial code)

I am trying to make sure this fires once each time the IF comes true (checking if the field was previously something different). I am doing something wrong at either the Map line or the line directly above. Error =

Illegal assignment from LIST<SObject> to SOBJECT:Account at line 14 column 12

...which points to the line ABOVE Map. I pulled these two lines from another site, but they were corrupted (Map.get was right next to trigger.old with no space between). Help?? :-(

public class AccountTriggerHelper{

   public void createCaseWhenNeeded(List<Account> accounts){

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

       // Loop over the accounts. Remember, we don't know how many accounts we will have
       for(Account acc:accounts){

           // This is where you need to determine what your condition will be.
           // You will replicate this system with more if statements or else if
           // statements
LINE 14:   Account beforeUpdate = System.Trigger.old;
           Map.get(acc.Id);
           if( beforeUpdate.LastName != 'CreateCase' && acc.LastName = 'CreateCase' ) {

       // Your account meets the criteria, create the case you want and
       // add it to your List of cases
               Case caseToAdd = new Case();
       // Set up any fields you want
               caseToAdd.AccountId = acc.Id;
               caseToAdd.RecordTypeId = '012600000005DYN';
               caseToAdd.Origin = 'Receptionist';
               caseToAdd.Products__c = 'Other';
               casesToCreate.add(caseToAdd);

           }

       // You have your full List of cases to add, now just run the insert DML statement
       insert casesToCreate;
       }
   }
}

UPDATE:

Error if I use THIS instead of the line 14-15 above -

Illegal assignment from SObject to SOBJECT:Account at line 14 column 12 

(when using...)

Account beforeUpdate = System.Trigger.oldMap.get(acc.Id); 
2

Actually, the code you pulled wasn't corrupted - it was using Trigger.oldMap (a Map of Ids -> SObjects) instead of Trigger.old (a list of SObjects).

Try combining lines 14 and 15 to make:

Account beforeUpdate = System.Trigger.oldMap.get(acc.Id);

Update: Are you even sure that the Trigger context variables are valid when this method is called, since you're not passing them in with the method call? To use Trigger.oldMap this code would need to be in the trigger - when it is, the trigger understands that Trigger.old is a list of whatever object the trigger is on (in this case, the Account object). Outside of a trigger's context, though, the Apex parser would give you the error you're seeing.

Try passing in the oldMap in with the method call:

public class AccountTriggerHelper {
   public static void createCaseWhenNeeded(List<Account> accounts, Map<Id, Account> oldMap){
        List<Case> casesToCreate = new List<Case>();
        for(Account acc:accounts){
            Account beforeUpdate = oldMap.get(acc.Id);
            if( beforeUpdate.LastName != 'CreateCase' && acc.LastName == 'CreateCase' ) {
                Case caseToAdd = new Case();
                caseToAdd.AccountId = acc.Id;
                caseToAdd.RecordTypeId = '012600000005DYN';
                caseToAdd.Origin = 'Receptionist';
                caseToAdd.Products__c = 'Other';
                casesToCreate.add(caseToAdd);
            }
        }
        insert casesToCreate;
   }
}

This would allow you to just call the following in your Account trigger:

AccountTriggerHelper.createCaseWhenNeeded(Trigger.new, Trigger.oldMap);
10
  • Tried that... UPDATE added to original post at bottom.
    – AMM
    Mar 19 '13 at 16:01
  • @AMM I have updated my answer. The code you found was most likely intended for use within the trigger itself, so you'll need to pass Trigger.oldMap in with the method call to your helper class in order to reference it.
    – JCD
    Mar 19 '13 at 16:10
  • The updated PUBLIC statement did not work; same exact error. :-/ Sorry.
    – AMM
    Mar 19 '13 at 16:14
  • Illegal assignment from SObject to SOBJECT:Account at line 15 column 12
    – AMM
    Mar 19 '13 at 16:15
  • Odd. I pasted your WHOLE code just in case you changed something I'm not noticing, and I'm getting a dumb error saying I can't use Ampersand in the IF because it's not boolean). Digging....... DO I NEED to do something for the oldmap in the TRIGGER that calls the function?
    – AMM
    Mar 19 '13 at 16:17

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.