You need to sign in to do that
Don't have an account?
SF Dev Ceno
Apex Validation Rule On Account Based On Related Opportunity Fields
All,
I have written two methods on Account for validation rules based on related Opportunity field values. My debug log is returning values in both lists in the methods when it should only return values in one method. Basically, both methods are firing and therefore it's just defaulting to second method's error messages. This means that my conditions for the related Opportunity fields are returning false. Can someone see why?
Line 26 and 60
I have written two methods on Account for validation rules based on related Opportunity field values. My debug log is returning values in both lists in the methods when it should only return values in one method. Basically, both methods are firing and therefore it's just defaulting to second method's error messages. This means that my conditions for the related Opportunity fields are returning false. Can someone see why?
Line 26 and 60
public with sharing class WR_AccountValidationRules { Map<Id, Account> oldAccts; Map<Id, Account> newAccts; Set<Id> acctIds = new Set<Id>(); public WR_AccountValidationRules(Map<Id, Account> oldTriggerAccts, Map<Id, Account> newTriggerAccts) { oldAccts = oldTriggerAccts; newAccts = newTriggerAccts; } public void executeOrderProvValidation(){ Map<Id, Account> acctMapToUpdate = new Map<Id, Account>(); Map<Id, Opportunity> oppByAccId = new Map<Id, Opportunity>([Select Id, AccountId, Order_Provisioning_Status__c, StageName From Opportunity]); for(Opportunity opp : oppByAccId.values()){ if(opp.StageName != '5. Closed-Won'&& (opp.Order_Provisioning_Status__c == 'Provisioning And Billing - In Progress' || opp.Order_Provisioning_Status__c == 'Order Entry - Complete')){ acctIds.add(opp.AccountId); } } System.debug('!= Stage 5 size acctIds '+ acctIds.size()); Map<Id, Account> acctMap = new Map<Id, Account>([Select Id, Name, Type, RecordTypeId From Account Where Id in :acctIds]); System.debug('!= Stage 5 size acctMap '+ acctMap.size()); for(Id oppId : oppByAccId.keySet()){ Id acctId = oppByAccId.get(oppId).AccountId; acctMapToUpdate.put(oppId, acctMap.get(acctId)); } System.debug('!= Stage 5 size acctMapToUpdate '+ acctMapToUpdate.size()); for(Account a : newAccts.values()){ if(oldAccts.get(a.Id).Type != newAccts.get(a.Id).Type){ a.addError('Account Type cannot be modified while a related Opportunity has an Order Provisioning Status of Provisioning And Billing - In Progress OR Order Entry - Complete.'); } else if(oldAccts.get(a.Id).RecordTypeId != newAccts.get(a.Id).RecordTypeId){ a.addError('Account Record Type cannot be modified while a related Opportunity has an Order Provisioning Status of Provisioning And Billing - In Progress OR Order Entry - Complete.'); } } } public void executeStage5Validation(){ Map<Id, Account> acctMapToUpdate = new Map<Id, Account>(); Map<Id, Opportunity> oppByAccId = new Map<Id, Opportunity>([Select Id, AccountId, Order_Provisioning_Status__c, StageName From Opportunity]); for(Opportunity opp : oppByAccId.values()){ if(opp.Id != null && opp.StageName == '5. Closed-Won' && (opp.Order_Provisioning_Status__c == 'Order Entry – In Progress' || opp.Order_Provisioning_Status__c == 'Order Entry – On Hold' || opp.Order_Provisioning_Status__c == 'Order Entry – Complete')){ acctIds.add(opp.AccountId); } } System.debug('== Stage 5 size acctIds '+ acctIds.size()); Map<Id, Account> acctMap = new Map<Id, Account>([Select Id, Name, Type, RecordTypeId From Account Where Id in :acctIds]); System.debug('== Stage 5 size acctMap '+ acctMap.size()); for(Id oppId : oppByAccId.keySet()){ Id acctId = oppByAccId.get(oppId).AccountId; acctMapToUpdate.put(oppId, acctMap.get(acctId)); } System.debug('== Stage 5 size acctMapToUpdate '+ acctMapToUpdate.size()); for(Account a : newAccts.values()){ if(oldAccts.get(a.Id).Type != newAccts.get(a.Id).Type){ a.addError('Account Type cannot be modified while a related Opportunity is in Stage 5 and has an Order Provisioning Status of Order Entry – In Progress, Order Entry – On Hold, or Order Entry – Complete.'); } else if(oldAccts.get(a.Id).RecordTypeId != newAccts.get(a.Id).RecordTypeId){ a.addError('Account Record Type cannot be modified while a related Opportunity is in Stage 5 and has an Order Provisioning Status of Order Entry – In Progress, Order Entry – On Hold, or Order Entry – Complete.'); } } } }Trigger:
trigger WR_MasterAccountTrigger on Account ( before insert, after insert, before update, after update, before delete, after delete) { // to exclude data migration if(Utility_Class.isDM()){ return; } if(system.isBatch() || system.isFuture()) return; if (Trigger.isBefore) { if (Trigger.isInsert) { // Call class logic here! } if (Trigger.isUpdate) { // Call class logic here! new WR_AccountValidationRules(trigger.oldMap, trigger.newMap).executeOrderProvValidation(); new WR_AccountValidationRules(trigger.oldMap, trigger.newMap).executeStage5Validation(); } if (Trigger.isDelete) { // Call class logic here! } } if (Trigger.IsAfter) { if (Trigger.isInsert) { // Call class logic here! } if (Trigger.isUpdate) { // Call class logic here! } if (Trigger.isDelete) { // Call class logic here! } } }