0

I had to populate other objects data with an object ELE Agreements. I am not sure my code is best practice or not because I am using two Objects queries.

I am afraid of any SOQL Limitation error while insert bulk records.

Can someone please suggest me on this?

 public static void populateContactFields(List<ELE_Agreement__c> ee)
{
    for(ElE_Agreement__c e:ee){

        try
        {
         contact c=[select id,Business_Advisor__c,Business_Advisor_Email__c from contact where email=:e.Email__c Limit 1];
         for(WCT_Leave__c LOA : [select id, WCT_Employee__c, WCT_Leave_Start_Date__c,WCT_Leave_End_Date__c  from WCT_Leave__c where WCT_Deloitte_Email__c =:e.Email__c order by LastModifiedDate desc LIMIT 1]){
           if(LOA.WCT_Leave_End_Date__c!= null && LOA.WCT_Leave_Start_Date__c!= null){

               e.Contact__c=LOA.WCT_Employee__c;
               e.Business_Advisor__c= c.Business_Advisor__c;
               e.Business_Advisor_Email__c= c.Business_Advisor_Email__c;
               e.LOA_End_Date__c =  LOA.WCT_Leave_End_Date__c;
               e.LOA_Start_Date__c = LOA.WCT_Leave_Start_Date__c;
      }

       e.Contact__c=LOA.WCT_Employee__c;
        }  
      }
        catch(Exception ex)
        {

        }
    }
  }   
6
  • Not a good practice to query inside a for loop.
    – RCS
    Apr 5, 2018 at 10:44
  • Can you please guide me to avoid those queries in for loop. I mean how to write those queries outside of those loops?
    – Frank
    Apr 5, 2018 at 10:48
  • What's the logic behind having the contact object limit 1 with Agreement's email
    – RCS
    Apr 5, 2018 at 10:50
  • might have duplicate contacts.
    – Frank
    Apr 5, 2018 at 10:52
  • You can add that contact piece of code outside of for loop as you're limiting it to 1 so instead you can add that outside the for loop with list's zero index email in the where clause.
    – RCS
    Apr 5, 2018 at 11:00

1 Answer 1

1

Please check the below code and try to test as per your business logic

To make the trigger code bulkify use collections.

Create emailSet with all ELE_Agreement__c associated email for which we need to create a Map<String, WCT_Leave__c> so you don't need to iterate inside the for loop

public static void populateContactFields(List<ELE_Agreement__c> ee)
{
    contact c;
    Set<String> emailSet = new Set<String>();
    Map<String, WCT_Leave__c> wctMap = new  Map<String, WCT_Leave__c>();
    Map<String, WCT_Leave__c> cntMap = new  Map<String, WCT_Leave__c>();
    try
    {
        for(ElE_Agreement__c e : ee){
            emailSet.add(e.Email__c);
        }

        for(WCT_Leave__c LOA : [select id, WCT_Employee__c, WCT_Leave_Start_Date__c,WCT_Leave_End_Date__c  from WCT_Leave__c where WCT_Deloitte_Email__c IN: emailSet AND WCT_Leave_End_Date__c != null AND LOA.WCT_Leave_Start_Date__c != null order by LastModifiedDate desc]){
            if(!wctMap.containsKey(String.valueOf(LOA.WCT_Deloitte_Email__c)))
                wctMap.put(String.valueOf(LOA.WCT_Deloitte_Email__c), LOA);
        }

        for(Contact cntObj: [select id,Business_Advisor__c,Business_Advisor_Email__c from contact where email IN: emailSet]) {
            if(!cntMap.containsKey(String.valueOf(cntObj.email)))
                cntMap.put(String.valueOf(cntObj.email), cntObj);
        }

        for(ElE_Agreement__c e : ee){           
           if(wctMap.containsKey(String.valueOf(e.Email__c)) && cntMap.containsKey(String.valueOf(e.Email__c))){
               WCT_Leave__c LOA = wctMap.get(String.valueOf(e.Email__c));
               Contact cnt = cntMap.get(String.valueOf(e.Email__c));
               e.Contact__c=LOA.WCT_Employee__c;
               e.Business_Advisor__c= cnt.Business_Advisor__c;
               e.Business_Advisor_Email__c= cnt.Business_Advisor_Email__c;
               e.LOA_End_Date__c =  LOA.WCT_Leave_End_Date__c;
               e.LOA_Start_Date__c = LOA.WCT_Leave_Start_Date__c;

               e.Contact__c=LOA.WCT_Employee__c;
           }
        }

    }catch(Exception ex){
        system.debug('Error : '+ex);
    }
}   
3
  • 3
    Some explanation is useful with code answers, so it can benefit users other than the original poster. It's also a really bad practice to catch and swallow all exceptions.
    – David Reed
    Apr 5, 2018 at 11:34
  • @DavidReed tried to explain the code as per the code snippet given in the questions. Yes and i agree we can catch specific exceptions i.e. DMLException and so on.
    – RCS
    Apr 5, 2018 at 11:42
  • Great!! RCS. It's working as expected. Thank you very much to make aware of Map usage.
    – Frank
    Apr 5, 2018 at 11:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .