Way to lock a record from being edited even by the record owner after some criteria was met. For a while, there were lots of ideas thrown about such as validation rules and changing the record type so it uses a different page layout. Both of these solutions weren’t ideal. Validation rules happen after the user has entered in a bunch of data and then, wham! they can’t save. So frustrating! Also, you’d have to check every field to see if it had changed. Changing the record type is another option, but users can still edit the record through the API or list view edit functionality.
We then came around to Approval Processes and how they can lock the record. Now wouldn’t it be nice if we could lock the record too? Unfortunately, there isn’t a way in workflow rules or apex to just set an isLocked field to true.
Now that we’ve whined about this lack of functionality, let’s try to work around it. I found that you can submit a record for approval from Apex. I immediately started thinking of triggers, so when I got home, I whipped up a solution pretty quickly. (Yeah, I lead an exciting life.)
First, let’s set up an approval process. I want my Account records to lock after the rating is changed to Cold, so I’ll set that as my entry criteria. You need at least one step in an approval, but I didn’t want to deal with it, so I set the criteria on the set to False. This means that as soon as the record is submitted for approval, it is approved and left locked!
Now all I need is a trigger to catch the event and submit the approval:
Now as soon as I save a record with a Rating of Cold, the approval process kicks off and locks the record. Only users with Modify All permission on the object can now edit it!
You can add the Approval Process related list to the view to see that the approval was kicked off.
No comments:
Post a Comment