1

I have written a method which I will call in a Trigger.While trying to save this code as Apex class, I am getting the error " Error: Compile Error: Loop variable must be of type SObject at line 7 column 13"

My code goes as below ,appreciate any help, thanks

public class LeadBefore {
    public static void UpdateChapterEmailAddress (){

        set<Id> JDRFLocationId = new set<Id>();
        List<Lead> vLstLead = new List<Lead>();

         for(Lead vLead:trigger.new)
        {
            if(vLead.JDRF_Location__c != null)
            {
                JDRFLocationId.add(vLead.JDRF_Location__c);
                vLstLead.add(vLead);
            }
        }
        Map<Id, JDRF_Location__c> vMapIdJDRFLocat = new Map<Id, JDRF_Location__c>([Select Id, Name, General_Email_Address__c from JDRF_Location__c where Id In: JDRFLocationId]);
        for(Lead vLead: vLstLead)
        {
            if(vMapIdJDRFLocat.containsKey(vLead.JDRF_Location__c))
            {
                vLead.Chapter_Email_Address__c = vMapIdJDRFLocat.get(vLead.JDRF_Location__c).General_Email_Address__c;
            }
        }
    }
}
  • 1
    you don't have a trigger.new list in an apex class. – highfive Apr 29 '14 at 5:02
  • 1
    All of the trigger variables are global and they are available in this way, exactly as written, without being passed as a parameter to the method as long as this code is called from a trigger. – Mark Pond Apr 29 '14 at 6:28
  • @Shalini, do you have an Apex class in your org which has the name Lead in addition to the standard Lead sObject? – Mark Pond Apr 29 '14 at 6:33
  • No Mark, no other Apex classes with the name Lead. It was happening cos I had Trigger.new in my Apex class, as pointed out by Baskaran S. I changed my code and its working fine now. Thanks ! – Shalini Apr 29 '14 at 6:40
4

The actual reason it failed compilation is because the compiler couldn't detect that it was iterating over a list of sObjects. The same code, with an explicit cast to the proper type, works fine.

That said, if the static method accepts a list or a map as a parameter it makes this code a bit easier to write unit tests against. Passing the trigger collections to the method is not a mandatory practice.

public class LeadBefore {
    public static void UpdateChapterEmailAddress (){

        set<Id> JDRFLocationId = new set<Id>();
        List<Lead> vLstLead = new List<Lead>();

         for(Lead vLead : (List<Lead>)trigger.new)
        {
            if(vLead.JDRF_Location__c != null)
            {
                JDRFLocationId.add(vLead.JDRF_Location__c);
                vLstLead.add(vLead);
            }
        }
|improve this answer|||||
  • I am trying the above code but its throwing an exception – Shalini Apr 29 '14 at 8:26
  • I am trying the above code but its throwing an exception – Shalini Apr 29 '14 at 8:27
  • I am trying the above code but its throwing an exception Compile Error: unexpected token: '(' at line 5 column 40public class LeadBefore { public static void UpdateChapterEmailAddress (){ set<Id> JDRFLocationId = new set<Id>(); List <Lead> vLstLead = newlist <Lead>(); for(Lead vLead:(List<Lead>)trigger.new) { if(vLead.JDRF_Location__c != null) { JDRFLocationId.add(vLead.JDRF_Location__c); vLstLead.add(vLead); } } – Shalini Apr 29 '14 at 8:27
  • You are missing a space between the word new and the word list on line 5. List <Lead> vLstLead = newlist <Lead>(); – Mark Pond Apr 29 '14 at 16:29
0

Its throwing this error because trigger.new is not available in your class Lead Before. trigger.new is only available in context of trigger. If you want to use it in LeadBefore->UpdateChapterEmailAddress() then you need to pass it as a parameter from your trigger.

|improve this answer|||||
  • I realize this is an old answer, but it's actually incorrect. Trigger.new is still available in classes, but compiles as a generic SObject[] list instead of a concrete SObject type. Simply casting to the appropriate type resolves the compilation error. – sfdcfox Aug 27 '17 at 16:45
0

since you took the logic out of trigger and used trigger.new in a class separately will not work. You need to pass the trigger.new as a parameter to the method inside of this class and assign to a list.

    public static void UpdateChapterEmailAddress (List<Lead> l){
        List<Lead> lstLead = l;
for(Lead vLead:lstLead)
        {
            if(vLead.JDRF_Location__c != null)
            {
                JDRFLocationId.add(vLead.JDRF_Location__c);

In your trigger,

LeadBefore .UpdateChapterEmailAddress(trigger.new);
|improve this answer|||||
  • you are few seconds faster that me :) – AtulRajguru9 Apr 29 '14 at 5:29
  • Good to hear it helped you @Shalini – Bforce Apr 29 '14 at 11:10
  • I realize this is an old answer, but it's actually incorrect. Trigger.new is still available in classes, but compiles as a generic SObject[] list instead of a concrete SObject type. Simply casting to the appropriate type resolves the compilation error. – sfdcfox Aug 27 '17 at 16:45

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.