2

I'm trying to build a process which looks up a matching account for a lead, given a cleaned email or website.

The issue I'm facing is that this could be run against 1000 or 10k leads at once and the query within the loop is going to cause some issues.

What can I do to bulkify this so that I don't hit REQUEST_LIMIT_EXCEEDED errors?

HelperClass

public class LeadTriggerHelper {

    Public static void UpdateLead(List<Lead> Leads){
        ldupdate = new List<Lead>();

        for(Lead ld1 : Leads){
            String accountName;

            if(ld1.cleaned_email__c != null && ld1.cleaned_email__c != ''){
                accountName = ld1.cleaned_email__c;
            }//if

            else if(ld1.cleaned_website__c != null && ld1.cleaned_website__c != ''){
                accountName = ld1.cleaned_website__c;                
            }//else

            List<List<Account>> acc2 = [FIND :accountName IN NAME FIELDS RETURNING Account LIMIT 1];

            if(acc2.size()>0){
                ld1.Account_Lookup__c = acc2[0][0].Id;
            }//if
        }//for   
    }//UpdateLead
}//LeadTriggerHelper

Trigger

trigger LeadTrigger on Lead (before insert, before update) {
    if(trigger.isBefore){
        If(trigger.isUpdate){
            for (Lead lead : Trigger.new){
                Lead oldLead = Trigger.oldMap.get(lead.Id);
                if (oldLead.Trigger_Account_Update__c != lead.Trigger_Account_Update__c){
                    LeadTriggerHelper.UpdateLead(trigger.new);                        
                }//if
            }//for
        }//isUpdate
    }//isBefore
}//Trigger
4

To bulkify the method, SOSL should be outside of for loop.

Correct syntax for joining field is (refer documentation):

FIND {"Joe Smith" OR "Joe Smythe"}

IN Name Fields

RETURNING lead(name, phone), contact(name, phone)

So,

  • create List<String> lstName = new List<String>();
  • add values to lstName
  • use String str = String.join(lstName, ' OR ');

And, finally use the list into SOSL as

FIND :str IN NAME FIELDS RETURNING Account LIMIT 1

So possible changes will be as follows

public class LeadTriggerHelper {

    Public static void UpdateLead(List<Lead> Leads){
        ldupdate = new List<Lead>();

        List<String> lstName = new List<String>();
        for(Lead ld1 : Leads){
            String accountName;
            if(ld1.cleaned_email__c != null && ld1.cleaned_email__c != ''){
                accountName = ld1.cleaned_email__c;
                lstName.add(accountName);
            }//if
            else if(ld1.cleaned_website__c != null && ld1.cleaned_website__c != ''){
                accountName = ld1.cleaned_website__c; 
                lstName.add(accountName);
            }//else
        }//for 

        String str = String.join(lstName, ' OR ');

        List<List<Account>> acc2 = [FIND :str IN NAME FIELDS RETURNING Account LIMIT 1];

            if(acc2.size()>0){
                // perform your desired operation.
            }//if

    }//UpdateLead
}//LeadTriggerHelper
  • Your code only updates ld1, I may need to update 10,000 leads. – shartshooter May 10 '17 at 17:04
  • 4
    I haven't done any changes to updates. he has to write this logic – Santanu Boral May 10 '17 at 17:07
  • @kmomo, if my answer solves your purpose then accept this, so that others may find this useful – Santanu Boral May 11 '17 at 15:05
  • This is great thanks! Is there any limit on the number of "OR" joining fields that can be used...? – Aislinn Hayes Jul 18 '17 at 16:12
  • I think no limit, I think Salesforce standard query character limit is imposed here – Santanu Boral Jul 18 '17 at 16:14

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.