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
RestAPI UerRestAPI Uer 

Avoid adding same contact to another lookup field

Hello All,

We have four contact lookup in the custom object called 'Reservation Center'. Values entered in the lookup fields should be unique. 
If we provided value for Contact1__c as Test Contact, then rest of other three lookup field should not use Test Contact.

Is this possible in the configuration? If not, please provide suggestion on how to achieve this in code.
Best Answer chosen by RestAPI Uer
Prakhar Saxena 19Prakhar Saxena 19
@RestAPI,

Try this, it worked for me:
 
OR(AND(NOT(ISBLANK(Field_1__c)), 
NOT(ISBLANK(Field_2__c)), 
Field_1__c = Field_2__c), 
AND(NOT(ISBLANK(Field_1__c)), 
NOT(ISBLANK(Field_3__c)), 
Field_1__c = Field_3__c), 
AND(NOT(ISBLANK(Field_1__c)), 
NOT(ISBLANK(Field_4__c)), 
Field_1__c = Field_4__c), 
AND(NOT(ISBLANK(Field_2__c)), 
NOT(ISBLANK(Field_3__c)), 
Field_2__c = Field_3__c), 
AND(NOT(ISBLANK(Field_2__c)), 
NOT(ISBLANK(Field_4__c)), 
Field_2__c = Field_4__c), 
AND(NOT(ISBLANK(Field_3__c)), 
NOT(ISBLANK(Field_4__c)), 
Field_3__c = Field_4__c) 
)

(Replaced ISNULL with ISBLANK)

Regards,
Prakhar

All Answers

Prakhar Saxena 19Prakhar Saxena 19
Hi RestAPI,

This can be achieved by using a Validation Rule.

If the four lookup fields to Contact in 'Reservation Center' object are Field_1__c, Field_2__c, Field_3__c and Field_4__c, then the following Validation Rule on 'Reservation Center' object can be applied:
 
(Field_1__c  =  Field_2__c) || (Field_1__c  =  Field_3__c) || (Field_1__c  =  Field_4__c) || (Field_2__c  =  Field_3__c) ||(Field_2__c  =  Field_4__c) || (Field_3__c  =  Field_4__c)

Regards,
Prakhar
RestAPI UerRestAPI Uer
Hi @Prakhar,

Thanks for the suggestion. I have tried the above scenario. Still, I got no luck. 

Below are the formula is used to achieve:
 
OR(AND(NOT(ISNULL(Target_Contact__c)),
		NOT(ISNULL(Other_Contact_1__c)),
		Target_Contact__c  =  Other_Contact_1__c),
	AND(NOT(ISNULL(Target_Contact__c)),
		NOT(ISNULL(Other_Contact_2__c)),
		Target_Contact__c  =  Other_Contact_2__c),
	AND(NOT(ISNULL(Target_Contact__c)),
		NOT(ISNULL(Other_Contact_3__c)),
		Target_Contact__c  =  Other_Contact_3__c),
	AND(NOT(ISNULL(Other_Contact_1__c)),
		NOT(ISNULL(Other_Contact_2__c)),
		Other_Contact_1__c  =  Other_Contact_2__c),
	AND(NOT(ISNULL(Other_Contact_1__c)),
		NOT(ISNULL(Other_Contact_3__c)),
		Other_Contact_1__c  =  Other_Contact_3__c),
	AND(NOT(ISNULL(Other_Contact_2__c)),
		NOT(ISNULL(Other_Contact_3__c)),
		Other_Contact_2__c  =  Other_Contact_3__c)
	)


 
Prakhar Saxena 19Prakhar Saxena 19
Ya, my bad. Forgot the null checks.
RestAPI UerRestAPI Uer
@Prakhatr

Sorry, my previous formula didn't work properly. If I keep blank in all field is showing the error message. But it shouldn't behave like that. Do you have any idea about that
Prakhar Saxena 19Prakhar Saxena 19
@RestAPI,

Try this, it worked for me:
 
OR(AND(NOT(ISBLANK(Field_1__c)), 
NOT(ISBLANK(Field_2__c)), 
Field_1__c = Field_2__c), 
AND(NOT(ISBLANK(Field_1__c)), 
NOT(ISBLANK(Field_3__c)), 
Field_1__c = Field_3__c), 
AND(NOT(ISBLANK(Field_1__c)), 
NOT(ISBLANK(Field_4__c)), 
Field_1__c = Field_4__c), 
AND(NOT(ISBLANK(Field_2__c)), 
NOT(ISBLANK(Field_3__c)), 
Field_2__c = Field_3__c), 
AND(NOT(ISBLANK(Field_2__c)), 
NOT(ISBLANK(Field_4__c)), 
Field_2__c = Field_4__c), 
AND(NOT(ISBLANK(Field_3__c)), 
NOT(ISBLANK(Field_4__c)), 
Field_3__c = Field_4__c) 
)

(Replaced ISNULL with ISBLANK)

Regards,
Prakhar
This was selected as the best answer
RestAPI UerRestAPI Uer
Hey @Prakhar,

Thanks. It's working fine.