function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
skumar123skumar123 

How to lock the Record?

Hi,

 

I want to lock particular Record on some condition.

 

Ex: I will include one custom field(check box) in Opportunity as "Lock". If this is checked for any Opportunity, then that Opportunity should not be accessible by any other group (Ex: Sales, Marketing).

How can I achieve this?

 

Thanks for any help.....

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

You might create two record types, "open" and "locked".  Configure them with whatever security setup you wish the users to have.

 

Then write a simple before update trigger on Opportunity to change the recordtype of the opportunity from one to the other depending on the status of your checkbox.

 

Best, Steve.

All Answers

shillyershillyer

Read locking statements in Apex Dev Guide.

 

Hope that helps,

Sati

SteveBowerSteveBower

You might create two record types, "open" and "locked".  Configure them with whatever security setup you wish the users to have.

 

Then write a simple before update trigger on Opportunity to change the recordtype of the opportunity from one to the other depending on the status of your checkbox.

 

Best, Steve.

This was selected as the best answer
PerGeertPerGeert

Or you may create a workflow, which on the change of your locl-field to true makes a field-update action, which change the record type to 'locked opportunity'.

Make a separate layout for this particular record type.

skumar123skumar123

 

Thanks....this is helped me...

ShamilShamil

Steve,

 

is there any other workaround that does not use record types?

 

 

 

thanks,

Shamil

SteveBowerSteveBower

Recordtypes and workflows as previously mentioned would be easier, but yes, there might be other ways around this.

 

But, they require writing code.

 

For example, you could deal with changes with a before update Trigger that implements "Reject with an error any updates to this record if xyz field is set to true.  (Remember to allow them if the field being changed is xyz, otherwise you're really locked out.)

 

However, then you're letting users go down the path of making edits and hitting save before they see that the record is locked.

 

So, instead you could write a visualforce page which overrides the Edit button, and do the check when the custom controller extension loads and reply appropriately. This way the user doesn't even go down that path, however it doesn't stop changes to the records via the API.   So, to really lcok it down nicely, you would need both.

 

 

Another approach, I'd have to think about a bit more, might be to change the ownership of the record once the field is set to an owner whose records nobody else has the permissions to see / edit, etc.

 

 

However, why not use record types?  :-)

 

Best, Steve. 

ShamilShamil

Steve, thanks for the options!

 

The reason I don't want to use record types is because: 1) they work only in Unlimited Edition and Enterprise Edition, 2) they are not packageable....yet.

 

I'd prefer the Visualforce path, since it's more user-friendly, but still I wish there was a locking mechanism in Apex and/or Force.com API.

 

 

Thank you!

Shamil

Success ObjectsSuccess Objects

Check out this AppExchange solution that works with PE.  (Overrides - Validation Rules for Buttons)

NIKHIL_SFDCNIKHIL_SFDC

Hi Shamil,

 

One simple solution to lock any record is to write a valication rule

like:

if(and( lock__c =true,
or( ISCHANGED( Check__c )=true, ISCHANGED(  Text_A__c  )=true)),  Check1__c =true, Check1__c =false
)

 

This will show error message when lock (chckbox) is checked and you will try to change any value in that record

 

Thanks,

Nikhil

NIKHIL_SFDCNIKHIL_SFDC

Hello Steve,

 

Thanks for solution..!!

 

I have a query here: How to prevent that record for system administrator except one field (that would help to unlock the record to admin)

 

Thanks,

Nikhil

Sys Admin 3Sys Admin 3
Using role hierarchy we can achieve this.
First Setup, In the Organization-Wide Defaults section, make it as private for that particular object.
Upto here the higher official in Role hierarchy having a permission to see the sub-ordinates data.
Now just navigate to setup->Administration setup->Security Controls->Sharing settings, in the org-wide default section you should see the Grant Access Using Hierarchies column.
If you set a custom object’s organization-wide default to Private and deselect “Grant Access Using Hierarchies” for that object, then without additional sharing, only record owners and administrators can access the object’s records.
Jakub OrackiJakub Oracki
You can create simple Visualforce page using <apex:detail> tag so your layout configuraction will be in use. Then you can hide "edit" button using javascript and disable inline editing for the page. When the page is ready overwrite View and Edit actions on the object with the visualforce page.

Hope it help you

Regards
Kuba 
sfdcFanBoysfdcFanBoy

There are many ways to lock a record in Salesforce.

I've consolidated the list - Here it is - 7 ways to lock record in Salesforce

https://sfdcfanboy.com/2017/04/26/7-ways-to-lock-a-record-in-salesforce/