Skip to content

Help with the finalizer #192

@justin-lyon

Description

@justin-lyon

Overview

I was just trying to demo the DMLFinalizer functionality. Below is my code and cmdt for the Opportunity. I'm expecting the Finalizer to simply call System.debug but I don't see my USER_DEBUG line in the debug logs. This is in a new Developer Edition sandbox with TAF, Apex-rollup, and Nebula Logger installed as unlocked packages.

Trigger

trigger OpportunityTrigger on Opportunity (
  before insert,
  before update,
  after insert,
  after update
) {
  new MetadataTriggerHandler().run();
}

Trigger Action

public with sharing class Opportunity_SalesRules implements TriggerAction.BeforeInsert, TriggerAction.BeforeUpdate {
  public static final String PROSPECTING_STAGE = 'Prospecting';
  public static final String INVALID_STAGE_INSERT_ERROR = 'New Opportunities must start in the Prospecting stage.';

  public void beforeInsert(List<Opportunity> newOpportunities) {
    for (Opportunity newOpp : newOpportunities) {
      if (newOpp.StageName != 'Prospecting') {
        Logger.debug(INVALID_STAGE_INSERT_ERROR, newOpp);
        newOpp.addError(INVALID_STAGE_INSERT_ERROR);
      }
    }
  }

  public static final String CLOSED_WON_STAGE = 'Closed Won';
  public static final String INVALID_STAGE_UPDATE_ERROR = 'Closed Won Opportunities cannot be moved to another stage.';

  public void beforeUpdate(List<Opportunity> newOpps, List<Opportunity> oldOpps) {
    Map<Id, Opportunity> oldOppMap = new Map<Id, Opportunity>(oldOpps);
    for (Opportunity newOpp : newOpps) {
      Opportunity oldOpp = oldOppMap.get(newOpp.Id);
      if (oldOpp.StageName == CLOSED_WON_STAGE && newOpp.StageName != CLOSED_WON_STAGE) {
        Logger.debug(INVALID_STAGE_UPDATE_ERROR, newOpp);
        newOpp.addError(INVALID_STAGE_UPDATE_ERROR);
      }
    }
  }
}

Account Finalizer

public with sharing class Account_DmlFinalizer implements TriggerAction.DmlFinalizer {
  public void execute(FinalizerHandler.Context context) {
    System.debug('Account DML Finalizer executed.');
    Logger.saveLog();
  }
}

Opp Finalizer

public with sharing class Opportunity_DmlFinalizer implements TriggerAction.DmlFinalizer {
  public void execute(FinalizerHandler.Context context) {
    System.debug('Opportunity DML Finalizer executed.');
    Logger.saveLog();
  }
}

Opportunity Trigger Setting

Image

Finalizer Config

Account Finalizer Config

Image

Opp Finalizer Config

Image

Two Finalizers

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions