• Joe Lavin
  • NEWBIE
  • 5 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
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?
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?