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
vijaya kudupudivijaya kudupudi 

how to lock record ?

Hi,

I want to lock my record using apex coding. I have tried using "Approval.LockResult[] lrList = Approval.lock(recordID);"  from the link(https://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/rn_apex_approval_locks_unlocks.htm). But it's locked the record in API version 35 and it's not locking the record in API version 38. How can i acheve this ? Any one can help me ?
Best Answer chosen by vijaya kudupudi
karthikeyan perumalkarthikeyan perumal
Hello, 

use Belo code for update trigger Record Type 

Note: for this Record Type  associate new page layout and all field should be in read only and unlock button(custom button) should be place at top of the page 
 
trigger RecordlockTrigger on Account (before insert, before update) {
Map<String, Id> typeMap = New Map<String, Id>();

   for(RecordType rt: [Select DeveloperName, Id From RecordType Where sObjectType = 'Account']) {
      typeMap.put(rt.DeveloperName, rt.Id);
   }

   for (Account Acc : trigger.new)  {

      recordtype agmtRT = [select id, developername from recordtype where id=:Acc.recordtypeid];
      recordtype IFRT = [select id, developername from recordtype where developername = 'Locked'];
      // If the Record Type = Locked
      if (agmtRT.id == IFRT.id) { 
            // And the Agreement Category on the record = Services
            if (Acc.Type == 'Customer - Direct') {
                // Then automatically change the Record Type to Services Agreement.
                id recid = typeMap.get('Locked');
                recordtype rectype = [select id, developername from recordtype where id=:recid];
               Acc.RecordTypeid = rectype.id; 
            }
       }
   }
}

Thanks
karthik

 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

kinldy try below method, 

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.

Hope this will help you, 
Mark Best ANSWER if its clear. 

Thanks
karthik
 
NagendraNagendra (Salesforce Developers) 
Hi Vijaya,

There appears to be some issue when using Apex to lock records in Salesforce if your sandbox is on Spring '16 (metadata version 36). To work around the issue the metadata version of Apex classes locking records should be set to 36. This may make production deployments an issue until the issue has been fixed / all production environments have been upgraded to Spring '16.

For additional information check here: Good Luck.

Best Regards,
Nagendra.P
Navandeep_Kaur23Navandeep_Kaur23
Hi ,

Record Locking was introduced in Winter16 Release .It should be compatible with the higher versions.

You can write a workflow rule with no actions , only to track that the lock is working consistently.

Let me know the exceptions or errors which you are getting with higher version 38.

Thanks,
Navan
vijaya kudupudivijaya kudupudi
Hi Navandeep,

I am not getting any exceptions. If i run that through Anonymous Window in debug logs it showing like record is locked. But if i go to record and checking means record has not been locked.
karthikeyan perumalkarthikeyan perumal
after locking record you have to assign read only record type to that record. so that suggest  you my above approach. create Read only record type and page layout assign to that for locking record. 

Thanks
karthik
 
vijaya kudupudivijaya kudupudi
Hi karthikeyan perumal,

I tried this option on account object and i wrote trigger. But record has not locked. Here i am attaching my trigger. Please correct me if is there any mistake in my trigger.
trigger RecordlockTrigger on Account (before insert,before update) {
Map<String,Id> rectypMap = new Map<String,Id>();
for(RecordType rctyp: [Select id, name from RecordType where SobjectType='Account']){
rectypMap.put(rctyp.name,rctyp.id);
}

for(Account ac :Trigger.new){
    if(ac.Type=='Prospect'){
        ac.recordTypeId = rectypMap.get('Open');
        system.debug('Open record type'+ ac.recordTypeId);
    }
    if(ac.Type=='Customer - Direct'){
        ac.recordTypeId = rectypMap.get('Locked');
        system.debug('locked record type'+ ac.recordTypeId);
    }
}
}

 
karthikeyan perumalkarthikeyan perumal
Hello, 

use Belo code for update trigger Record Type 

Note: for this Record Type  associate new page layout and all field should be in read only and unlock button(custom button) should be place at top of the page 
 
trigger RecordlockTrigger on Account (before insert, before update) {
Map<String, Id> typeMap = New Map<String, Id>();

   for(RecordType rt: [Select DeveloperName, Id From RecordType Where sObjectType = 'Account']) {
      typeMap.put(rt.DeveloperName, rt.Id);
   }

   for (Account Acc : trigger.new)  {

      recordtype agmtRT = [select id, developername from recordtype where id=:Acc.recordtypeid];
      recordtype IFRT = [select id, developername from recordtype where developername = 'Locked'];
      // If the Record Type = Locked
      if (agmtRT.id == IFRT.id) { 
            // And the Agreement Category on the record = Services
            if (Acc.Type == 'Customer - Direct') {
                // Then automatically change the Record Type to Services Agreement.
                id recid = typeMap.get('Locked');
                recordtype rectype = [select id, developername from recordtype where id=:recid];
               Acc.RecordTypeid = rectype.id; 
            }
       }
   }
}

Thanks
karthik

 
This was selected as the best answer