0

This is more of a conceptual question related to Apex.

We have a functionality where we have case trigger. That trigger creates child cases, so case trigger runs again. This is what case trigger looks like -

trigger CaseTrigger on Case (before insert, before update, after insert, after update){
    CaseTriggerhandler = new CaseTriggerhandler();
    if (trigger.isBefore) {
        if (trigger.isInsert) {
      handler.beforeInsert(trigger.new);
    }
    else if (trigger.isUpdate) {
      handler.beforeUpdate(trigger.new, trigger.oldMap);
    }
    } else {
        if (trigger.isInsert) {
            handler.afterInsert(trigger.new);
        }
        else if (trigger.isUpdate) {
            handler.afterUpdate(trigger.new, trigger.old, trigger.oldMap);
        }
    }
}

I believe the idea behind it was that we will have an instance of handler processing parent case. When this trigger (while running for a parent case) inserts child cases (that code is in handler), trigger is invoked again, and we have a separate handler instance running for child cases.

We have multiple teams writing their own case triggers. So we are migrating to this popular trigger framework - https://krishhari.wordpress.com/2013/07/22/an-architecture-framework-to-handle-triggers-in-the-force-com-platform/

With this framework, we create a class, and add a custom metadata record for listing that class for case trigger.

When case trigger is invoked, trigger framework will pull all the classes that are listed for object Case, run them one by one.

This way, instead of each team having their own trigger, they all have their own Classes. Framework takes care of running all of them one by one.

So now i have to create a class, which will be equivalent to framework's trigger handler, but i dont want to touch our existing handler class.

Whatever code is in CaseTrigger, we want to replace it in a class.

I tried this -

public with sharing class CaseTriggerHandler_Adapter extends TriggerHandlerBase{

    static CaseTriggerHandler handler = new CaseTriggerHandler();
    

    public override void handleBeforeInsert(TriggerParameters tp, List<SObject> filterList){
        handler.beforeInsert((List<Case>)tp.newList);
    }

    public override void handleBeforeUpdate(TriggerParameters tp, List<SObject> filterList){
        handler.beforeUpdate((List<Case>)tp.newList, (Map<Id,Case>)tp.oldMap);
    }

    public override void handleAfterInsert(TriggerParameters tp, List<SObject> filterList){
        handler.afterInsert((List<Case>)tp.newList);
    }

    public override void handleAfterUpdate(TriggerParameters tp, List<SObject> filterList){
        handler.afterUpdate((List<Case>)tp.newList,(List<Case>)tp.oldList,(Map<Id,Case>)tp.oldMap);
    }

}

So framework has single Case trigger. It iterates over custommetadata records. It picks up the class CaseTriggerHandler_Adapter. It instantiates this class, runs the code inside it.

Code inside it inserts child cases.

Trigger gets invoked again. This class gets instantiated again.

But issue is we have same instance of CaseTriggerHandler running for parent case as well as child case, due to it being static. So it deviates from original flow that we had (where we know it is a recursive trigger, and there is a separate instance of CaseTriggerHandler for each recursion.

Singleton pattern will do the same.

Any suggestions on how i can preserve original flow of code while migrating from trigger to a class?

0

As near as I can tell, you simply need to make the handler variable not static:

public with sharing class CaseTriggerHandler_Adapter extends TriggerHandlerBase{
    CaseTriggerHandler handler = new CaseTriggerHandler();

This should allow you to emulate the original behavior.

1
  • Yeah i picked up a java book ... so nonstatic instances gets initialized when class gets initialized. I was under impression that i can only declare non-static variables, and only initialize them either in a constructor or by having them as a static variable. Sorry, poor java concepts on my part. Thank you for your help. – user2957592 Oct 15 '20 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.