Sometimes we have faced business requirement to Lock or Unlock records in Salesforce. We can use apex lock() and unlock() methods in the System.Approval namespace to lock and unlock records by passing in record IDs or sObjects.
To enable this feature, go to Setup | Search Automation Settings in the Quick Find box | click on Automation Settings. Then, select Enable record locking and unlocking in Apex.
Lock Record Example:
//Get records to lock
List<case> caseList = [SELECT Id From Case LIMIT 10];
//Lock records
List<Approval.LockResult> lrList = Approval.lock(caseList, 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 account 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());
}
}
}
Unlock Record Example:
//Get records to unlock
List<case> caseList = [SELECT Id From Case LIMIT 10];
//Check locked records
List<case> caseLockList = new List<Case>();
for(Case c :caseList){
if(Approval.isLocked(c.id)){
caseLockList.add(c);
}
}
//Unlock record
if(!caseLockList.isEmpty()){
//Unlock records
List<Approval.UnlockResult> ulrList = Approval.unlock(caseLockList, false);
// Iterate through each returned result
for(Approval.UnlockResult ulr : ulrList) {
if (ulr.isSuccess()) {
//Operation was successful, so get the ID of the record that was processed
System.debug('Successfully locked account with ID: ' + ulr.getId());
}
else {
//Operation failed, so get all errors
for(Database.Error err : ulr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Case fields that affected this error: ' + err.getFields());
}
}
}
}
//Get your document from document Object
Document doc = [Select Id, Name, Body, ContentType, DeveloperName, Type From Document Where DeveloperName = 'Your_Doucment_DeveloperName'];
//Create Email file attachment from document
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setContentType(doc.ContentType);
attach.setFileName(doc.DeveloperName+'.'+doc.Type);
attach.setInline(false);
attach.Body = doc.Body;
//Apex Single email message
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setUseSignature(false);
mail.setToAddresses(new String[] { 'itzbiswajeet@gmail.com' });//Set To Email Address
mail.setSubject('Test Email With Attachment');//Set Subject
mail.setHtmlBody('Please find the attachment.');//Set HTML Body
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });//Set File Attachment
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });//Send Email
Sample Code Approach 2:
//Get your document from document Object
Document doc = [Select Id, Name, Body, ContentType, DeveloperName, Type From Document Where DeveloperName = 'Your_Doucment_DeveloperName'];
//Apex Single email message
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setUseSignature(false);
mail.setToAddresses(new String[] { 'itzbiswajeet@gmail.com' });//Set To Email Address
mail.setSubject('Test Email With Attachment');//Set Subject
mail.setHtmlBody('Please find the attachment.');//Set HTML Body
mail.setDocumentAttachments(new Id[]{doc.Id});//Set Document Attachment
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });//Send Email