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
SDSFuserSDSFuser 

Trigger to Fire Lead Assignment Rules Does not Fire them

 Hello
 I have the below trigger that I want to use to fire assignment rules when the Non_Junk is True.  Thus when the user changes Non_Junk to True I want the lead assignment rules to fire.  The trigger is firing just not the assignment rules.
 
trigger RunAssignmentRules on Lead (before update) {

for (Integer i=0; 1 < trigger.new.size(); i++){
   if (trigger.new[i].Not_Junk__c == TRUE){
    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule= true;
    trigger.new[i].setOptions(dmo);
    }
    }
}
Best Answer chosen by Admin (Salesforce Developers) 
jkucerajkucera

Assignment rules DO fire upon insert. You have to call Database.Update(lead) after assignment 

 

 

 

http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=20880

 

Working example from the above post:

 

trigger WebCaseAssign on Case (after insert) {
    if(trigger.new.size() < 21) {
        for (Case theCase:trigger.new) {
            if(theCase.Origin == 'Web') {
                Database.DMLOptions dmo = new Database.DMLOptions();
                dmo.emailHeader.triggerUserEmail = true;
                dmo.assignmentRuleHeader.useDefaultRule = true;
                theCase.setOptions(dmo);
                Database.update(theCase);    
            }      
        }
    }
}

All Answers

jkljkl

In the loop below, condition you are checking should be "i< trigger.new.size()" instead of "1<trigger.new.size() "

for (Integer i=0; 1 < trigger.new.size(); i++){
SDSFuserSDSFuser
Sorry the code did have an i.....and still does not work 
 
trigger RunAssignmentRules on Lead (before update) {

for (Integer i=0; i < trigger.new.size(); i++){
   if (trigger.new[i].Not_Junk__c == TRUE){
    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule= true;
    trigger.new[i].setOptions(dmo);
    }
    }
}
paul-lmipaul-lmi

assignment rules being triggered via apex only work on an update, not on trigger insert.  so you'd need to something like

 

update trigger.new[]; (no idea if syntax is correct of if bulk friendly...)

 

to actually have those rules kick off.  i don't do this in a trigger, but i know for a fact that seting the DMLOptions for assignment rules on anything other than an Update method will be ignored, unless they fixed that in the winter release and didn't document it.

SDSFuserSDSFuser

Does anyone know the update syntax to get this to work....as I have tried update trigger.new etc and that does not work

 

jkljkl
Could you check the status of the trigger
SDSFuserSDSFuser
It is active and validated
jkucerajkucera

Assignment rules DO fire upon insert. You have to call Database.Update(lead) after assignment 

 

 

 

http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=20880

 

Working example from the above post:

 

trigger WebCaseAssign on Case (after insert) {
    if(trigger.new.size() < 21) {
        for (Case theCase:trigger.new) {
            if(theCase.Origin == 'Web') {
                Database.DMLOptions dmo = new Database.DMLOptions();
                dmo.emailHeader.triggerUserEmail = true;
                dmo.assignmentRuleHeader.useDefaultRule = true;
                theCase.setOptions(dmo);
                Database.update(theCase);    
            }      
        }
    }
}

This was selected as the best answer
SDSFuserSDSFuser

What I want to do is for an existing lead that is already in the system....when a certain field is update to a Yes have the assignment rules fire on save of the lead.

 

Does after insert fire on update of an existing lead.  This does not appear to work for me.

jkucerajkucera

Update on 8-10-10.  My last post was incorrect so I'm changing it below:

You cannot use DML options in an update trigger as that creates a DML error.  To avoid this error, you should call an @future method for the DMLOptions reassignment so that it is outside of the context of the trigger.  Also,  you must check if the method has already been called in the trigger so that it doesn't try to continually update the same lead over and over again (Database.update calls the trigger again, which calls the method again...)

 

Use code from this post:

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