+ Start a Discussion
atidevatidev 

Avoid duplicate in Product

Hi,

  How can i add validation to avoid duplicates in Product. In my scenario, duplicates ahould not exist for the same product code and region combination. ie, Product code and region combination values should be unique.

Thanks.

gm_sfdc_powerdegm_sfdc_powerde

You can write an apex trigger to do this.  But the logic could become really complex to handle bulk insert scenario and you might also run into issues with governor limits as you query for existing records with the same product name and region combination.

 

One approach that might work is to create a hidden field for this purpose and populate it with a hash of product code and region.  You will have to use a "before" trigger to compute the hash and store it there.  By making this field "unique", you can let Salesforce handle the validation for you.  This is the best approach that I can think of.  Let's see if other experts out here can come up with a better approach.

Ritesh AswaneyRitesh Aswaney

create  a custom field, say primary_key__c, check the unique checkbox when creating this field

create a workflow rule, which populates the primary_key__c with the concatenation of the two required fields

 

the workflow field update will fail owing to the uniqueness check if a duplicate value is inserted.