0

I have Account object as my master and have created a contract object which is a child of Account. I am trying to restrict the duplicate creation of contract records based on pick list values(category ,subcategory) under the same account records i.e. one account record can't have more than one contract records of same category and subcategory.

trigger duplicateRecords on Contract_Details__c (before Insert,before Update) {
    List<Contract_Details__c> oldValueUpdate = new List<Contract_Details__c>();
    List<Contract_Details__c> contrct=new List<Contract_Details__c>();
    Map<String,String> compareValues=new Map<String,String>();
    Set<ID> ids=new Set<ID>(); 
    If(Trigger.isInsert){
        For(Contract_Details__c cdet: Trigger.New){
            if(cdet.Product_Sub_Category__c == null){
                cdet.addError('Sub-Category cant be left blank.');
            }
            else{           
                ids.add(cdet.Customer__c); 
            }
        }            
        contrct=[select Id,Name,Brand__c,Product_Sub_Category__c,Customer__c from Contract_Details__c where Customer__c in :ids];
        System.debug('###########'+contrct.size());
        if(contrct.size() > 1){
             For(Contract_Details__c cont : contrct){
                For(Contract_Details__c con : Trigger.New)
                {                                 
                    if(cont.Brand__c == con.Brand__c && cont.Product_Sub_Category__c == con.Product_Sub_Category__c){
                        con.addError('Duplicate Contract');
                    }
                }
            }
        }
     }    

It is not working as per the requirement and i am not able to find why?

1

Let me give it a shot. There are few issues with the code. Mainly its not bulkified to handle inserting of multiple contacts across same/different customers in bulk. So whats bound to happen is if 2 contracts are inserted across 2 Accounts respectively (Call them ContractC on Account1 and ContractD on Account2)

Now consider both Account1 & Account2 already have ContractA and ContractB respectively earlier. And Brand and SubCategory for ContractA matches with ContractD & Brand and SubCategory for ContractB matches with ContractC. Still it will tag them as duplicate though they are non-duplicate.

So can you try out this snippet and let me know.


trigger duplicateRecords on Contract_Details__c (before Insert,before Update) {
List<Contract_Details__c> oldValueUpdate = new List<Contract_Details__c>();
Map<Id,List<Contract_Details__c>> contractsByAccountMap=new List<Contract_Details__c>();
Map<String,String> compareValues=new Map<String,String>();
Set<ID> ids=new Set<ID>(); 
if(Trigger.isInsert){
    For(Contract_Details__c cdet: Trigger.New){
        if(cdet.Product_Sub_Category__c == null){
            cdet.addError('Sub-Category cant be left blank.');
        }
        else{           
            ids.add(cdet.Customer__c); 
        }
    }            
    for(Contract_Details__c cont :[select Id,Name,Brand__c,Product_Sub_Category__c,Customer__c from Contract_Details__c where Customer__c in :ids]){
        if(contractsByAccountMap.containskey(cont.Customer__c)){
            List<Contract_Details__c> contrct=new List<Contract_Details__c>();
            contrct = contractsByAccountMap.get(cont.Customer__c);
            contrct.add(cont);
            contractsByAccountMap.put(cont.Customer__c,contrct);
        } else{ 
            List<Contract_Details__c> contrct=new List<Contract_Details__c>();
            contrct.add(cont);
            contractsByAccountMap.put(cont.Customer__c,contrct);    
        }
    }
    System.debug('###########'+contrct.size());
    if(!contractsByAccountMap.IsEmpty()){
        for(Contract_Details__c con : Trigger.New)
        {                                 
            if(contractsByAccountMap.containskey(con.Customer__c)){
                List<Contract_Details__c> contrct=new List<Contract_Details__c>();
                contrct = contractsByAccountMap.get(con.Customer__c);
                if(!contrct.IsEmpty()){
                    for(Contract_Details__c cn : contrct){
                        if(cn.Brand__c == con.Brand__c && cn.Product_Sub_Category__c == con.Product_Sub_Category__c){
                            con.addError('Duplicate Contract');
                        }
                    }
                }
            }
        }
    }
}

}


thanks,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.