0

I am trying to make an trigger handler from which I took the logic from the trigger and placed in the handler.

Before i can save my handler I get the error: Invalid loop variable type expected SObject was Relatie__c on line 11

How do I write this correctly

Trigger

 trigger Huishouden on Relatie__c (before insert, before update, before delete) {

    HuishoudenTriggerHandler handler = new HuishoudenTriggerHandler();

    if(Trigger.isInsert && Trigger.isBefore){
    handler.OnBeforeInsert(Trigger.new);
    }  
}

Handler

    public with sharing class HuishoudenTriggerHandler {

     private boolean m_isExecuting = false;

    public HuishoudenTriggerHandler(boolean isExecuting){
        m_isExecuting = isExecuting;
    }

    public void OnBeforeInsert(Object[] newObjects){
        // EXECUTE BEFORE INSERT LOGIC  
        for(Relatie__c rc : Trigger.New){

            List<Account> acc = [SELECT Relatienummer__c 
                                 FROM Account 
                                 WHERE Id = :Rc.Contactpersoon__r.accountId];

            if(rc.Relatietype__c == 'Lid'){
                List<Huishouden__c> huishList = [SELECT id 
                                                 FROM Huishouden__c 
                                                 WHERE Name= :rc.Contactpersoon__r.MailingPostalCode +  + rc.Contactpersoon__r.mailingstreet + ' '+ acc[0].Relatienummer__c];
                if(huishList.size()>0){
                    rc.Huishouden__c = huishList[0].Name ;
                    List<Relatie__c> rcList = [SELECT id, Contact_Birthdate__c 
                                               FROM Relatie__c 
                                               WHERE Huishouden__c = :huishList[0].Name  
                                               ORDER BY Contact_Birthdate__c DESC];
                    for(Relatie__c rch : rcList ){
                        for(integer x=0; x< rcList.size() ;x++){
                            if(x == 0){
                                rch.Hoofdlid_huishouden__c= true;
                            }
                            Else{
                                rch.Hoofdlid_huishouden__c= false; 
                            }
                        }
                    }
                }
                else{
                    Huishouden__c huish = new Huishouden__c(Name = rc.Contactpersoon__r.MailingPostalCode + rc.Contactpersoon__r.mailingstreet + ' '+ acc[0].Relatienummer__c);  
                    insert huish;
                    rc.Huishouden__c = huish.Name;
                }
            }
        }
    }
    public void OnAfterInsert(Object[] newObjects){
        // EXECUTE AFTER INSERT LOGIC
    }
}
3
  • You need to work on your code as there are multiple SOQL queries in the for loop. Avoid using SOQL queries inside the for loop. – RCS May 15 '18 at 8:44
  • @RCS, any suggestions? – Thomas May 15 '18 at 8:59
  • 1. For error you can directly use the newObjects instead of Trigger.New since you're already passing the Trigger.New in handler method or else directly use Trigger.new in handler method. 2. For removing the SOQL inside from for loop try to use collections like `map' to hold the value and use map inside a for loop. – RCS May 15 '18 at 9:06
5

Trigger.new is generic List while iterating you have to typecast it into List.

So your for loop will be,

 for(Relatie__c rc :(List<Relatie__c >) Trigger.New){

 }

That being said, I found few issues with your code.

  1. If you are using Trigger.new in Handler you don't need to pass it as a parameter
  2. You are doing SOQL in for loops. Thus the code is not bulkified and won't work when few hundred records of Relatie__c are inserted
  3. You are also doing an insert in for loop, again bad practice.

https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code

5
  • Since I asked about the loop, this answer doesn't work. Thanks for the other suggestions. – Thomas May 15 '18 at 8:57
  • I wrote about the loop first, let me know if thats not clear enough. – Pranay Jaiswal May 15 '18 at 8:59
  • Check the syntax which I have used.. Its (List<Relatie__C>)Trigger.new. Mybad.. copy paste issue. :( – Pranay Jaiswal May 15 '18 at 9:03
  • I am trying to understand the bulkify part, but since I need to query account how do I bulkify this? Could you point me in the right direction? – Thomas May 15 '18 at 10:42
  • Sure: Buddy lets chat here. chat.stackexchange.com/rooms/77511/… – Pranay Jaiswal May 15 '18 at 10:52
4

A slightly cleaner way to make the type specific is to be specific about the type in the handler method:

public void OnBeforeInsert(Relatie__c[] rcs) {
    // EXECUTE BEFORE INSERT LOGIC  
    for(Relatie__c rc : rcs) {
        ...
    }
}

as Trigger.new is of type Relatie__c[] within the trigger (but not outside the trigger - one of the gotchas of using handler classes).

0

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.