AfterUpdate.Bypassable
Skip a handler for the whole chunk when a condition holds, such as a static flag, a batch job or a custom permission.
Signature
apex
public interface Bypassable {
Boolean bypassOnAfterUpdateWhen();
}bypassOnAfterUpdateWhen(): called once per chunk, before parents load. Returnstrueto skip this handler for this chunk.
Example
apex
public with sharing class ContactWriter implements AfterUpdate.Writer, AfterUpdate.Bypassable {
public static Boolean isBypassed = false;
public Boolean bypassOnAfterUpdateWhen() {
return ContactWriter.isBypassed;
}
public Boolean writeOnAfterUpdateWhen(TriggerTypes.UpdateRecord record) {
return record.isChanged(Contact.AccountId);
}
public void writeOnAfterUpdate(TriggerTypes.UpdateRecord record, TriggerTypes.UnitOfWork unitOfWork) {
unitOfWork.toInsert(new Task(WhatId = ((Contact) record.getNewSObject()).AccountId, Subject = 'Review contact'));
}
}cls
public with sharing class AccountOwnerTransferWriter implements AfterUpdate.Writer, AfterUpdate.ParentQuery, AfterUpdate.PriorParentQuery, AfterUpdate.RelatedQuery, AfterUpdate.Bypassable {
public static Boolean isBypassed = false;
public Boolean bypassOnAfterUpdateWhen() {
return AccountOwnerTransferWriter.isBypassed || System.isBatch();
}
public Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnAfterUpdate() {
return new Map<SObjectField, TriggerTypes.ParentFields>{ Account.OwnerId => TriggerTypes.ParentFields.with(User.Name) };
}
public Map<SObjectField, TriggerTypes.ParentFields> queryPriorParentsOnAfterUpdate() {
return new Map<SObjectField, TriggerTypes.ParentFields>{ Account.OwnerId => TriggerTypes.ParentFields.with(User.Name) };
}
public Map<String, AfterUpdate.RecordsProvider> queryRelatedOnAfterUpdate() {
return new Map<String, AfterUpdate.RecordsProvider>{ 'openOpportunities' => new AccountOpenOpportunitiesProvider() };
}
public Boolean writeOnAfterUpdateWhen(TriggerTypes.UpdateRecord record) {
return record.isChanged(Account.OwnerId);
}
public void writeOnAfterUpdate(TriggerTypes.UpdateRecord record, TriggerTypes.UnitOfWork unitOfWork) {
Account newAccount = (Account) record.getNewSObject();
User oldOwner = (User) record.getOldParent(Account.OwnerId);
User newOwner = (User) record.getNewParent(Account.OwnerId);
String handoverNote = Date.today().format() + ' - account handed over from ' + oldOwner?.Name + ' to ' + newOwner?.Name;
for (SObject row : record.getRelated('openOpportunities').getAllWhereKeyEquals(record.getId())) {
Opportunity opportunity = (Opportunity) row;
unitOfWork.toUpdate(new Opportunity(Id = opportunity.Id, OwnerId = newAccount.OwnerId, Description = this.notedDescription(handoverNote, opportunity.Description)));
}
}
private String notedDescription(String handoverNote, String description) {
return String.isBlank(description) ? handoverNote : handoverNote + '\n' + description;
}
private with sharing class AccountOpenOpportunitiesProvider implements AfterUpdate.RecordsProvider {
public List<SObject> query(TriggerTypes.UpdateRecords records) {
return [
SELECT Id, AccountId, Description
FROM Opportunity
WHERE AccountId IN :records.getIds() AND IsClosed = FALSE
];
}
public String keyOf(SObject row) {
return ((Opportunity) row).AccountId;
}
}
}Rules
- Reset static flags in
finally. A static flag lasts for the whole transaction, nested saves included. - Outside the error handling. An exception here is not logged, ContinueOnError does not apply, and the update fails.
- Only this context. Apex bypasses cover every context, and
bypass().handler(X.class)never matches an inner class. ATriggerHandler__mdtrecord covers the one context it names, for every user.
TIP
To skip single records, return false from the predicate.
