By passing code not working
trilok490 opened this issue · 1 comments
trilok490 commented
I am trying to bypass the action method but the framework isn't working properly, it is not bypassing the trigger action using metadatahandler.bypass, & also not bypassing the whole object when added tour code of triggerbase.bypass.
Can you please check and feedback.
I am following the same examples you have mentioned.
public class TA_Opportunity_StageInsertRules implements TriggerAction.BeforeInsert {
@TestVisible
private static final String PROSPECTING = 'Prospecting';
@TestVisible
private static final String INVALID_STAGE_INSERT_ERROR = 'The Stage must be \'Prospecting\' when an Opportunity is created';
public void beforeInsert(List<Opportunity> newList){
MetadataTriggerHandler.bypass('TA_Opportunity_StageInsertRules');
for (Opportunity opp : newList) {
if (opp.StageName != PROSPECTING) {
opp.addError(INVALID_STAGE_INSERT_ERROR);
}
}
MetadataTriggerHandler.clearBypass('TA_Opportunity_StageInsertRules');
}
}
mitchspano commented
Transactional bypasses prevent future instantiation within the transaction.
You cannot bypass from within the action or trigger you are trying to bypass - once they are instantiated they will be executed.
public class TA_Opportunity_Demo implements TriggerAction.AfterInsert {
public void afterInsert(List<Opportunity> newList){
TriggerBase.bypass('Opportunity');
// will only bypass future Opportunity triggers within the transaction, but won't do anything for the current trigger.
MetadataTriggerHandler.bypass('TA_Opportunity_Demo');
// will only bypass future `TA_Opportunity_Demo` actions within the transaction, but won't do anything for the current action.
throw new IllegalArgumentException('not allowed');
}
Documentation for transactional bypasses is available here in the README