AfterUpdate.ContinueOnError
Log and swallow the handler's exceptions, so later handlers still run and the update goes on.
Signature
apex
public interface ContinueOnError {
}AfterUpdate.ContinueOnError has no methods: implementing it is the whole opt-in.
Example
apex
public with sharing class ContactWriter implements AfterUpdate.Writer, AfterUpdate.ContinueOnError {
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 OpportunityWinTaskWriter implements AfterUpdate.Writer, AfterUpdate.ParentQuery, AfterUpdate.ContinueOnError {
public Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnAfterUpdate() {
return new Map<SObjectField, TriggerTypes.ParentFields>{ Opportunity.AccountId => TriggerTypes.ParentFields.with(Account.Name) };
}
public Boolean writeOnAfterUpdateWhen(TriggerTypes.UpdateRecord record) {
return record.isChangedTo(Opportunity.StageName, 'Closed Won');
}
public void writeOnAfterUpdate(TriggerTypes.UpdateRecord record, TriggerTypes.UnitOfWork unitOfWork) {
Opportunity newOpportunity = (Opportunity) record.getNewSObject();
Account account = (Account) record.getNewParent(Opportunity.AccountId);
unitOfWork.toInsert(
new Task(
WhatId = record.getId(),
OwnerId = newOpportunity.OwnerId,
Subject = 'Onboarding kick-off: ' + (account?.Name ?? newOpportunity.Name),
Description = 'Won on ' + Date.today().format() + '. Agree the implementation plan with the customer.',
ActivityDate = Date.today().addDays(7),
Priority = 'High',
Status = 'Not Started'
)
);
}
}cls
public with sharing class AccountAddressCascadeWriter implements AfterUpdate.Writer, AfterUpdate.RelatedQuery, AfterUpdate.ContinueOnError, AfterUpdate.RecursionGuard {
public Integer maxRunsPerRecordOnAfterUpdate() {
return 1;
}
public Map<String, AfterUpdate.RecordsProvider> queryRelatedOnAfterUpdate() {
return new Map<String, AfterUpdate.RecordsProvider>{ 'contacts' => new AccountContactsProvider() };
}
public Boolean writeOnAfterUpdateWhen(TriggerTypes.UpdateRecord record) {
return record.isAnyChanged(Account.BillingStreet, Account.BillingCity, Account.BillingState, Account.BillingPostalCode, Account.BillingCountry);
}
public void writeOnAfterUpdate(TriggerTypes.UpdateRecord record, TriggerTypes.UnitOfWork unitOfWork) {
Account newAccount = (Account) record.getNewSObject();
Account oldAccount = (Account) record.getOldSObject();
for (SObject row : record.getRelated('contacts').getAllWhereKeyEquals(record.getId())) {
Contact contact = (Contact) row;
if (!this.mailingFollowedAccount(contact, oldAccount)) {
continue;
}
unitOfWork.toUpdate(
new Contact(
Id = contact.Id,
MailingStreet = newAccount.BillingStreet,
MailingCity = newAccount.BillingCity,
MailingState = newAccount.BillingState,
MailingPostalCode = newAccount.BillingPostalCode,
MailingCountry = newAccount.BillingCountry
)
);
}
}
private Boolean mailingFollowedAccount(Contact contact, Account oldAccount) {
if (String.isBlank(contact.MailingStreet) && String.isBlank(contact.MailingCity)) {
return true;
}
return contact.MailingStreet == oldAccount.BillingStreet &&
contact.MailingCity == oldAccount.BillingCity &&
contact.MailingPostalCode == oldAccount.BillingPostalCode &&
contact.MailingCountry == oldAccount.BillingCountry;
}
private with sharing class AccountContactsProvider implements AfterUpdate.RecordsProvider {
public List<SObject> query(TriggerTypes.UpdateRecords records) {
return [
SELECT Id, AccountId, MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry
FROM Contact
WHERE AccountId IN :records.getIds()
];
}
public String keyOf(SObject row) {
return ((Contact) row).AccountId;
}
}
}Rules
- The handler stops. After an exception, its remaining records and its Finalizer are skipped.
- A Writer gets the automatic unit of work. Its failure never touches the other Writers' writes. The unit commits right after the Writer, before the shared unit of work. A Writer that also implements OwnUnitOfWork uses that unit instead.
- A Dispatcher keeps what it did. A job already enqueued or an event already published stays.
- Some errors still fail the update. Library exceptions,
System.LimitException, theFinalExceptionfrom writing a trigger row and exceptions outside the handler's own methods, such asbypassOnAfterUpdateWhen()or the final commit, are never swallowed.
TIP
Add a Logger. Without one, a swallowed exception leaves no trace.
