I have a trigger that assigns incoming leads to the Account owner if an account already exists. It looks for a matching email domain field. This trigger works fine if i or any other user creates account in Salesforce but new leads from Pardot are not getting assigned. The email domain field is a formula field and the field on Accounts is a string field.
Should this be a After Insert?
Trigger:
trigger leadOwnerAssignEmailTrigger on Lead (before insert) {
if(trigger.isBefore && trigger.isInsert) {
LeadOwnerAssignEmailHandler.updateLeadOwner(trigger.new);
}
}
Class:
public class leadOwnerAssignEmailHandler {
public static void updateLeadOwner(List<lead> domainList) {
set<String> domainSet = new set<string>();
For (lead l:domainList){
if(l.domain__c !=null && l.domain__c !='')
domainSet.add(l.domain__c);
}
List<Account> accDomainList = [Select Id, OwnerId, domain__c FROM Account
WHERE domain__c IN: domainSet];
Map<String, Id> accountsByDomainMap = new Map<String, Id>();
if(accDomainList!= null && accDomainList.size()>0) {
for(Account acc : accDomainList) {
accountsByDomainMap.put(acc.domain__c,acc.OwnerID);
}
}
for(lead l : domainList) {
if(accountsByDomainMap != null &&
accountsByDomainMap.containskey(l.domain__c))
l.ownerID = accountsByDomainMap.get(l.domain__c);
}
}
}