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
Dastagiri BashaDastagiri Basha 

Hi, developer's. How to lock the records, can help me.

Hi developer's, i have a requirement like in the case object have RT "payment's".and status "close Completed"
if the case status updated to  close completed for that perticular record type, i need to lock other case fields(should not be editable) how can i achive this one?
CharuDuttCharuDutt
Hii Dastagiri
Try Below Solution Example 
1> Vlidation Rule 
AND($RecordType.Name = "payment's", 
ISPICKVAL(Status, 'Close Completed'),
OR(ISCHANGED(Description),OR(ISCHANGED(Origin))
)

#####################################
2> To enable this feature, from Setup, enter Process Automation Settings in the Quick Find box, then click Process Automation Settings. Then, select Enable record locking and unlocking in Apex.
The new Approval.LockResult and Approval.UnlockResult classes let you interact with the results of your programmatic record locks and unlocks.


trigger tresLockCase on Case (after insert, after update) {
    set<Id> recordtolock = new set<Id>();
    Id devRecordTypeId = Schema.SObjectType.case.getRecordTypeInfosByName().get('payments').getRecordTypeId();
    if(Trigger.IsAfter){
        if(trigger.IsInsert){
            for(Case oCase : trigger.new){
                if(oCase.RecordTypeId == devRecordTypeId && oCase.Status == 'Close Completed'){
                    recordtolock.add(oCase.Id);
                }
            }
        }else if(trigger.IsUpdate){
            for(Case oCase : trigger.new){
                if(oCase.RecordTypeId == devRecordTypeId && oCase.Status == 'Close Completed' && oCase.Status!= trigger.oldMap.get(oCase.Id).Status){
                    recordtolock.add(oCase.Id);
                }
            }
        }
    }
    list<Id> recordtolock2= new list<Id>();
    recordtolock2.addAll(recordtolock);
    Approval.LockResult[] lrList = Approval.lock(recordtolock2, false);
    
    // Iterate through each returned result
    for(Approval.LockResult lr : lrList) {
        if (lr.isSuccess()) {
            // Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully locked Case with ID: ' + lr.getId());
        }
        else {
            // Operation failed, so get all errors                
            for(Database.Error err : lr.getErrors()) {
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('Case fields that affected this error: ' + err.getFields());
            }
        }
    }
    
}
Please Mark It As Best Answer If It Helps
Thank You!
AnkaiahAnkaiah (Salesforce Developers) 
Hi Basha,

Refer the below link have solution for similar kind of ask.
https://trailhead.salesforce.com/trailblazer-community/feed/0D54S00000A7wjqSAB

If this helps, Please mark it as best answer.

Thanks!!
mukesh guptamukesh gupta
Hi Dastagiri,

You can learn Record Lock and Unlock by below url:-

https://technogeeksfdc.wordpress.com/2020/04/14/record-locking-unlocking-from-apex/

https://jayakrishnasfdc.wordpress.com/2021/05/02/lock-unlock-records-using-apex-in-salesforce/#:~:text=go%20to%20Setup%20%7C%20Search%20Automation,locking%20and%20unlocking%20in%20Apex.


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh