Is it always necessary to write a trigger which can be run only once?
And what is the best way to control a trigger recursion.
I have tried the below code to handle trigger recursion;
Trigger
trigger TestTrigger on Campsite__c (before insert,before update,after insert,after update) {
if(trigger.isBefore && trigger.isInsert)
{
if(!TestTriggerHelper.beforeInsertOperationProcessed)
{
TestTriggerHelper.processBeforeInsertion(Trigger.new,Trigger.newMap);
TestTriggerHelper.beforeInsertOperationProcessed = true;
for(Sobject c : Trigger.new)
system.debug(((Campsite__c)c).email__C);
}
else
{
TestTriggerHelper.beforeInsertCount++;
system.debug('Not Processing before insert'+' : '+TestTriggerHelper.beforeInsertCount);
for(Sobject c : Trigger.new)
system.debug(((Campsite__c)c).email__c);
}
}
if(trigger.isBefore && trigger.isUpdate)
{
if(!TestTriggerHelper.beforeUpdateOperationProcessed)
{
TestTriggerHelper.processBeforeUpdate(Trigger.new,Trigger.newMap);
TestTriggerHelper.beforeUpdateOperationProcessed = true;
for(Sobject c : Trigger.new)
system.debug(((Campsite__c)c).email__c);
}
else
{
TestTriggerHelper.beforeUpdateCount++;
system.debug('Not Processing before update'+' : '+TestTriggerHelper.beforeUpdateCount);
for(Sobject c : Trigger.new)
system.debug(((Campsite__c)c).email__c);
}
}
if(trigger.isAfter && trigger.isInsert){
if(!TestTriggerHelper.afterInsertOperationProcessed)
{
TestTriggerHelper.processAfterInsertion(Trigger.new,Trigger.newMap);
TestTriggerHelper.afterInsertOperationProcessed = true;
for(Sobject c : Trigger.new)
system.debug(((Campsite__c)c).email__c);
}
else
{
TestTriggerHelper.afterInsertCount++;
system.debug('Not Processing after insert'+' : '+TestTriggerHelper.afterInsertCount);
for(Sobject c : Trigger.new)
system.debug(((Campsite__c)c).email__c);
}
}
if(trigger.isAfter && trigger.isUpdate)
{
if(!TestTriggerHelper.afterUpdateOperationProcessed)
{
TestTriggerHelper.processAfterUpdate(Trigger.new,Trigger.newMap);
TestTriggerHelper.afterUpdateOperationProcessed = true;
for(Sobject c : Trigger.new)
system.debug(((Campsite__c)c).email__C);
}
else
{
TestTriggerHelper.afterUpdateCount++;
system.debug('Not Processing after update'+' : '+TestTriggerHelper.afterUpdateCount);
for(Sobject c : Trigger.new)
system.debug(((Campsite__c)c).email__c);
}
}
}
TriggerHelper
public class TestTriggerHelper {
public static integer beforeInsertCount = 0;
public static integer beforeUpdateCount = 0;
public static integer afterInsertCount = 0;
public static integer afterUpdateCount = 0;
public static Boolean beforeInsertOperationProcessed = false;
public static Boolean beforeUpdateOperationProcessed = false;
public static Boolean afterInsertOperationProcessed = false;
public static Boolean afterUpdateOperationProcessed = false;
public static void processBeforeInsertion(List newOrders,Map newOrdersMap)
{
beforeInsertCount++;
system.debug('Processing before insert'+' : '+beforeInsertCount);
}
public static void processBeforeUpdate(List newOrders,Map newOrdersMap)
{
beforeUpdateCount++;
system.debug('Processing before update'+' : '+beforeUpdateCount);
}
public static void processAfterInsertion(List newOrders,Map newOrdersMap)
{
afterInsertCount++;
system.debug('Processing after insert'+' : '+afterInsertCount);
}
public static void processAfterUpdate(List newOrders,Map newOrdersMap)
{
afterUpdateCount++;
system.debug('Processing after update'+' : '+afterUpdateCount);
}
}
I have a Campsite object, and have two workflow rules with field updates which make the trigger get fired totally 3 times (Once for Insert or update and two times because of workflow updates).
workflow rule ‘Rule - 1’ with condition Name contains ‘Test’ i.e. “Campsite: Campsite NameCONTAINSTest” Action is a field update which updates Name to ‘Best’. workflow rule ‘Rule - 2’ with condition Name contains ‘Best’ i.e. “Campsite: Campsite NameCONTAINSBest” Action is a field update which updates Name to ‘Rest’.
Though it works for various scenarios as inserting more that 200 records or updating more than 200 records, but it fails for upsert.
TestScript:
campsite__c c = [select ID,Name,email__c from campsite__c limit 1];
c.name='Test';
campsite__c c2 = new Campsite__C(NAme='Test',email__c='[email protected]');
upsert new List{c,c2};
I can see in debug log that object c is not processed. Please do enlighten me the best approach.