You need to sign in to do that
Don't have an account?
Jonathan Boulter
Is there a way around these problems or do I just have to accept some messy code and only 70% coverage :(
Trigger Framework / testing for null Trigger
Hi,
I've found that my triggers are becoming difficult to maintain, so I decided to create a framework that would hopefully make things easier. I've used Kevin O'Hara post (https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices) as a starting point, but I've stripped out a lot of it.
My code is working, but is a little messy because I've hit a problem with testing Trigger.isBefore:
private void setTriggerContext(string ctx) { if(ctx == null){ if(Trigger.isBefore && Trigger.isInsert) { this.context = TriggerContext.BEFORE_INSERT; } else if(Trigger.isBefore && Trigger.isUpdate) { this.context = TriggerContext.BEFORE_UPDATE; } else if(Trigger.isBefore && Trigger.isDelete) { this.context = TriggerContext.BEFORE_DELETE; } else if(Trigger.isAfter && Trigger.isInsert) { this.context = TriggerContext.AFTER_INSERT; } else if(Trigger.isAfter && Trigger.isUpdate) { this.context = TriggerContext.AFTER_UPDATE; } else if(Trigger.isAfter && Trigger.isDelete) { this.context = TriggerContext.AFTER_DELETE; } else if(Trigger.isAfter && Trigger.isUndelete) { this.context = TriggerContext.AFTER_UNDELETE; } else { this.context = TriggerContext.UNKNOWN; } } else { if(ctx == 'before insert') { this.context = TriggerContext.BEFORE_INSERT; } else if(ctx == 'before update') { this.context = TriggerContext.BEFORE_UPDATE; } else if(ctx == 'before delete') { this.context = TriggerContext.BEFORE_DELETE; } else if(ctx == 'after insert') { this.context = TriggerContext.AFTER_INSERT; } else if(ctx == 'after update') { this.context = TriggerContext.AFTER_UPDATE; } else if(ctx == 'after delete') { this.context = TriggerContext.AFTER_DELETE; } else if(ctx == 'after undelete') { this.context = TriggerContext.AFTER_UNDELETE; } else { this.context = TriggerContext.UNKNOWN; } } system.debug('Context Set: ' + this.context); }What I'd like to write is:
private void setTriggerContext(String ctx) { if(ctx == 'before insert' || (Trigger.isBefore && Trigger.isInsert)) { this.context = TriggerContext.BEFORE_INSERT; } else if(ctx == 'before update' || (Trigger.isBefore && Trigger.isUpdate)) { this.context = TriggerContext.BEFORE_UPDATE; } else if(ctx == 'before delete' || (Trigger.isBefore && Trigger.isDelete)) { this.context = TriggerContext.BEFORE_DELETE; } else if(ctx == 'after insert' || (Trigger.isAfter && Trigger.isInsert)) { this.context = TriggerContext.AFTER_INSERT; } else if(ctx == 'after update' || (Trigger.isAfter && Trigger.isUpdate)) { this.context = TriggerContext.AFTER_UPDATE; } else if(ctx == 'after delete' || (Trigger.isAfter && Trigger.isDelete)) { this.context = TriggerContext.AFTER_DELETE; } else if(ctx == 'after undelete' || (Trigger.isAfter && Trigger.isUndelete)) { this.context = TriggerContext.AFTER_UNDELETE; } else { this.context = TriggerContext.UNKNOWN; } }Unfortunatly when I test this I get an Attempt to de-reference a null object error, which I'm guessing is because its not being run from a Trigger. I've tried:
if(Trigger == null) { // Set Trigger to something... }But get greeted with unexpected token: 'Trigger'...
Is there a way around these problems or do I just have to accept some messy code and only 70% coverage :(
UC Innovation
Are you onlycalling setTriggerContext() from a trigger? If so, the Trigger variable should exist when you get to setTriggerContext().
Jonathan Boulter
I'm just trying to test the framework at the moment. The unit test for the actual trigger does test the other part of the code, but I want to be able to tidy it up (and have the unit test for the framework not miss out such a large chunk of code).