Clarifications on Finalizer Method
sethuramangithub opened this issue · 3 comments
Dear Mitchspano,
The issue is related to #127
I am currently assuming that, through Finalizers we can do single DML for the entire transaction per object. Kindly confirm whether my understanding is correct.
Kindly check my code below and let me know your suggestions.
ApexTrigger:
trigger CEP_OpportunityTrigger on Opportunity (
before insert,
after insert,
before update,
after update,
before delete,
after delete,
after undelete
) {
new MetadataTriggerHandler().run();
}
Apex Class:
public class CEP_TA_Opportunity_RecalculateCategory implements TriggerAction.AfterUpdate {
public void afterUpdate(List<Opportunity> newList, List<Opportunity> oldList) {
Map<Id,Opportunity> oldMap = new Map<Id,Opportunity>(oldList);
List<Opportunity> oppsToBeUpdated = new List<Opportunity>();
for (Opportunity opp : newList) {
if (
TriggerBase.idToNumberOfTimesSeenAfterUpdate.get(opp.id) == 1 &&
opp.StageName != oldMap.get(opp.id).StageName
) {
Opportunity curOpp = new Opportunity (Id = opp.id);
curOpp.Type = 'Existing Business';
oppsToBeUpdated.add(curOpp);
}
}
if (!oppsToBeUpdated.isEmpty()) {
this.recalculateCategory(oppsToBeUpdated);
}
}
private void recalculateCategory(List<Opportunity> opportunities) {
update opportunities;
}
}
Am also having Finalizer class
public with sharing class Finalizer implements TriggerAction.DmlFinalizer {
public void execute(FinalizerHandler.Context context) {
System.debug('Finalized!');
}
}
I tried the following CMDT record
Finalizer
I am not seeing Finalized debug log from Finalizer class. Also please confirm whether we need to write multiple DML statements in the helper class.
Please do share some sample codes/videos related to DML finalizers using helper classes.
Thank you for your help.
Thanks,
Sethu.
Your code looks to be correct. If you are not seeing the finalizer being invoked, it's likely because there are cascading DML operations on other sObjects for which the Trigger Actions Framework is not enabled. Check the state of the TriggerBase.contextStack
in the checkForEndOfTriggerProcessing method to confirm.
https://github.com/mitchspano/apex-trigger-actions-framework?tab=readme-ov-file#universal-adoption
Also for future reference, please use backticks to highlight your code - I have updated your original comment using this.
Dear @mitchspano ,
Thank you for your explanation. But I haven't written any other trigger in any other sobjects. But I have noticed couple of triggers on Account and Opportunity from a managed package.
I have also verified the contextstack, it never entered into the below if block.
if (TriggerBase.contextStack.isEmpty() && rowsLeftToProcess == 0) {
rowsLeftToProcess = null;
existingDmlRowsOffset = getDmlRows();
system.debug('## suppressFinalizeExecution ## ' + suppressFinalizeExecution);
if (!suppressFinalizeExecution) {
instanceToFinalize.finalizeDmlOperation();
instanceToFinalize = null;
}
}
TriggerBase.contextStack.isEmpty() always return false and rowsLeftToProcess always return -1.
Please let me know your suggestion.
Thanks,
Sethu
The managed package trigger on opportunity will prevent the system from being able to detect when to finalize. Multiple triggers interfere with the context stack and the rows left to process count.