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
Developer129Developer129 

Duplicate Validation Based on Two Fields

Hi,

 

Is there a way in salesforce to do duplicate validation based on two object fields

 

Thanx

 

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

Create a third field on the object perhaps called "unique check" or something like that.  Define it to be Unique.  You don't need to show it on the page layout, etc.

 

Use a workflow rule which fires whenever the object is changed (or whatever is appropriate for your case), and use a field update which sets the "unique" field with a concatenation of some kind of the two fields you want to check.

 

So, if a user enters a record with the same two field values as an existing record, the workflow would fire, the field update would be done, and there would be a uniqueness violation.

 

Best, Steve.

All Answers

Ispita_NavatarIspita_Navatar

Yes you can do it via a trigger, which fires on before insert event and before update. In the code you can check if either of these field value exist if yes, then raise an error something like below:-

sObj[] Arr=[select id, name from SObj where field1=: trigger.new[0].field1 or field2=: trigger.new[0].field2];

if(Arr.size()>0)

{

   trigger.new.field1.addError('Duplicate value');

}

 

Did this answer your question? if so, please mark it solved.

SteveBowerSteveBower

Create a third field on the object perhaps called "unique check" or something like that.  Define it to be Unique.  You don't need to show it on the page layout, etc.

 

Use a workflow rule which fires whenever the object is changed (or whatever is appropriate for your case), and use a field update which sets the "unique" field with a concatenation of some kind of the two fields you want to check.

 

So, if a user enters a record with the same two field values as an existing record, the workflow would fire, the field update would be done, and there would be a uniqueness violation.

 

Best, Steve.

This was selected as the best answer