title: Turn off lead assignment rules in salesforce lead trigger
body: <p>I'm having trouble with a Salesforce lead trigger, fired after insert.  The lead assignment rules need to be <strong>on by default</strong>, but I need to bypass the rules for leads that match a certain criteria.</p>
  
  <p>I've seen in the salesforce docs you are able to set assignment rules when inserting/updating leads: <a href="http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_database_dmloptions.htm#assignmentRuleHeader_section" rel="nofollow">http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_database_dmloptions.htm#assignmentRuleHeader_section</a></p>
  
  <p>However, I'm not able to turn off these lead assignment rules:</p>
  
  <pre><code>...
  // if criteria is met to skip assignment rules...
  Lead tempobj1 = new Lead(id=objLead.id,OwnerId=ConDomainNameAndOwnerIdMap.get(Emaildomain),isOwnerChanged__c = true);
  
  Database.DMLOptions dmo = new Database.DMLOptions();
  dmo.assignmentRuleHeader.useDefaultRule= false; //it's still running assignment rules!
  tempobj1.setOptions(dmo);
  
  ...
  ...
  
  update tempobj1;
  </code></pre>
  
  <p>I've also tried creating a new lead assignment rule set with no actual rules and set the DML options to use this - but the default assignment rules are still fired! <code>dmo.assignmentRuleHeader.assignmentRuleID = '01Qe00000000HAq';</code></p>
  
  <p>The order of execution states the record is saved to database (not committed) > after triggers executed > assignment rules executed. So the trigger is run before assignment rules.</p>
  
  <p>I've tried the code on before insert, but get the error 'DML statment cannot operate on trigger.new or trigger.old: Trigger.AssignOwnerToNewLead: line 133, column 1'</p>
  
  <p>Thanks in advance,
  Sam</p>