+ Start a Discussion
ColaCola 

How to get a trigger to run after lead assignment rules?

Hi,

I currently have a trigger that checks if a new lead has the same email domain as an existing account. If it does, then the lead owner is assigned to the account owner. I have tested the trigger by itself and it seems to work as required. Here is the trigger for reference: 

trigger AssignNewLeadToAccount on Lead (after insert)  {

	public list<Lead> LeadsToUpdate = new List<Lead>();

	Map<string,Id> DomainAccountMap = new Map<string,Id>();
    for (Account a :[SELECT ownerID, Email_domain__c FROM Account WHERE Email_domain__c != null]) {
		DomainAccountMap.put(a.email_domain__c, a.ownerId);
    }

    for(integer i=0; i < trigger.new.size();i++) {
        ID owner = DomainAccountMap.get(trigger.new[i].Email_domain__c);
        if(owner != null) {
            LeadsToUpdate.add(new Lead(Id=trigger.new[i].Id, Ownerid = owner));
        }  
    }
    
    if(leadstoupdate.size() > 0){
        update LeadsToUpdate;
    }
}

However, I have existing lead assignment rules that seem to be overriding this trigger. What is the easiest way to have the trigger take priority over the lead assignment rule when the lead email domain is equal to an existing account email domain? 
Hargobind_SinghHargobind_Singh
Hi Cola, 

As per Salesforce execution sequence https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm, the assignment rules will run after triggers have already executed. So definitely that could over-write your trigger updates. 

Can you use a checkbox field on your Lead, and set it to true in your trigger, and you can add that field check in your assignment rule in conditions ?