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
Shuhbam SinhaShuhbam Sinha 

Validations on Record Edit Form LWC


Hello Everyone,
I am creating a form with the help of record edit form in LWC to create a record. Form has six lookup fields of User.Now the requiremen is, all the six look ups should have unique value , no values should be repeated . User should see an error message when he tries to create a record with duplicate lookup value. Can anyone please me on the same. Can i use validation rule or trigger here.
Thanks in advance
Shri RajShri Raj

Yes, you can use a custom validation rule or trigger to achieve the requirement of unique lookup values in the record edit form.
A custom validation rule can be created in Salesforce and associated with the object that you are creating a record for. This rule can validate the six lookup fields and check if they have unique values. If the values are not unique, it can display an error message to the user.
A trigger can also be used to perform the same validation. A before insert trigger can be created that will validate the six lookup fields and display an error message if the values are not unique.
If you are using a custom validation rule, it will be enforced automatically when the user saves the record. If you are using a trigger, you can use the addError method to display the error message to the user.

 

 

Here's an example of how you can implement a custom validation rule in Salesforce to ensure that the six lookup fields have unique values:
 

AND( 
COUNTIF($User.Name, User1__c) + 
COUNTIF($User.Name, User2__c) + 
COUNTIF($User.Name, User3__c) + 
COUNTIF($User.Name, User4__c) + 
COUNTIF($User.Name, User5__c) + 
COUNTIF($User.Name, User6__c) > 6, 
TRUE, 
FALSE )
Juber kimberJuber kimber
You can use both validation rules and triggers to enforce uniqueness constraints on lookup fields in a form.
Validation Rules: Validation rules are simple to set up and can be created directly in the Salesforce setup. You can use a validation rule to check the values of the six lookup fields and ensure that no duplicates exist. If a duplicate is found, you can display an error message to the user.
Triggers: Triggers are Apex code that are executed before or after a record is inserted, updated, or deleted. You can write a trigger that checks the values of the six lookup fields before the record is saved and displays an error message if any duplicates are found. This approach is more flexible, but requires writing Apex code and is more complex to set up and maintain.
In either case, you will need to consider the unique requirements of your application and choose the approach that best meets your needs. https://www.fmc4me.net/