I need to be able to detect that the trigger execution is happening because of a workflow field update. There is a suggestion in the cookbook:
Controlling Recursive Triggers
But this solution is not properly bulkified. If my batch of records is split up (200, 100, 50 whatever the server decides) the 2nd to nth batches are not processed correctly because the flag has already been set.
The second part of the problem is: This is part of a managed package. I have no control of the customer's workflow rules, supporting and configuring a flag/last changed field on a record is not realistic, our company doesn't have the support resources to be walking each customer through the configuration.
Has anyone been able to solve this problem for large batches of records?
Edit (In Response to Calebs Answer)
Separate trigger batches do not run in separate contexts
trigger AccountTriggerContext on Account (before insert) {
System.debug('counter='+TriggerContextCounter.getCounter());
}
.
global without sharing class TriggerContextCounter {
private static Integer counter = 0;
public static Integer getCounter() {
Integer value = counter;
counter++;
return value;
}
private static testMethod void testTriggerContexts() {
List<Account> accounts = new List<Account>();
for(Integer i = 0; i < 250; i++) {
accounts.add(new Account(Name='testTriggerContexts: ' + i));
}
insert accounts;
}
}
Unit test debug messages
USER_DEBUG|[2]|DEBUG|counter=0
USER_DEBUG|[2]|DEBUG|counter=1
Both counters should have been 0 if the batches were running in separate contexts
Edit Further explanation of background for @Ralph
This is a generalized explanation, but when certain conditions are met, some date fields on other objects are updated. The trigger runs and updates the dates.
If a workflow rule causes a field update on the object, the same trigger is run again and the dates on the other objects are updated for a second time. And because this is an increment operation the dates on the other objects are off by a factor of 2.