You need to sign in to do that
Don't have an account?
Joe 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.
Any advice?
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?
Can you please post which error you have when you insert lead(s).
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
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.
Please try this once -
newLead.Account__c = matchingAcctDomain.get(newLead.Email_Domain__c);
}
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?