our sales team is using a custom object Exception_Tracking__c
to keep track of various exceptions on the Opportunity
before it becomes a deal. There's a lookup to Opportunity in the Exception_Tracking__c
object, but not an Exception_Tracking__c
lookup on the Opportunity
(just a related list).
A really pointless field that Sales just has to have is Opportunity.Exception_NA__c
(boolean).
If there aren't any Exceptions for a specific Opportunity, they'll mark that as TRUE. However, they also want validation in place to prevent a user from marking the field as TRUE if there are any related Exception_Tracking__c
records. Trying to figure out how to do this.
My understanding is that standard validation won't work and I would need to use Apex. I'm thinking I would need to do a Map of some kind, but just don't know how to tie it all together. My failed attempt:
trigger OpportunityExceptionTrigger on Opportunity (before insert, before update) {
Map<Id,Exception_Tracking__c> exceptionRelatedFields=new Map<Id,Exception_Tracking__c>(
[Select Opportunity__r.Id
from Exception_Tracking__c
where Opportunity__c IN:Trigger.New]);
For(Opportunity opp : trigger.new){
If(opp.Exception_NA__c == TRUE && exceptionRelatedFields.get(opp.Id) != null){
opp.addError('Because there are Exception Tracking records submitted for this Opportunity, the Exception NA field cannot be marked as true');
}
}
}