Validation rules make sure that the data being entered by a user meets the criteria that you specify before the user can save the record. A validation rule contains an expression that evaluates the data in one or more fields and returns a boolean value of “True” or “False”. If the criteria is met, which means that the rule returns "True", the validation rule displays an error message to the user.
Validation rules are specific for the objects. In order to create a validation rule, you have to navigate to the object in the Object Manager, click on Validation Rules and create a new one. It has a simple interface that lets you select fields from the current record or from the parent record/object. You can select fields from a parent record or a record that is related with a lookup/master-detail field. However, you cannot go down when selecting a field. This means that, you cannot check a value from a child record or a record that is not directly related to the current record. Read this article to learn more about validation rules and its limitations.
Using a before save record-triggered flow, you can check any record in the system and build complex validation rules. Read this post to learn more about Record-Triggered Flows.
Let's create a record-triggered flow that runs when Payment Method becomes Bank Transfer. Then display an error if the related account doesn't have any Bank Account records.
Before Creating the Flow
Record-triggered flows cannot have a screen element. Therefore, you cannot display a message on the screen. The main idea is to have a simple validation rule that is triggered when a checkbox field becomes true.
1- Create a checkbox field on the object. You can call it Payment_Method_Validation__c and it doesn't need to be on the layout.
2- Create a validation rule that displays an error message when Payment_Method_Validation__c becomes true.
Create a Record-Triggered Flow
1- Create a record-triggered flow that will run when a Work Order (you can do it for another object as well) is created or updated. Set the entry condition as Payment_Method__c = 'Bank Transfer' and mark the checkbox to run the flow only when the record is updated to meet the condition requirements. Lastly, make it a before save flow. In order to do so, select Optimize the Flow for Fast Field Updates.
2- In this flow, we want to check if there is a bank account record related to the account of the work order. In order to do so, add a Get Records element and get the Bank Account records of the related account.
3- Add a Decision element to check if there are bank account records.
4- If there is a bank account record, then there is no problem. So, the flow can end. However, if there is no bank acount, we want to trigger the validation rule that we created at the beginning. In order to do so, just update the checkbox field to true. Then the validation rule will display its message.
At the end, your flow should look like this. Don't forget to activate it.
After activating the flow, if you update the Payment Method to Bank Transfer and there is no Bank Account record related to the related Account, then the user will get an error message. Pay attention that the flow is not failing, it is triggering the validation rule to display a message. If it was an after save flow, then the flow would fail.
It is a very simple example of expanding the validation rule conditions. You can build much more complex logic if you want. Since you are using a flow, you can reach almost anything in the system, which lets you build any logic that you want.
Leave a Reply