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
anup-prakashanup-prakash 

Related to trigger query

Hi I have a master detail relationship between Product__c and Product_Detail__c and I want to stop duplicate entry in the Product_detail__c and ensure that iff a user is trying to enter data into the Within Product with exixting Name of Detail record then the Quantity field of the detail should be totalled leaving the rest unaltered..

 

Product__c Fields :

Name
Description__c
ManufacturerName__c

----------------------------------

Product_Detail__c Fields:


Name,

Quantity__c,

MRP__c,

Unit_Price__c,

Product__c -> reference

____________________

 

 

anup-prakashanup-prakash

I Am getting an error when adding a duplicate data

 

trigger CheckDuplicateBatchNumber on Prod_Detail__c (before insert, before update) {
	Map<String,Prod_Detail__c> mapProdDetailName = new Map<String,Prod_Detail__c>();
	
	for(Prod_Detail__c pd: Trigger.new){
		if(Trigger.isInsert || !(trigger.oldMap.get(pd.ID).Name.equalsIgnoreCase(pd.Name))){
			if(mapProdDetailName.containskey(pd.Name)){
				pd.Name.addError('Product By this Name already exists');
			}
			mapProdDetailName.put(pd.Name,pd);
		}
	}
	
	System.debug('a');
	List<Prod_Detail__c> listProductDetail = new List<Prod_Detail__c>();
	
	if(!mapProdDetailName.isEmpty()){
		for(Prod_Detail__c pd : Trigger.New){
			for(Prod_Detail__c prod :[Select Name, Quantity__c, MRP__c, Unit_Price__c, Prod__c from Prod_Detail__c where Name IN : mapProdDetailName.keyset()]){
				if((pd.Prod__c == prod.Prod__c)&&(prod.Name.equalsIgnoreCase(pd.Name))){
					
						Prod_Detail__c prodObject = new Prod_Detail__c();
				
						prodObject.Name =  pd.Name;
						prodObject.Prod__c = pd.Prod__c;
						prodObject.MRP__c = prod.MRP__c;
						prodObject.Unit_Price__c = prod.Unit_Price__c;
						prodObject.Quantity__c = prod.Quantity__c + pd.Quantity__c ;
					
						listProductDetail.add(prodObject);
				}
				else{
					continue;
				}
			}
		}
		if(listProductDetail.size() != Null)
		insert listProductDetail;
	}
}

 Please Help..