Salesforce trigger could allow one parent will have only one child and same parent doesn't allow duplicates

Trigger Code :
----------------
trigger restrictChildDuplicates on Contact (before Insert) {
    if(System.trigger.isInsert && System.trigger.isBefore){
        restrictContactDuplicateHelper.avoidDuplicateRecords(trigger.new);
    }
}

Helper Apex Class :
-----------------------
    public class restrictContactDuplicateHelper{
    public restrictContactDuplicateHelper(){
 
    }
    public static set<Id> accId = new set<Id>();
    public static map<Id, Set<Decimal>> accConMap = new map<Id, Set<Decimal>>();
    public static void avoidDuplicateRecords(List<Contact> conLis){
        for(Contact con : conLis){
            if(con.AccountId != null){
                accConMap.put(con.AccountId, new set<Decimal>());
            }
       }
       if(!accConMap.isEmpty()){
            for(Contact con : [Select Id,Priority__c,AccountId from Contact where AccountId =: accConMap.keySet()]){
                accConMap.get(con.AccountId).add(con.Priority__c);
            }
      }
      if(!accConMap.isEmpty()){
        for(Contact conRec : conLis){
             if(accConMap.containsKey(conRec.AccountId)){              
                 Set<Decimal> conSet = accConMap.get(conRec.AccountId);
                 if(conSet.contains(conRec.Priority__c)){
                     conRec.Priority__c.addError('Priority was registered already for the Account.');
                }
            }
         }
      }
    }
}

Featured

What is Cryptography in salesforce and what are all the algorithms provided by them ?

A). It is a security protocal between two systems. Lets say we are integration two systems without any encrytion mechanism then hackers wil...

Popular