1

We are using a trigger framework by Kevin O'Hara and I have a basic question about it. Why when I use Trigger.New inside one of the methods I have to cast it to a List of cases in order to use it?

public class CaseTriggerHandler extends TriggerHandler {

    public CaseTriggerHandler() {
        this.setMaxLoopCount(35);
    }

    private static Boolean isMethod1 = false;

    protected override void beforeInsert(){
        if (!isMethod1) { 
            method1(Trigger.new); 
            isMethod1 = true;
        } 
    }

    private void method1() {

        for(Case c: (List<Case>)Trigger.New) {}      //<------------------

    }
}

If I write for(Case c: Trigger.New) then I get this error:

Operation: Compiling: C:\Users\Dante\proj\src\classes\CaseTriggerHandler.cls Timestamp: Sun, 27 Aug 2017 18:29:00 Result: [OPERATION FAILED]: classes/CaseTriggerHandler.cls: Invalid loop variable type expected SObject was Case (Line: 1324, Column: 9)

Can someone explain?

  • Next time please post the exact error message not one you paraphrase. It will help everyone to assist you better. I could be wrong but I do not recall an error message as you posted before as it usually appears as in the duplicate question. If that was indeed the exact error message please post a comment stating such as I would be interested in reproducing it since the answer may be different – Eric Aug 27 '17 at 15:52
  • I edited my question and you can see that I posted only the necessary part to avoid a long post. The post you provided only says that the compiler couldn't detect that it was iterating over a list, I was looking for some other explanations. – Json Aug 27 '17 at 19:48
  • Reopening as I cannot find any information on this error. This was the duplicate I marked it as: salesforce.stackexchange.com/questions/33918/… – Eric Aug 27 '17 at 20:11
  • check my answer and explanations – Santanu Boral Aug 28 '17 at 1:26
1

This is not related to the framework you are using. It is standard compiler error. Below is the list of scenarios and explanations.

Scenario 1: Trigger.New in Trigger, which works well as it is implicitly type casted to specific object, here is Case

Result: it works wells and complies

Trigger simpleTrigger on Case (after insert) {
    //works well
    for (Case a : Trigger.new) {
        // Iterate over each Case
    }
}

Scenario 2: In the Trigger handler class and in a specific method, we are receiving specific list List<Case> as an argument, and trying to loop through the specific object.

Result: it works wells and complies

public class CaseTriggerHandler
{   
    public void onAfterInsert(List<Case> oldList, List<Case> newList)
    {
        //this works fine
        for(Case a:newList)
        {

        }
    }   
}

Scenario 3: In a method, directly accessing Trigger.new which actually returns list of SObject. So, following code will works well.

Result: it works wells and complies

public class CaseTriggerHandler
{
    public void onAfterInsert()
    {
        //works well
        for(SObject a:Trigger.new)
        {
            if(a instanceOf Case)
            {
                Case caseObj = (Case) a;
            }
        }
    }
}

Scenario 4: Type casting Trigger.new to list of Case

Result: it works wells and complies

public class CaseTriggerHandler
{
    public void onAfterInsert()
    {
        //works well
        for(Case a: (List<Case>)Trigger.new)
        {

        }
    }
}

Scenario 5: Directly accessing Trigger.new without type casting to specific object.

Result: Compile Error: Invalid loop variable type expected SObject was Case

public class CaseTriggerHandler
{
    public void onAfterInsert()
    {
        //will not compile
        for(Case a: Trigger.new)
        {

        }
    }
}
  • So the answer is the same as the linked duplicate they just changed the error message from what we were used to – Eric Aug 28 '17 at 2:28
  • Reason is same, only I have explained in more detailed way – Santanu Boral Aug 28 '17 at 3:11
  • Right. Just stating that they changed the error message from what we were used to – Eric Aug 28 '17 at 3:18
  • Thank you Santanu for the break down! though it still doesn't explain why it's happening. – Json Aug 28 '17 at 9:18
  • Basically Trigger.new returns List<SObject>, you need to type cast it to specific list like List<Case>, refer scenario 3, hope it makes sense! – Santanu Boral Aug 29 '17 at 5:53

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.