You need to sign in to do that
Don't have an account?

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.....
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
Read locking statements in Apex Dev Guide.
Hope that helps,
Sati
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.
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.
Thanks....this is helped me...
Steve,
is there any other workaround that does not use record types?
thanks,
Shamil
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.
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
Check out this AppExchange solution that works with PE. (Overrides - Validation Rules for Buttons)
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
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
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.
Hope it help you
Regards
Kuba
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/