2

I am baffled by this. For some reason I am seeing all lead triggers fire twice in a row on insert. Even stranger, all the work being done in the first trigger apparently isn't taking. For instance, I can see in the debug logs all the lead Ids being assigned to the batch - they are unique and different between each run. If I attempt to open a record Id from the first run, I get a 'data not available' page as though it has been deleted or never existed. I also wrote some test code to illustrate these odd effects:

trigger testInsert on Lead (before insert, after insert)
{
    if (trigger.isBefore)
    {
        leadTestInsert.triggerHasRun++;

        if (leadTestInsert.triggerHasRun == 1)
            system.debug('assigning title ' + leadTestInsert.triggerHasRun + ' to all leads in batch');

        else if (leadTestInsert.triggerHasRun == 2)
            system.debug('assigning duns number ' + leadTestInsert.triggerHasRun + ' to all leads in batch');

        for (lead l : trigger.new)
        {
            if (leadTestInsert.triggerHasRun == 1)
                l.Title = string.valueof(leadTestInsert.triggerHasRun);

            else if (leadTestInsert.triggerHasRun == 2)
                l.CompanyDunsNumber = string.valueof(leadTestInsert.triggerHasRun);
        }
    }
}

public class leadTestInsert
{
    public static integer triggerHasRun = 0;
}

When I import a batch, all the duns numbers are 2, but title is null, in spite of what the logs read.

Here are some snippets from the resulting debug log:

17:38:12.0 (529150)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS

17:38:12.0 (622377)|CODE_UNIT_STARTED|[EXTERNAL]|01q36000001Gdu6|testInsert on Lead trigger event BeforeInsert for [new, new, new...]

17:38:12.0 (2772805)|USER_DEBUG|[8]|DEBUG|assigning title 1 to all leads in batch

17:38:12.0 (126299156)|CODE_UNIT_FINISHED|testInsert on Lead trigger event BeforeInsert for [new, new, new...]

17:38:13.227 (1227482523)|CODE_UNIT_STARTED|[EXTERNAL]|01q36000001Gdu6|testInsert on Lead trigger event AfterInsert for [00Q3600000G4ZTP, 00Q3600000G4ZTQ, 00Q3600000G4ZTR...]

17:38:13.227 (1229327421)|CODE_UNIT_FINISHED|testInsert on Lead trigger event AfterInsert for [00Q3600000G4ZTP, 00Q3600000G4ZTQ, 00Q3600000G4ZTR...]

The above Ids are not in the system!!!

Beginning an unexpected round two of the same trigger for this batch

17:38:13.227 (1530702279)|CODE_UNIT_STARTED|[EXTERNAL]|01q36000001Gdu6|testInsert on Lead trigger event BeforeInsert for [new, new, new...]

17:38:13.227 (1530957758)|USER_DEBUG|[11]|DEBUG|assigning duns number 2 to all leads in batch

17:38:13.227 (1652591238)|CODE_UNIT_FINISHED|testInsert on Lead trigger event BeforeInsert for [new, new, new....]

17:38:14.561 (2561560019)|CODE_UNIT_STARTED|[EXTERNAL]|01q36000001Gdu6|testInsert on Lead trigger event AfterInsert for [00Q3600000G4ZWT, 00Q3600000G4ZWU, 00Q3600000G4ZWV...]

17:38:14.561 (2563531284)|CODE_UNIT_FINISHED|testInsert on Lead trigger event AfterInsert for [00Q3600000G4ZWT, 00Q3600000G4ZWU, 00Q3600000G4ZWV...]

The above Ids are in the system

17:38:14.561 (3124184801)|CODE_UNIT_FINISHED|TRIGGERS 17:38:14.561 (3124205777)|EXECUTION_FINISHED

Also, as an FYI I have no active workflow rules for the lead object, nor any other active lead triggers.

I am somewhat experienced in apex, but am mindblown right now. Can someone smarter than I am please explain what might be happening? :)

4
  • 3
    check out cropredysfdc.com/2015/04/26/245 for a possible cause
    – cropredy
    Aug 2, 2016 at 1:25
  • 2
    Good call cropredy - this seems to be the issue... -ish. It seems that when there are records that fail on import specifically because of state picklist validation that the trigger re-fires. I have no idea why though or how to turn this off... any ideas? By the way - fantastic article. Really glad folks like yourself are willing to post for the rest of us to benefit :) Aug 2, 2016 at 3:08
  • Brent -- so, are you doing Database.insert(someListofLeads,false) ? That is, allOrNothing = false. If yes, then any error, including VR would be a partial success in a batch. Don't get me started on state and country picklists :-( - you can't turn off the validations.
    – cropredy
    Aug 2, 2016 at 14:57
  • Surprisingly, I'm just using the Data Import Wizard, and have the same results with the Data Loader. My results indicate that when a validation error is thrown because of state & country picklist validation, the trigger behaves in the same manner as database.insert phenomenon you discovered when allOrNothing = false. This behavior does not seem to occur based on any other kind of validation error. No idea why :-| Aug 4, 2016 at 1:43

0

You must log in to answer this question.

Browse other questions tagged .