Business Requirement
One of the common requirements we see in most of the projects is to make a record Read Only based on some conditions. Some of the use cases to make the record read only are listed here:-
- When a case is closed
- When a record has been sent to a 3rd Party system via integration and waiting for response from the 3rd party system
- When a checkbox field is marked true. e.g Received flag on a custom payment object
- When a lookup field is updated with a value.
Solution
One common solution for above use cases is to use a page layout with all fields marked read only and link it to a record type. When required conditions are met, update the record type for record to make it read only. In this solution all fields are set to read-only for all users apart from users with administrator profiles. This solution also gives a flexibility of keeping one or few fields as editable if needed. Other option is to use Salesforce Dynamic Form and make the fields or sections required based on field values.
Another good solution that we have been using for this kind of requirement is to use Salesforce out of the box Approval Class methods Lock & Unlock. This is used in the Salesforce standard approval process to make records read only when submitted for approval. This function can be used in flow or in apex using the below syntax.
Here recordId is the Salesforce ID of the record that we would like to lock or unlock
To Lock a Record
Approval.lock(recordId)
To Unlock a Record
Approval.unlock(recordId)