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
Terry411Terry411 

Trigger Lead Assignment

I need to trigger the lead assignment rules from a Lead Before trigger but only on some records.  The before trigger is processing some additional requirements so the code below is only a partial set.  I exit the trigger early if if my initial for loop doesn't require me to go do the additional processes.  However, this initial loop is setting the .setOption on all the records so wouldn't that trigger the Lead Assignment Rules regardless?  Because it's not, I thought if i add the .setOption to my second loop, that that would trigger the rules but it does not either.  Where am I going wrong?  (yes... l1.Ignore_Rules__c is false)

 

 

trigger leadBefore on Lead (before insert, before update) {

	Database.DMLOptions dmlOpts = new Database.DMLOptions();
	dmlOpts.assignmentRuleHeader.useDefaultRule = true;
	
	list<Lead> hasPI = new list<Lead>();
	list<Lead> allLeads = new list<Lead>();
    for( Lead l1 : trigger.new ) {
    	if (l1.Ignore_Rules__c == false) l1.setOptions(dmlOpts);
    	allLeads.add(l1);
    	
    	if (l1.Board_Reporting__c == true || l1.Certifier__c == true || l1.CSR__c == true || l1.Funds__c == true || 
    			l1.Private_Reporting__c == true || l1.Recovery_and_Resolution__c == true || l1.SEC_Reporting__c == true || 
    			l1.Section_16__c == true) {
    		
    		hasPI.add(l1);
    	}
    }
	if (hasPI.isEmpty() == true) return;

/*  more code below that processes records in the hasPI list.  
    then eventually we get to the final for loop where I'm setting the .setOptions(dmlOpts) along with several other fields (only showing the setOption below)
*/

    for( Lead l : allLeads ){
		l.setOptions(dmlOpts);
       }