function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Joe LavinJoe Lavin 

Auto assigning leads to accounts based on two criteria

Hello, hoping you guys can help me with this. I've written an Apex trigger to auto-assign leads to accounts using a custom lookup relationship field. There are two "domain" fields on account which if a match is found with either of them, the lead should be auto assigned to account. But only if the lead is not already assigned to an account.

I wrote this trigger and it does work, but it keeps hitting limits and failing... not sure how it can be improved to avoid that.
 
trigger Assign_Leads_to_Account2 on Lead (before insert, before update) {

   List<String> LeadEmailDomains = new List<String>();
   Map<String, Id> matchingAcctDomain = new Map<String,Id>();

    for (Lead newLead : trigger.new){
        LeadEmailDomains.add(newLead.Email_Domain__c);
    }
    for(Account acct1 : [Select Id, Email_Domain__c from Account where Email_Domain__c IN : LeadEmailDomains]){
        matchingAcctDomain.put(acct1.Email_Domain__c, acct1.Id);
    }
    for(Account acct2 : [Select Id, Email_Domain_2__c from Account where Email_Domain_2__c IN : LeadEmailDomains]){
        matchingAcctDomain.put(acct2.Email_Domain_2__c, acct2.Id);
    }
    for (Lead newLead : trigger.new){
        if(newLead.account__c == null){
        newLead.Account__c = matchingAcctDomain.get(newLead.Email_Domain__c);
    }
    }

}

Any advice?
Pradhyumn BansalPradhyumn Bansal
Hi Joe,
   Can you please post which error you have when you insert lead(s).



 
Pradhyumn BansalPradhyumn Bansal
Hi Joe,
     
          Here Also if you have two accounts with same domain values which the lead have. Only one account assign to lead because you make Map with domain is key and key are not duplicates in map. So if two accounts have same domain field value then the account id overwrite for that key and only second account is assign to that lead.

Thank you
Joe LavinJoe Lavin
The domain is unique to account. So that is no issue. I am getting:


Error Occurred: The flow tried to update these records: null. This error occurred: LIMIT_EXCEEDED: System.LimitException: pw_cc:Too many SOQL queries: 101. You can look up ExceptionCode values in the SOAP API Developer Guide.

Is it possible to set the trigger so it only runs if Account__c is null? I only have this criteria on get action, but figure it would be more efficient if applied to the newLead for map.
Pradhyumn BansalPradhyumn Bansal
Hi Joe !
    Please try this once -
 if(newLead.account__c == null && matchingAcctDomain.get(newLead.Email_Domain__c) != null){
           newLead.Account__c = matchingAcctDomain.get(newLead.Email_Domain__c);
  }
     
Joe LavinJoe Lavin
I will give that a try. Does it make sense to put an an if( function on the


    for (Lead newLead : trigger.new){
        LeadEmailDomains.add(newLead.Email_Domain__c);

like this?


    for (Lead newLead : trigger.new){
        if(newLead.account__cc == null){
        LeadEmailDomains.add(newLead.Email_Domain__c);
}
}

This way the trigger does not spend resources finding matching account if there is no reason to?