You need to sign in to do that
Don't have an account?
SDSFuser
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);
}
}
}
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
In the loop below, condition you are checking should be "i< trigger.new.size()" instead of "1<trigger.new.size() "
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.
Does anyone know the update syntax to get this to work....as I have tried update trigger.new etc and that does not work
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);
}
}
}
}
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.
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