function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
wbrproductionswbrproductions 

Reassign Lead Owner before insert Trigger -- assignment rules not executing

Hello,

 

I've been following 2 threads in an attempt to write a trigger that reassigns ownership of certain leads upon insert, then execute assignment rules for the ones not meeting that criteria:

 

http://boards.developerforce.com/t5/General-Development/Urgent-Lead-Assignment-Rule-Trigger-recursively-updates-itself/td-p/174995

 

- and -

 

http://boards.developerforce.com/t5/Apex-Code-Development/How-to-route-LEADs-back-through-LEAD-Assignment-rules/m-p/171285#M26391

 

The problem: this line in the trigger code:

 

lIds.add(l.Id);

 never seems to have an Id to add, so the assignment rules don't reassign anything. I may not have constructed this trigger correctly based on the example, so please let me know if you see something wrong with this.

 

Trigger code:

 

trigger LeadNamedAccountOwnership on Lead (before insert)
{
    List<Id> lIds = new List<id>();
    
    for (lead l : trigger.new)
    {
        // get lead email, lookup related domains
        if (l.Email != null && l.Email.length() > 0)
        {
            LeadCaseAssignment.leadAssignAlreadyCalled = LeadCaseAssignment.SetLeadOwnerByEmail(l);
        }
        if (!LeadCaseAssignment.leadAssignAlreadyCalled && l.IsConverted == false)
        {
            system.debug('leadId = ' + l.Id);
            lIds.add(l.Id);
        }
    }
    if (LeadCaseAssignment.leadAssignAlreadyCalled() == false)
    {
        LeadCaseAssignment.LeadAssign(lIds);
    }
}

 Helper class:

 

public with sharing class LeadCaseAssignment 
{
	public static Boolean leadAssignAlreadyCalled = false;

    public static boolean leadAssignAlreadyCalled()
    {
        return leadAssignAlreadyCalled;
    }
    
    @future
    public static void LeadAssign(List<Id> lIds)
    {
        leadAssignAlreadyCalled = true;
        
        List<Lead> leads = [   Select Id From Lead Where Id IN: lIds];

        for (lead l : leads)
        {
            Database.DMLOptions dmo = new Database.DMLOptions();
            dmo.assignmentRuleHeader.useDefaultRule= true;
            l.setOptions(dmo);
        }
        
        //try{
        update(leads);
        //}
    }

    public static boolean SetLeadOwnerByEmail(Lead lead)
    {
        List<Account> accounts = GetDomainAccounts(lead.Email);
        
        if (accounts.size() > 0)
        {
            if (accounts.get(0).Mid_Market_Named_Region__c != '' || accounts.get(0).US_Enterprise_Region__c != '')
            {
                lead.OwnerId = accounts.get(0).OwnerId;
                return true;
            }
        }

        return false;
    }
    
    private static List<Account> GetDomainAccounts(String email)
    {
        List<Domain__c> domains = new List<Domain__c>();
        List<Account> accts = new List<Account>();
        
        String emailDomain = email.split('@').get(1).trim();

        domains = [ Select Id, Account__c, Name 
                    From Domain__c 
                    Where Name =: emailDomain];
                    
        for (Domain__c domain : domains)
        {
            accts = [   Select a.US_Enterprise_Region__c, a.Mid_Market_Named_Region__c, a.Id, a.OwnerId 
                        From Account a 
                        Where Id =: domain.Account__c limit 1];
        }

        return accts;
    }

    static testMethod void testLeadCaseAssignment()
    {
    	Test.startTest();

        TestUtility util = new TestUtility();
        Account testAccount = util.getTestAccount();
        Contact testContact = util.getTestContact(testAccount);

        Domain__c domain = new Domain__c();
        domain.Account__c = testAccount.Id;
        domain.Name = 'test.com';
        insert domain;

        Lead testLead = util.getTestLead();
        testLead.Email = 'test@test.com';
        update testLead;
                
        system.assertEquals(true, LeadCaseAssignment.leadAssignAlreadyCalled());

        List<Id> ids = new List<Id>();
        ids.add(testLead.Id);
        LeadCaseAssignment.LeadAssign(ids);

        LeadCaseAssignment.SetLeadOwnerByEmail(testLead);
        
        testLead.Email = 'test@doink.com';
        update testLead;
        
        LeadCaseAssignment.SetLeadOwnerByEmail(testLead);

        Test.stopTest();
    }
}

 

Thanks in advance,

Brian

dmchengdmcheng

IDs are not available BeforeInsert because the record has not been created yet.  They are only available in AfterInsert.

trictric

No id's in beforeinsert trigger

 

 

wbrproductionswbrproductions

fair enough, thanks.

 

Followed some similar threads and updated the code as follows:

 

trigger LeadNamedAccountOwnership on Lead (before insert)
{
    List<Id> lIds = new List<id>();
    
    for (lead l : trigger.new)
    {
    	// get lead email, lookup related domains
    	if (l.Email != null && l.Email.length() > 0)
    	{
    		LeadCaseAssignment.leadAssignAlreadyCalled = LeadCaseAssignment.SetLeadOwnerByEmail(l);
    	}
    	if (!LeadCaseAssignment.leadAssignAlreadyCalled && l.IsConverted == false)
    	{
    		Database.Dmloptions dmo = new Database.Dmloptions();
    		dmo.assignmentRuleHeader.useDefaultRule = true;
    		l.setOptions(dmo);
    	}
    }    
}

 The "Database.Dmloptions" code near the bottom should engage the lead assignment rules, but doesn't seem to be working. Any ideas?

 

Thanks again!

dmchengdmcheng

I have not worked with lead or case assignment.  This thread refers to cases, but it might be applicable to leads as well.

 

http://boards.developerforce.com/t5/Apex-Code-Development/using-DMLOptions-in-trigger-not-working/m-p/124461/highlight/true#M15118