+ Start a Discussion

Duplicate Record Trigger on specific Account ID not entire object



I am looking to create an Apex Trigger that will look for a duplicates, but instead of the Trigger looking for the duplicate in the entire object I want it to only look at the specific Account the Account Team record is being created against.


I currently have the below code, however it is checking against the entire Account_Team_Assignment__c object instead of just the Account ID the Account Team record is being created for. Any ideas on how to limit the code I have below to only look at the specific Account record the Account Team Assignment is being created on for the duplicate?


(The code looks to make sure the Assignment is Active, and then it is checking to make sure a record with the same Business segment does not already exist)



trigger AccountTeamDuplicateTrigger on Account_Team_Assignment__c (before insert,before update) { 
  for (Account_Team_Assignment__c c : Trigger.new){

   Account_Team_Assignment__c[] Account_Team_Assignments= [select id from Account_Team_Assignment__c Where Active__c = TRUE and Business_Segments__c = :c.Business_Segments__c LIMIT 1 ];
   if (Account_Team_Assignments.size() > 0) { 
     c.Representative__c.addError('BDRep cannot be created - An active BD already exists in the Account Team for this Business Segment');