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
XIOXIO 

Need Help Duplicate Record Trigger

Hello,

I need the trigger below to only find the duplicate record if the Status__c is 'Active' on both records. Status__c is a picklist field. Any assistance is greatly appreciated!
trigger DuplicateExecutiveContact on Committee_Board_Member__c (before insert. before update) {
        for(Committee_Board_Member__c c:Trigger.new)
        {
      
            List<Committee_Board_Member__c> comm=[select ID from Committee_Board_Member__c where Account__c=:c.Account__c and Committee_Board__c=:c.Committee_Board__c and Service_Discipline__c=:c.Service_Discipline__c and Status__c=:c.Status__c];
            if(comm.size()>0)
            {
                c.adderror('Duplicate Record');
            }
        }
    }

 
Best Answer chosen by XIO
SabrentSabrent
Try adding a if statement after FOR loop


trigger DuplicateExecutiveContact on Committee_Board_Member__c (before insert. before update) {
        for(Committee_Board_Member__c c:Trigger.new)
        {

            if (c. Status__c == true){
            
            List<Committee_Board_Member__c> comm=[select ID from 
                     Committee_Board_Member__c where Account__c=:c.Account__c and 
                     Committee_Board__c=:c.Committee_Board__c and 
                     Service_Discipline__c=:c.Service_Discipline__c and 
                     Status__c=:c.Status__c];
            
            }

            if(comm.size()>0)
            {
                c.adderror('Duplicate Record');
            }
        }
    }

 

All Answers

Naren9Naren9
Give a try with this code:
I don't have the Committe Board Member Object in my Org to test.

trigger DuplicateExecutiveContact on Committee_Board_Member__c (before insert. before update) {
             for(Committee_Board_Member__c c:Trigger.new)
             {
           
                 List<Committee_Board_Member__c> matchingCommittemembers=[select ID from Committee_Board_Member__c where Account__c=:c.Account__c and Committee_Board__c=:c.Committee_Board__c and Service_Discipline__c=:c.Service_Discipline__c and Status__c=:c.Status__c];
                 
                 if(!matchingCommittemembers.isEmpty())
                    {
                    for (Committee_Board_Member__c c1 : matchingCommittemembers)
                     {
                    if(c1.Status__c == c.Status__c)
                    {
                    c.adderror('Duplicate Record');
                    }
                            }
                    }

                 
             }
    }

Thanks,
Naren
SabrentSabrent
Try adding a if statement after FOR loop


trigger DuplicateExecutiveContact on Committee_Board_Member__c (before insert. before update) {
        for(Committee_Board_Member__c c:Trigger.new)
        {

            if (c. Status__c == true){
            
            List<Committee_Board_Member__c> comm=[select ID from 
                     Committee_Board_Member__c where Account__c=:c.Account__c and 
                     Committee_Board__c=:c.Committee_Board__c and 
                     Service_Discipline__c=:c.Service_Discipline__c and 
                     Status__c=:c.Status__c];
            
            }

            if(comm.size()>0)
            {
                c.adderror('Duplicate Record');
            }
        }
    }

 
This was selected as the best answer
Naren9Naren9
In the above code, I forgot to add the Status = Active.
 if((c1.Status__c == c.Status__c ) && (c1.Status__c=='Active'))
                    {
                    c.adderror('Duplicate Record');
                    }


Thanks,
Naren
XIOXIO
Thank you Rov and Naren!!!