function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zach AckermanZach Ackerman 

Overlap Trigger for related list.

I have a related list on the account object called Broker_Commission_Code__c. I want to ensure that broker commission codes do not overlap. I need a trigger that will check for that specific account what broker code records exist and what their effective_date__c and termination_date__c. An example would be record 1 effective date = 1/1/16 and term date = 1/31/2016 Record 2 Effective date= 1/1/2016. I would need the trigger to stop from the second record being created with that date. The earliest the second record could exist is 2/1/2016. 

Thanks!
R Z KhanR Z Khan
trigger YourTrigger on Broker_Commission_Code__c(before insert, before update){
Set<ID> accountIds =new Set<Id>();
for(Broker_Commission_Code__c broker:trigger.new){
    accountIds.add(broker.AccountId);
}

List<Broker_Commission_Code__c> brokers = [select effective_date__c,termination_date__c from Broker_Commission_Code__c WHERE AccountId IN :accountIds ];

for(Broker_Commission_Code__c broker:trigger.new){
   for(Broker_Commission_Code__c br){
    if(broker.effective_date__c == br.effective_date__c){
    broker.addError('Your error');
break;
}

if(broker.termination_date__c == br.termination_date__c){
   broker.addError('Your error');
break;
}
}
}
}

 
R Z KhanR Z Khan
Dont forget to update the trigger name adn object/field names as necessary. This trigger will add an error  if effective date or termination date are the same. 
Oh, i forgot ot add the check for acccountId. Replace if(broker.effective_date__c == br.effective_date__c) with if(broker.effective_date__c == br.effective_date__c && broker.accountId == br.accountId). Do the same for termination date please
Zach AckermanZach Ackerman
I am getting an error on line 10. It says I need an equal sign. Do you need to set the br variable pervious to line 10?