LOCKING A RECORD FROM EDITING | Techila
1071
single,single-post,postid-1071,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

LOCKING A RECORD FROM EDITING

LOCKING A RECORD FROM EDITING

LOCKING A RECORD FROM EDITING

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. Wouldn’t that be nice? 

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.

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!

Approval Process to Lock Record

Approval Process to Lock Record

 

Now all I need is a trigger to catch the event and submit the approval:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

trigger AccountRecordLock on Account (after update, after insert){

for(Account a :Trigger.New){

if(a.Rating==‘Cold’){

// Create an approval request for the account

Approval.ProcessSubmitRequest req1 =

newApproval.ProcessSubmitRequest();

req1.setComments(‘Automatic record lock.’);

      req1.setObjectId(a.id);

 

// Submit the approval request for the account

Approval.ProcessResult result =Approval.process(req1);

}

}

}

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.

 

Courtesy:-http://www.verticalcoder.com/2012/07/26/locking-a-record-from-editing/

“Know more about Salesforce Consulting at Techila”

Author: techila

No Comments

Post A Comment