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
ahonahon 

how to display error or prevent users from updating a record

If all records on one related list do not have a specific field value (a picklist status), then prevent users from updating/selecting a specific picklist value on a different related list record.

Please let me know if I can provide more detail -- I am trying to determine if a validation rule can work or a flow using loop element would be a good solution.
SubratSubrat (Salesforce Developers) 
Hello ,

To enforce the requirement that if all records on one related list do not have a specific picklist status, then prevent users from selecting a specific picklist value on a different related list record, you can use a combination of validation rules and a flow with a loop element.

Here's a high-level overview of the solution:

Create a validation rule on the object where the picklist field exists to check if the specific picklist value is selected on a related list record. If the condition is met, show an error message to the user to prevent saving the record.

Create a flow that checks if all records on one related list have a specific picklist value. If not, the flow updates a field on the parent record (e.g., a checkbox) to indicate that the specific picklist value is not allowed.

Create a second validation rule on the object with the related list to check if the specific picklist value is selected and the checkbox from the parent record indicates that the value is not allowed. If the condition is met, show an error message to the user to prevent saving the record.

Here's a step-by-step guide to implementing the solution:

Step 1: Create a Validation Rule on the Parent Object

Go to the Object's (e.g., Account, Custom Object) schema where the picklist field exists.

Create a new validation rule with a formula that checks if the specific picklist value is selected on a related list record. For example, if the picklist field API name is "Status__c," and the value you want to prevent is "Not Allowed," the formula could be:
AND(
    ISPICKVAL(Status__c, "Not Allowed"),
    NOT(Has_Picklist_Value_On_Related_List__c)  // A custom checkbox on the parent object (default false)
)
This formula checks if the picklist value is "Not Allowed" and the checkbox "Has_Picklist_Value_On_Related_List__c" is not checked (false).

Set the error message to explain why the record cannot be saved with the selected picklist value.

Step 2: Create a Flow with a Loop Element

Create a new flow using the Flow Builder in Salesforce.
Use a loop element to iterate over the related list records to check if any of them have the specific picklist value. If you find any record with the value, set a variable to indicate that the picklist value is present on the related list.
Step 3: Update Parent Record Checkbox in the Flow

In the loop element, if the specific picklist value is found on the related list, update the parent record's checkbox field "Has_Picklist_Value_On_Related_List__c" to true.
Step 4: Create a Validation Rule on the Child Object with Related List

Go to the Object's (e.g., Account, Custom Object) schema where the related list exists.
Create a new validation rule with a formula that checks if the specific picklist value is selected and the checkbox on the parent record indicates that the value is not allowed. For example:
AND(
    ISPICKVAL(Status__c, "Not Allowed"),
    Parent_Object_r.Has_Picklist_Value_On_Related_List_c
)
This formula checks if the picklist value is "Not Allowed" and the checkbox on the parent object is checked (true).

Set the error message to explain why the record cannot be saved with the selected picklist value.

By using this combination of validation rules and a flow with a loop element, you can enforce the requirement that if all records on one related list do not have a specific picklist status, then prevent users from selecting a specific picklist value on a different related list record.

Hope this helps !
Thank you.