Search code examples
salesforceapex-codeapex

Lead Assignment Trigger Incorrectly Firing On Insert


I am trying to create an Apex trigger that will re-run Lead assignment rules if a "Reassign" checkbox on the Lead is set to true.

I have the trigger below written in one of my Sandbox Orgs, and it works perfectly when an existing Lead is edited to meet this criteria.

However, I am noticing that the assignment rules are now triggering upon the creation of any Lead, even though the "Assign using active assignment rules" checkbox is not set to True.

Does anyone know what I'm missing?

Thanks!

 trigger runAssignmentRule on Lead (after insert,after update) {

  //Variable declaration
  List<Lead> leadList = new List<Lead>();


  for (Lead leadObj : Trigger.new) {
    if (leadObj.Reassign__c == TRUE) {
      leadList.add(new Lead(id = leadObj.id));
      }

    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule = true;
    Database.update(leadList, dmo);
  }

Solution

  • It turns out that my Org has some custom Round Robin code that sits on top of the Assignment rules. This code was triggering assignment rules to be run when a certain field was set to true, which I was using as my test case.