+ Start a Discussion
RCJesseRCJesse 

DMLOptions in a before insert trigger

I'm going to try and write my order of actions and maybe then somebody can tell me that i'm missing something. What I am trying to accomplish is seperate web-to-lead's into 1 of three round robin assignment rules. The round robin rules function off of a custom object counter (1 for each rule)

 

-Form is filled out firing before insert trigger

-Trigger checks against a field entered from the form, logic is like this:

if x > 9, get the counter value for Large and save into custom lead field, set assignmentRuleID to rule1

 

if x > 3, get the counter value for Medium, save into custom lead field, set assignmentRuleID to rule2

else get the counter value for Small, save into custom lead field, set assignmentRuleID to rule3

 

Now the problem seems to be that the round robin assignment rules are based off of a formula field which is the MOD of the counter value stored in the lead. So if there are 5 people in my group and counter is 3 the mod value would be 2 meaning that the rule entry corresponding to that mod value would be called. But the leads are not being assigned at all. I'm thinking that since the rules are based off a formula value that maybe that value is not set because the lead has not really been saved yet. 

 

I know the trigger is being called and that it is going through my logic but the rules are not being applied at all.I can also see that the lead gets the correct counter value and that its mod value is calculated correctly and stored in the lead. 

 

Any help would be appreciated.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
RCJesseRCJesse

Alright so I got it working, not as cleanly as I would have liked but it works.

 

So I left my trigger as is except that i removed the DML set options code, all it does is verify the lead and depedning on the information in the lead assigns a routing number. Then I just made a long active assignment rule which combined the logic of all three assignments rules i was trying to assign in the code originnally.

 

This might pose problems when we start pushing data into SF from other sources but my assignment rule looks to custom fields that are not going to be used for anything else. So as long as I keep my active assignment rule ready for all senarios then i'm okay.

 

Message Edited by RCJesse on 06-03-2009 08:09 PM

All Answers

NikiVNikiV
I think you are right about the formula field calculation timing.  There could also be workflow that sets other values which doesn't get fired until after the insert triggers.  Why not put the calculation of that formula directly into your trigger code?  Then it will be calculated on the fly and you can output the results into the debug log to make sure it's what you expect.
RCJesseRCJesse

Okay so i put the calculation of the mod number directly in the trigger and save it but the assignment rule is still not being called.

I should note that this is for Web-To-Leads.

 

 

This is my current code:

 

*************************************

 

 
trigger webLead on Lead (before insert) {
    Database.DMLOptions dmoEmergingBusinessTeam = new Database.DMLOptions();
    dmoEmergingBusinessTeam.assignmentRuleHeader.assignmentRuleId= '01Q800000007hZ3';  

    Database.DMLOptions dmoSmallBusinessGroup = new Database.DMLOptions();
    dmoSmallBusinessGroup.assignmentRuleHeader.assignmentRuleId= '01Q800000007gil';  

    Database.DMLOptions dmoMidSizeBusinessGroup = new Database.DMLOptions();
    dmoMidSizeBusinessGroup.assignmentRuleHeader.assignmentRuleId= '01Q800000007hZ3';  
    
    Counter__c c;
    Integer j=0;
    
    for(Lead l : Trigger.new) {
    	l.Notes__c ='this is before if';
    	if((l.LeadSource=='RC Office Weblead') || (l.LeadSource=='RC Online Weblead')) {
    		l.Notes__c='this is after if';
		   	if(l.NumberOfEmployees > 9) {
	        	// send to Mid size business group
	    		c = [select id,value__c from Counter__c where name='Mid Size Business Group'];
	    		c.Value__c++;
	    		j=c.Value__c.intValue();
	    		l.Mid_Size_Business_Group_Lead_Number__c=math.mod(j,1);	
	    		l.setOptions(dmoMidSizeBusinessGroup);	    		
	    		update c;  
	    		l.Notes__c='this is more than 9 people'; 		
	    	}
	    	else if(l.NumberOfEmployees > 3) {
	    		// send to Small business group		
	    		c = [select id,value__c from Counter__c where name='Small Business Group'];
	    		++c.Value__c;
	    		j=c.Value__c.intValue();
	    		l.Small_Business_Group_Lead_Number__c=math.mod(j,6);
	    		l.setOptions(dmoSmallBusinessGroup);
	    		update c;
	    		l.Notes__c='this is more than 3 people';    		
	    	}
	    	else {
	    		// send to emerging business team	    		
	    		c = [select id,value__c from Counter__c where name='Emerging Business Team'];
	    		++c.Value__c;
	    		j=c.Value__c.intValue();
	    		l.Emerging_Business_Team_Lead_Number__c=math.mod(j,14);
	    		l.setOptions(dmoEmergingBusinessTeam);
	    		update c;
	    		l.Notes__c='this 1-3 people';
	    	}
    	}
    }
}
Message Edited by RCJesse on 05-29-2009 06:53 PM
Message Edited by RCJesse on 05-29-2009 07:24 PM
RCJesseRCJesse

This is the my debug log:

 

*** Beginning webLead on Lead trigger event BeforeInsert for null

20090530013441.979:Trigger.webLead: line 14, column 5: SelectLoop:LIST:SOBJECT:Lead
20090530013441.979:Trigger.webLead: line 30, column 21: SOQL query with 1 row finished in 20 ms
20090530013441.979:Trigger.webLead: line 35, column 17: Update: SOBJECT:Counter__c
20090530013441.979:Trigger.webLead: line 35, column 17:     DML Operation executed in 25 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 1 out of 20
Number of query rows: 1 out of 1000
Number of SOSL queries: 0 out of 0
Number of DML statements: 1 out of 20
Number of DML rows: 1 out of 100
Number of script statements: 17 out of 10200
Maximum heap size: 0 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 0
Number of System.runAs() invocations: 0 out of 0

Total email recipients queued to be sent : 0
Static variables and sizes:
webLead:c:44
webLead:dmoEmergingBusinessTeam:34
webLead:dmoMidSizeBusinessGroup:34
webLead:dmoSmallBusinessGroup:34
webLead:j:4


*** Ending webLead on Lead trigger event BeforeInsert for null

*** Beginning leadDupCheck on Lead trigger event BeforeInsert for null


Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 1 out of 20
Number of query rows: 1 out of 1000
Number of SOSL queries: 0 out of 0
Number of DML statements: 1 out of 20
Number of DML rows: 1 out of 100
Number of script statements: 17 out of 10200
Maximum heap size: 0 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 0
Number of System.runAs() invocations: 0 out of 0

Total email recipients queued to be sent : 0

*** Ending leadDupCheck on Lead trigger event BeforeInsert for null


Spooling All Immediate Actions
*** Beginning Workflow Evaluation
User: Jesse Ahluwalia
Start Time: 20090530013442.088
Starting All Rules Evaluation
Starting evaluation of rule type Assignment
Starting evaluation of rule type Response
[Lead: sh fdh 00Q8000000QFpHl]
Rule Name: Web lead
Evaluating Workflow Entry Criteria: 
Spooling All Immediate Actions
Ending All Rules Evaluation
Bulk Execute all Immediate Actions
Starting evaluation of rule type Workflow
Starting evaluation of rule type Escalation
End Time: 20090530013442.105
*** Ending Workflow Evaluation
NikiVNikiV

I wonder if the problem is there are 2 before insert triggers, and you are setting the DML options in the trigger that is running first.  I don't know if it retains the options between triggers or not - perhaps you can write more into the debug log using system.debug() commands instead of putting data into lead fields.  That way you can follow things along in the debug log as it goes.

 

I would recommend putting all your before insert logic into a single trigger anyways as you can't control which one happens first - maybe that will shed light on why the assignment rules aren't running as well.

RCJesseRCJesse

That is a good thought. I'm not to sure how to "write more into the debug log", I'll take a look at the system.debug() references. I guess I will just try to expose the DML option in a few places.

 

I can definetly try combining my weblead before insert trigger and my leaddupcheck before insert trigger. Actually i can deleted my leaddupcheck all together. I'll try that right now.

RCJesseRCJesse

I made my leaddupcheck trigger inactive as I wasnt using it anyways. I would delete it but frankly I have no idea how to delete it. This whole production/sandbox/IDE thing is a bit confusing.

 

In any case nothing changed except my debug log is shorter :)

Message Edited by RCJesse on 06-02-2009 06:32 PM
RCJesseRCJesse

Alright so I got it working, not as cleanly as I would have liked but it works.

 

So I left my trigger as is except that i removed the DML set options code, all it does is verify the lead and depedning on the information in the lead assigns a routing number. Then I just made a long active assignment rule which combined the logic of all three assignments rules i was trying to assign in the code originnally.

 

This might pose problems when we start pushing data into SF from other sources but my assignment rule looks to custom fields that are not going to be used for anything else. So as long as I keep my active assignment rule ready for all senarios then i'm okay.

 

Message Edited by RCJesse on 06-03-2009 08:09 PM
This was selected as the best answer