I have a trigger with two events, Before Insert and Before Update. They are working fine when I make an object or update an object in org, but when I'm trying to create a object and update that object in testclass then only insert is working update is not working. If I remove before insert event from trigger then before update is working. [before insert + before update = before insert working but before update not working], individually , [before insert = working ] [before update = working ]
I am using a trigger template, Here is the code :
Trigger :
trigger CompTrigger on Comp__c (before insert , before update)
{
new TriggerTemplateV2.TriggerManager(new TriggerHandler()).runHandlers();
}
Handler :
public with sharing class TriggerHandler extends TriggerTemplateV2.TriggerHandlerAdapter {
// Will be called before insertion of object
public override void onBeforeInsert (List<sObject> newList)
{
system.debug('IN onBeforeInsert');
}
// Will be called before updation of object
public override void onBeforeUpdate (List<sObject> newList, Map<Id, sObject> newMap, List<sObject> oldList, Map<Id, sObject> oldMap)
{
system.debug('IN onBeforeUpdate');
}
}
/**
* Helper method to get the class name for a given Object
* Used as the key identifier of the trigger handler classes
*/
private String getClassName(Object o)
{
String className = null;
if(o != null)
{
// The value looks something like this: "ProjectTriggerHandler:[idToOldProjectMap=null, newProjectList=null]"
String objectAsString = String.valueOf(o);
className = objectAsString.substring(0,objectAsString.indexOf(':'));
}
return className;
}
}
/**
* Interface definitions for the before and after trigger handlers
*
*/
public interface BeforeTriggerHandler
{
void onBeforeInsert (List<sObject> newList);
void onBeforeUpdate (List<sObject> newList, Map<Id, sObject> newMap, List<sObject> oldList, Map<Id, sObject> oldMap);
void onBeforeDelete (List<sObject> oldList, Map<Id, sObject> oldMap);
}
public interface AfterTriggerHandler
{
void onAfterInsert (List<sObject> newList, Map<Id, sObject> newMap);
void onAfterUpdate (List<sObject> newList, Map<Id, sObject> newMap, List<sObject> oldList, Map<Id, sObject> oldMap);
void onAfterDelete (List<sObject> oldList, Map<Id, sObject> oldMap);
void onAfterUndelete (List<sObject> newList, Map<Id, sObject> newMap);
}
/**
* An abstract adapter class that can be extended for convenience. By extending
* this class only the relevant handler methods need to be implemented.
*/
public abstract class TriggerHandlerAdapter implements BeforeTriggerHandler, AfterTriggerHandler
{
public virtual void onBeforeInsert (List<sObject> newList) {}
public virtual void onBeforeUpdate (List<sObject> newList, Map<Id, sObject> newMap, List<sObject> oldList, Map<Id, sObject> oldMap) {}
public virtual void onBeforeDelete (List<sObject> oldList, Map<Id, sObject> oldMap) {}
public virtual void onAfterInsert (List<sObject> newList, Map<Id, sObject> newMap) {}
public virtual void onAfterUpdate (List<sObject> newList, Map<Id, sObject> newMap, List<sObject> oldList, Map<Id, sObject> oldMap) {}
public virtual void onAfterDelete (List<sObject> oldList, Map<Id, sObject> oldMap) {}
public virtual void onAfterUndelete (List<sObject> newList, Map<Id, sObject> newMap) {}
}
}
TestClass :
@isTest
public class Test_TriggerHandler {
static testmethod void TriggerHandler_Test()
{
system.debug('in test method');
Test.startTest();
Comp__c comp = new Comp__c();
insert comp;
cmp.Status__c = 'failed' ;
update comp;
Test.stopTest();
}
}
system.debug()
comes in handy. You're expecting something to happen that's gated behind several levels of conditions, and the thing you're expecting to run isn't being run. Temporary debug statements to narrow down where exactly things are going wrong are helpful.