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
liron169liron169 

Uniqueness validation on several fields

Hello,

 

Need advise if I can achieve the following requirment without writing code.

 

 

I have custom_object and below child custom_object.

In the child object have several custom fields:
customField1
customField2
customField3

 


The requirement is that for each paernt object those fields on all childs will be unique.

example:

 

If I have:
parentObj1
    below:
        childObject1
        childObject2


Then the following fields must be unique:
childObject1.customField1
childObject1.customField2
childObject1.customField3
childObject2.customField1
childObject2.customField2
childObject2.customField3

 


Any suggestion?

liron169liron169

I found kind of solution, but I think it have some limitations, and it will be better to user apex code.

The solution is to create text field on the parent object that will concatene all the relvaent fields
from the childs (with workflow on the child).
Then, add validation rule on the chuild object on each relavent field-
if the concatene field from the parent contains the new value that entering then raise error for duplication.

The limitation:
1.The String field on the parent can be for maximum lenght of 255 character.
If it will be longer, I won't be able to use it in the validations.

2.The contains method will raise error also when I'll try to enter substring of existing fields.
e.g.: if in first field I entered: '12345'.
then in second field I'll enter '123', error will be raised.

truebruceleetruebrucelee

You can concatenate your fields into one and hash it to MD5 => then you will always have 32 digits that will uniquelly identify your record.

liron169liron169
Hi, Thanks.
But in this case I'll still need to write code.
wright?
truebruceleetruebrucelee

Yup,

 

simple before insert trigger should do, eg:

for(MyObject__c record : Trigger.new){

  Blob keyblob = Blob.valueof(record.field1__c + record.field__2);

  Blob key = Crypto.generateDigest('MD5',keyblob);

  record.Uniqueness_Check__c = EncodingUtil.convertToHex (key);
}

 

where Uniqueness_Check__c  is a text field with Unique checkbox ticked.

 

 

 

liron169liron169
Thanks again.
But my first target was to try and do it without code.