+ Start a Discussion
Uves RavatUves Ravat 

Lock an Record From Updating

Hi

 

Is it possible to stop a user from updating/deleting a record when a certain checkbox is true?

For example i have field approved. if the field is false, user can edit that record but if its true they cant?

Probably have to use a trigger and see if approved field is false then allow update and true not but i cant put it in code?

 

thanks

Uves

Best Answer chosen by Admin (Salesforce Developers) 
007rakeshistom007rakeshistom

Let's Assume your checkbox name is:- ABC__c

 

and use any workflow field update to update Above mentioned field

 

trigger triggername on Object (before update)
{
  try
        {
          for(objectname o : trigger.new){
          if((o.ABC__c=TRUE)){
            o.recordtypeid='012M0000000CpTm';// Read only Record type
            }
            else
            {
             o.recordtypeid='01220000000DHDI'; //Normal record type
             }
                      
                   }

        }
                catch (DMLException e)
         {        
             System.debug('Exception Thrown!');   
             Exception e1 = e;    
         }
   }

All Answers

SFDC-SDSFDC-SD

There are four ways to lock a record in Salesforce

1) Trigger
2) Workflow rule & Record Types

3) Validation Rules

Trigger
This is the first option the would crawl in to peoples mind whenever there is record locking involved is Trigger. This might be an option to lock a record but writing lot of triggers might bring us close to governor limits. Hence, this would be the last option for me.

Record Types
Lets assume that the record has a check box call ‘Restrict’. Create a Read Only record and create a workflow rules that changes the record type to ‘Read Only’ if the ‘Restrict’ check box is checked. Create a two page layouts with one normal  and locked pagelayout. Control access to Lock field and Page layout based on profile.

 

Validation Rules
Write a validation rules saying (account.Restrict__c, true), then display an error message saying “Record is read only”

Record Types is the cleanest way I could think of to lock a record in Salesforce.

007rakeshistom007rakeshistom

Let's Assume your checkbox name is:- ABC__c

 

and use any workflow field update to update Above mentioned field

 

trigger triggername on Object (before update)
{
  try
        {
          for(objectname o : trigger.new){
          if((o.ABC__c=TRUE)){
            o.recordtypeid='012M0000000CpTm';// Read only Record type
            }
            else
            {
             o.recordtypeid='01220000000DHDI'; //Normal record type
             }
                      
                   }

        }
                catch (DMLException e)
         {        
             System.debug('Exception Thrown!');   
             Exception e1 = e;    
         }
   }

This was selected as the best answer
Uves RavatUves Ravat

Thanks, the trigger solution worked.

 

I tried to change the code so i could get a better error message saying "This Proposal has been Locked."?

How do i do that?

 

Thanks

Uves

007rakeshistom007rakeshistom

you can use trigger.adderror, Please mark as solution

Uves RavatUves Ravat

trigger checkIfProposalIsLocked on ProjectProposalHeader__c (after delete, after update, before delete, before update)
{
try
{
for (ProjectProposalHeader__c projPros :trigger.new)
{
if(projPros.Project__r.Proposal_Approved__c = true)
{
projPros.RecordTypeID = '012M0000000CpTm';
projPros.addError('Proposal has been locked');
}
else
{
projPros.RecordTypeID = '01220000000DHDI';
}
}
}
catch (DMLException e)
{
System.debug('Error');
Exception e1 = e;
}
}

 

i tried this button i still the error message as 

 

"Error:Apex trigger checkIfProposalIsLocked caused an unexpected exception, contact your administrator: checkIfProposalIsLocked: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.checkIfProposalIsLocked: line 7, column 1"

i want it as "Proposal is Locked"

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/