When using Apex Database.DMLOptions for invoking standard Lead Assignment Rules, is it sane to use beforeUpdate instead of afterupdate? And is it sane to do this in non-future apex vs (as opposed to) @future apex? These questions are phrased below in more detail.
Trigger code
Trigger leadTrigger on Lead (before update){ //see Question 2
Set<Id> leadsToAssign = new Set<Id>();
for(Lead lead : (List<Lead>)Trigger.new){
//optional conditions for populating leadsToAssign
leadsToAssign.add(Lead.Id);
}
if(system.isfuture()) return; // avoid recursion scenario of the future method update
leadutil.autoassign(leadsToAssign);
}
}
Apex Class
public class leadutil {
@future //see Question 1
public static void autoassign(set<id> ids) {
lead[] leads = [select id from lead where id in :ids];
if(leads.isempty()) return;
database.dmloptions dmo = new database.dmloptions();
dmo.assignmentruleheader.usedefaultrule = true;
for(lead l:leads)
l.setoptions(dmo);
update leads;
}
}
Question 1:
What issues might I be overlooking in trying to do this synchronously (instead of in @future)? I don't think I need higher limits so if i can avoid using a @future call then i will; I care more about giving the users a "real-time" experience so that they can see the result of Lead Assignment without having to wait for a @future call to finish. Why are so many people in favor of doing this in a @future call?
Question 2:
What issues might I be overlooking when I put the logic in a beforeUpdate trigger context as opposed to an after update context?