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
apex code shareapex code share 

Please help me writing a trigger for preventing duplicate record to be saved on custom object.

Hi,

 

I have a custom object name "Product Segments"  and when someone click new to enter new product segments then it should check whether the record already exist or not ,,if exist then it should throw an error 

 

trigger should check on these field "Product Segment Name",,"Product Segment Code" and "Product Category Name".

 

if any record consist of same values in above field which already exists in system then it should not be saved. 

Best Answer chosen by Admin (Salesforce Developers) 
Chamil MadusankaChamil Madusanka

Nilesh also correct.

 

Here is code for your requirement.

 

trigger CheckForDuplicate on Product_Segment__c (before insert)
{
	Set<String> psName = new Set<String>();
	Set<String> psCode = new Set<String>();
	Set<String> categoryName = new Set<String>();
	List<Product_Segment__c> existingPS = new List<Product_Segment__c>();
	for(Product_Segment__c tempPS : Trigger.new)
	{
		psName.add(tempPS.Product_Segment_Name__c);
		psCode.add(tempPS.Product_Segment_Code__c);
		categoryName.add(tempPS.Product_Category_Name__c);
	}
	
	existingPS = [SELECT Id,
						 Product_Segment_Name__c, 
					     Product_Segment_Code__c, 
						 Product_Category_Name__c 
				  FROM Product_Segment__c 
				  WHERE Product_Segment_Name__c :IN psName
				  AND Product_Segment_Code__c :IN psCode
				  AND Product_Category_Name__c :IN categoryName];
				  
	for(Product_Segment__c tempPS : Trigger.new)
	{
		for(Product_Segment__c existPS : existingPS)
		{
			if(tempPS.Product_Segment_Name__c == existPS.Product_Segment_Name__c 
			   && tempPS.Product_Segment_Code__c == existPS.Product_Segment_Code__c
			   && tempPS.Product_Category_Name__c == existPS.Product_Category_Name__c)
			{
				tempPS.addError('Duplicate Record!!!!!');
			}
		}
	}
	
}

     


 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

All Answers

Nilesh ManeNilesh Mane

You can do it using validation rules also. Its easy.

Nilesh ManeNilesh Mane

Another way is.. go to Product Segments object. Go to Product Segment Name. Click on "Edit". And check the checkbox for unique

UniqueDo not allow duplicate values
 
  • Treat "ABC" and "abc" as duplicate values (case insensitive)
  • Treat "ABC" and "abc" as different values (case sensitive)

 

You can do this for remaining two fields also.No need to write validation /Trigger.

Chamil MadusankaChamil Madusanka

Nilesh also correct.

 

Here is code for your requirement.

 

trigger CheckForDuplicate on Product_Segment__c (before insert)
{
	Set<String> psName = new Set<String>();
	Set<String> psCode = new Set<String>();
	Set<String> categoryName = new Set<String>();
	List<Product_Segment__c> existingPS = new List<Product_Segment__c>();
	for(Product_Segment__c tempPS : Trigger.new)
	{
		psName.add(tempPS.Product_Segment_Name__c);
		psCode.add(tempPS.Product_Segment_Code__c);
		categoryName.add(tempPS.Product_Category_Name__c);
	}
	
	existingPS = [SELECT Id,
						 Product_Segment_Name__c, 
					     Product_Segment_Code__c, 
						 Product_Category_Name__c 
				  FROM Product_Segment__c 
				  WHERE Product_Segment_Name__c :IN psName
				  AND Product_Segment_Code__c :IN psCode
				  AND Product_Category_Name__c :IN categoryName];
				  
	for(Product_Segment__c tempPS : Trigger.new)
	{
		for(Product_Segment__c existPS : existingPS)
		{
			if(tempPS.Product_Segment_Name__c == existPS.Product_Segment_Name__c 
			   && tempPS.Product_Segment_Code__c == existPS.Product_Segment_Code__c
			   && tempPS.Product_Category_Name__c == existPS.Product_Category_Name__c)
			{
				tempPS.addError('Duplicate Record!!!!!');
			}
		}
	}
	
}

     


 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

This was selected as the best answer
apex code shareapex code share

Thank a lot for your help :-)

 

but i'm getting error Error: Compile Error: unexpected token: ':'

 

for this part :- 

WHERE Product_Segment_Name__c :IN psName
				  AND Product_Segment_Code__c :IN psCode
				  AND Product_Category_Name__c :IN categoryName]
Nilesh ManeNilesh Mane

Try this....

 

WHERE Product_Segment_Name__c  IN :psName
  AND Product_Segment_Code__c  IN :psCode
  AND Product_Category_Name__c  IN :categoryName]

apex code shareapex code share

Thanks!!

 

I used =: and it worked :-)

WHERE Product_Segment_Name__c  IN =:psName
  AND Product_Segment_Code__c  IN =:psCode
  AND Product_Category_Name__c  IN =:categoryName]

Chamil MadusankaChamil Madusanka

Sorry, my bad

 

IT must be IN: instead of :IN

 

Use following

 

WHERE Product_Segment_Name__c :IN psName
				  AND Product_Segment_Code__c IN: psCode
				  AND Product_Category_Name__c IN: categoryName]