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
Songtao ShiSongtao Shi 

How to restrict duplicated child record if it's existing

Hi Guys,

I have two customized objects called 'Conference' (Parent) and 'Attendee' (Child). In the object of 'Attendee', we only have one field called Name which our SF users can lookup their names and register the conference. Now, i don't want user can register a same conference more than once. How can i do that? This is my first time to write a trigger.  Any help is greatly appreciated.  Thank you.
Rohit Sharma 66Rohit Sharma 66
Please try following code, it will check duplicate records based on Contact's Email field, u can add your own field as per requirement
Replace Account with with your Parent Object
Replace Contact with your child object
 
trigger uniqueContactForAccount on Contact (before insert) {
    set<Id> accId = new set<Id>();
    map<Id, Set<String>> accConMap = new map<Id, Set<string>>();
    for(Contact con : trigger.new){
        if(con.AccountId != null){
           // accId.add(con.AccountId);
            accConMap.put(con.AccountId, new set<String>());
            system.debug('************'+accConMap);
        }
    }
    
    if(!accConMap.isEmpty()){
        for(Contact con : [Select Id, FirstName, LastName, email, AccountId from Contact where AccountId =: accConMap.keySet()]){
            accConMap.get(con.AccountId).add(con.email);
        }
        system.debug('************'+accConMap);
    }
    
    if(!accConMap.isEmpty()){
        for(Contact conRec : trigger.new){
            system.debug('************'+conRec.Email);
             Set<String> conSet = accConMap.get(conRec.AccountId);
             system.debug('************'+conSet);
             if(conSet.contains(conRec.Email)){
                 system.debug('************'+conRec.Email);
                     conRec.Email.addError('Duplicate user');
             }  
        }
    }
}
Please let me know if it works.
David Catindoy 101David Catindoy 101
Is your Name field a custom lookup field? There are actually lots of ways in order to avoid duplication without using any trigger (using config only).
Songtao ShiSongtao Shi
Thanks, Rohit. I will test this trigger.
Songtao ShiSongtao Shi
Thanks, David, the Name field is a custom lookup field that relates to 'User'.    Can you show one example of using config? Thanks.
David Catindoy 101David Catindoy 101
Sounds a bit more complicated than what I expected. But to give you an insight, here's a sample article (http://starrforce.com/2012/05/preventing-salesforce-duplicates/) that you might find useful. And if you wanted to push the trigger solution, then here's what I have created for you to solve your issue:
 
trigger AvoidDuplicateAttendee on Attendee__c (before insert, before update) 
{
    Set<String> setCombinedID = new Set<String>(); 
    for(Attendee__c attendee : [Select Name__c, Conference__c FROM Attendee__c WHERE Name__c != null AND Conference__c != null]){
        setCombinedID.add(attendee.Name__c+attendee.Conference__c);
    }

    for(Attendee__c newAttendee : Trigger.new)
    {
        if(!setCombinedID.contains(newAttendee.Name__c+newAttendee.Conference__c)){
            setCombinedID.add(newAttendee.Name__c+newAttendee.Conference__c);
        }
        else{
            newAttendee.addError('Attendee was registered already for the conference.');
        }
    }
}