- Notifications
You must be signed in to change notification settings - Fork108
Description
By way of example, I've excerpted from the samplecode DomainOpportunities.cls.
Is this by design?
IfonValidate() places an error viaopp.Type.addError(error(msg,opp,fieldToken)) on some record in the batch, say record 22, then when theonAfterInsert comes along, the Accounts related to all opportunities in the batch including record 22, will go through the update viauow.commitWork().
This seems flawed, - if there is aonValidate() handler error, the affected records should be excluded in the sample code from inclusion inaccountIds.
It would be nice if there were an easy way to communicate the erroneous records between handlers in the framework, but theerrors(...) method only saves the errors in a list, not a map keyed by therecord.ID.
I could, I guess, introduce my ownerror(...) into a subclass offflib_SObjectDomain that in turn is extended byOpportunities.cls. My version oferror(..) would capture the errorneous record id in a map for inspection by theonAfterInsert andonAfterUpdate handlers.
But I'm wondering if I'm missing something provided by the Domain layer that would obviate this seemingly basic feature.
public override void onValidate(Map<Id,SObject> existingRecords) { // Validate changes to Opportunities for(Opportunity opp : (List<Opportunity>) Records) { Opportunity existingOpp = (Opportunity) existingRecords.get(opp.Id); if(opp.Type != existingOpp.Type) { opp.Type.addError( error('You cannot change the Opportunity type once it has been created.', opp, Opportunity.Type) ); } } } public override void onAfterInsert() { // Related Accounts List<Id> accountIds = new List<Id>(); for(Opportunity opp : (List<Opportunity>) Records) if(opp.AccountId!=null) // ?! what about excluding the opps that had their type changed ? accountIds.add(opp.AccountId); // Update last Opportunity activity on the related Accounts (via the Accounts domain class) fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(new Schema.SObjectType[] { Account.SObjectType }); Accounts accounts = new Accounts([select Id, Name from Account where id in :accountIds]); accounts.updateOpportunityActivity(uow); uow.commitWork(); }