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?