Hi there! Let’s say you have your parent record that is pending approval. There are times when it is necessary to lock all or some related records from editing while parent record is not yet approved. For example, you want to lock all opportunity line items while parent opportunity is pending approval. I’m guessing you don’t want someone to edit related products and change line items back and forth.

In this tutorail, I’m going to show you one way to lock from editing related records by using simple apex trigger.

Step 1: Give access to approvals from Apex

This permission is very important as we won’t be able to use approval methods in our classes & triggers. Here is how you enable it:

Setup -> Create -> Workflows and Approvals -> Process Automation Settings

From there set checkbox Enable record locking and unlocking in Apex as true.

Salesforce | How to lock related records if pending approval?

Otherwise, you’ll get the following error:

FATAL_ERROR System.UnexpectedException: Apex approval lock/unlock api preference not enabled.

So please make sure it is done before moving further.

Step 2: Setting up a trigger

Here we need to decide which related objects we want to be locked. For this example, I’ll use an opportunity that is pending approval with some opportunity line items.

I’ll be using isLocked(id) approval method to check if our opportunity is currently locked.

More about approval methods you can find on Apex Developer Guide.

Trigger considerations:

  • User should not be able to add new products
  • User should not be able to edit existing products
  • User should not be able to delete existing products
Copy to Clipboard

But what about users that still should have permissions to edit locked records?

As you know, you can have admins be able to edit records. Or also there is an option to add currently assigned approvers.

Salesforce | How to lock related records if pending approval?

Here is what you do if you need to allow only system admins to edit related records. In your apex trigger you need to find out who is curently editing records. One way of doing it is to add the following to our trigger:

Copy to Clipboard

In fact, there is another way of doing this. We can simply add formula field to a related object to identify profile name.

Go to Object Manager -> Fields and Relationships – > Add New Field. Select Formula and click ‘Next’.

Salesforce | How to lock related records if pending approval?

From the next screen you need to choose ‘Text’ and enter formula to get curent user profile name:


You can name your field as you like. So the final result should look something like this:

Salesforce | How to lock related records if pending approval?

I’ve chosen the following name for the field: ‘Current User Profile‘. Which means the api will be Current_user_profile__c. You can simply add this field to a page layout to verify it works. Or you can check it in trigger:

Copy to Clipboard

Alringht, since we figured out with current user profile its time to update our trigger.

Copy to Clipboard

Now what if we want to grant edit access to users who are within assigned approvers? The game plan here is to first determine a user who is currentily editing a record. Next, we need to get a list of all assigned approvers for parent Opportunity. If current user is within assigned approvers – then he should be able to proceed with editing.

Lastly, we need to check user Profile. So the validation rule should fire only when a user is not within assigned approvers or not having ‘System Administrator’ profile.

Copy to Clipboard

Step 3: Adjust Visualforce Pages

In case you have cusom visualforce pages you could display some friendly reminder that editing is currently locked. Make sure you don’t forget to add page messages tag. Therefore, it will return any errors that occur while saving.

Copy to Clipboard

Here is a sample VF page with approval validations:

Copy to Clipboard

As you can see, it uses standard controller and small extention to check our validations.

Here is the extention itself:

Copy to Clipboard

So under normal conditions, the page will look like this:

Unlocked layout

Whenever opportunity its pending approval, the page will look different for those who are not within assigned approvers. It will look like this:

Locked layout

As you see,  we are hiding the content as well as ‘Save‘ button. Therefore is also warning message that says:

‘Can’t edit line items. Opportunity is pending approval’

I’m hoping this gave you idea on how to lock related records from editing while parent record is pending approval. There could be other approaches for this task. Please let me know in the comments section what you think.

See you next time!

0 0 vote
Article Rating