Tuesday, 25 July 2017

How to Lock/Unlock record thorough Apex code ?

Hi All,

We all have faced many business scenario where we have to lock/unlock records. We had traditional approach to create separate record type and page layout to achieve it, but salesforce has given API to do thorough apex code.

Please make sure below settings is enabled.

Locking Record 

1. Create a standard Approval process which will take care of locking records.


2. Invoke Approval process
  a. Using Process builder (Submit for Approval)
  b. Use Apex code to invok from trigger or custom button.

    * This method will invoke the standard approval process
    public static void submitForApproval(Case caseRecord)
        // Create an approval request for the Opportunity
        System.debug('Printing insyde approval process method');
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval automatically using Button');
        //req1.setNextApproverIds(new Id[] {case.owner});

        // Submit the approval request for the Opportunity
        Approval.ProcessResult result = Approval.process(req1);


If there is no approval process defined still we can lock the record.

// Query the accounts to lock
Case [] cases = [SELECT Id from Case];
// Lock the accounts
Approval.LockResult[] lrList = Approval.lock(cases, 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());

Unlocking record

Below is the code which will unlock the record.

    * This method will unlock case record.
    public static void unlockCaseRecord(Case caseRecord) {
        Approval.UnlockResult unlockedRersult = Approval.unlock(caseRecord);
        // Iterate through each returned result
        if (unlockedRersult.isSuccess()) {
            // Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully unlocked opportunity with ID: ' + unlockedRersult.getId());
        else {
            // Operation failed, so get all errors                
            for(Database.Error err : unlockedRersult.getErrors()) {
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('opportunity fields that affected this error: ' + err.getFields());

Monday, 24 July 2017

How to resolve the java script error "Cannot read property execute of undefined"

Hi All,

Debugging java script error is very tedious, if there is some still mistake then whole functionality will not work and it will not even tell line number of code where exactly that error is coming.

Anyways let us say you have custom detail page button (Source code = java script) , you want to call apex class from that button code, you faced this error "Cannot read property execute of undefined"
This is because you have missed to add {!requireScript("/soap/ajax/26.0/apex.js")} in the java script code.



Thursday, 13 July 2017

How to display button on apex page message ?

Hi All,

We might need to display button while showing page message to get user feed back. Based on user feedback necessary action need to be taken care.

For example

Below is the code snippet.

 <apex:pageMessages escape="false"/> 
<apex:outputPanel rendered="{!isYesVisible}">
        <apex:pageMessage severity="info" strength="3" summary="{!applicantMessage}">
                <apex:commandButton value="Yes" action="{!cloneOpportunity}"/>
                <apex:commandButton value="No" action="{!stayinSamePage}"/>

Tuesday, 11 July 2017

How to supress standard approval email in salesforce ?

Hi All,

Sometimes we might need to suppress approval email from being sent out without affecting approval process.

There are two way we can achieve it.
1. Creating Queue.

Create empty queue record and use empty queue in approval process as approver.

2. Update user record
Select Migration admin user as approver in the approval process.
Change approver setting for migration admin user.

Saturday, 8 July 2017

How to export detail record in word document ?

Hi All,

We can do this using conga composer , in my earlier post I have explained this .


We will do in simple way/easier way. here we go

1. Create visaulforce page

<apex:page standardController="Case" contentType="application/msWord#msword.doc">
    <apex:detail />

2. Create custom button. Content source might be execute java script or link.


How to use comparatiors operator in java script button code ?

Hi All,

This post is just to help you to use comparatiors operator (Greater than  > and less than <) effectively in Java script code.

I did face some issue while using,

Say the case record is having Absolute_Total_Exception_Amount_USD__c as 50,340.

alert('print-'+{!Case.Absolute_Total_Exception_Amount_USD__c}); -- it will Print50
alert('print-'+"{!Case.Absolute_Total_Exception_Amount_USD__c}"); - It will print 50,340
var amount =  "{!Case.Absolute_Total_Exception_Amount_USD__c}";

if(amount  > "120000"){
alert('webservice method');

Always else block will get executed, Pagemethod alert will come.

The correct approach will be
var amount = {!VALUE(TEXT(Case.Absolute_Total_Exception_Amount_USD__c))};
alert('Pagemethod-4-'+amount );
if(amount  > "120000"){
alert('webservice method');