2

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);
    }
    }
   }
1

You mentioned a very important key phrase here: The email domain field is a formula field.

Formula fields are VERY slow to query and difficult to run for the Salesforce Query engine. I would highly recommend that you run this query through the Query Optimizer. It's possible that your query may not be able to return all of your Pardot data within the 10 minute timeout limit. Formula fields need to be parsed before they can be queried, so if you have a large database of leads, this query simply may not run for you. Essentially every record in your database has to be read and processed for your query to run and complete.

  • Would it be better to just make it an After insert trigger? – dotnetdev Mar 27 '16 at 22:14
  • If the query will no longer be operating on formula fields, that would be a wise thing to do. If they're still formula fields, I don't know that it will make any difference. However, if your Pardot data is requiring you to make a web services callout, then that would make a difference because it would presumably all now be in the same Salesforce instance within a single database. – crmprogdev Mar 27 '16 at 22:43
  • The formula field was just splitting the email field to get the domain. I'll try to split the email field in the trigger and use that instead of the formula field. domainSet.add(l.Email.split('@').get(1)); if(accountsByDomainMap != null && accountsByDomainMap.containskey(l.email.split('@').get(1))) { l.ownerID = accountsByDomainMap.get(l.email.split('@').get(1)); – dotnetdev Mar 27 '16 at 23:53

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.