Friday 17 October 2014

How to avoid Recursive Trigger in Salesforce

To resolve recursive trigger please follow the below steps,

     1. Create a class with the Boolean variable and set this as true to avoid Recursive trigger.
  1. public class RecursiveHandler
  2. {
  3.      public static Boolean isNotRecursive = true;
  4. }

    2. Check the Boolean flag value before process your condition and set the flag value to false at the beginning of your process. Once if you set this false, the recursion will stop.
  1. trigger DuplicateRecordMaker on Account (after insert, after update)
  2. {
  3.     List<Account> accounts = new List<Account>();
  4.    
  5.     if(RecursiveHandler.isNotRecursive)
  6.     {
  7.         RecursiveHandler.isNotRecursive = false;
  8.        
  9.         for(Account currentAccount : Trigger.New)
  10.         {
  11.             Account acc = new Account();
  12.             acc.Name = currentAccount.Name;
  13.             accounts.add(acc);
  14.         }
  15.        
  16.         insert accounts;
  17.     }
  18. }

Hope the above coding will helpful to solve recursive trigger error.

How to avoid maximum trigger depth exceeded exception in Salesforce

As Per Salesforce Governor Limits, Total stack depth for any Apex invocation that recursively fires triggers due to insert, update, or delete statements size is 16.  If you go beyond this limit you will get “Maximum Trigger Depth Exceeded error”.

The below code is one of the example of getting this error,

  1. trigger DuplicateRecordMaker on Account (after insert, after update)
  2. {
  3.     List<Account> accounts = new List<Account>();
  4.     for(Account currentAccount : Trigger.New)
  5.     {
  6.         Account acc = new Account();
  7.         acc.Name = currentAccount.Name;
  8.         accounts.add(acc);
  9.     }
  10.     insert accounts;
  11. }
Reason for getting such error,

            My Trigger concept is whenever account record is created, the same record should be created as duplicate record. After insert a record the above trigger will fire then duplicate record will process after that trigger will call recursively, this cause a Maximum Trigger Depth Exceeded error.

Check out the below link to resolve this error,

 How to avoid Recursive Trigger in Salesforce




Tuesday 7 October 2014

How to Indent / Align / Format Apex Code in Salesforce without using eclipse or any other tools

Most of the Salesforce developer who are not using the Developer Console may not aware how to Indent / align the code. They generally go for external site or applications to intend their code.

If you write Apex class / Apex Trigger / Visualforce page using the normal User Interface, then you code is not auto formatted as well when reading the code is very ridiculous. The coding should always readable to everyone for this you need to indent your code. We can indent the coding using Developer Console.

Here is a simple steps to indent your code using the developer console.

1.    Click “Developer Console” from the Set up menu.



2.    Then Click File à Open à Classes à Select your class that you want to indent.




3.    Select all or just a section of code to indent

4.    Press Shift + TAB




5.    Save


That’s it...! Your code is Indented now...! 

Wednesday 1 October 2014

How to Convert JSON Data to List Class in Salesforce

Problem

  Need to format JSON response and display the response values in PageBlockTable in visualforce page.

JSON String
  1. [
  2.     {
  3.         "label": "Winter '11",
  4.         "url": "/services/data/v20.0",
  5.         "version": "20.0"
  6.     },
  7.     {
  8.         "label": "Spring '11",
  9.         "url": "/services/data/v21.0",
  10.         "version": "21.0"
  11.     },
  12.     {
  13.         "label": "Summer '11",
  14.         "url": "/services/data/v22.0",
  15.         "version": "22.0"
  16.     },
  17.     {
  18.         "label": "Winter '12",
  19.         "url": "/services/data/v23.0",
  20.         "version": "23.0"
  21.     },
  22.     {
  23.         "label": "Spring '12",
  24.         "url": "/services/data/v24.0",
  25.         "version": "24.0"
  26.     },
  27.     {
  28.         "label": "Summer '12",
  29.         "url": "/services/data/v25.0",
  30.         "version": "25.0"
  31.     },
  32.     {
  33.         "label": "Winter '13",
  34.         "url": "/services/data/v26.0",
  35.         "version": "26.0"
  36.     },
  37.     {
  38.         "label": "Spring '13",
  39.         "url": "/services/data/v27.0",
  40.         "version": "27.0"
  41.     },
  42.     {
  43.         "label": "Summer '13",
  44.         "url": "/services/data/v28.0",
  45.         "version": "28.0"
  46.     },
  47.     {
  48.         "label": "Winter '14",
  49.         "url": "/services/data/v29.0",
  50.         "version": "29.0"
  51.     },
  52.     {
  53.         "label": "Spring '14",
  54.         "url": "/services/data/v30.0",
  55.         "version": "30.0"
  56.     },
  57.     {
  58.         "label": "Summer '14",
  59.         "url": "/services/data/v31.0",
  60.         "version": "31.0"
  61.     }
  62. ]
Before creating class, Copy your JSON Response and Paste the response here Json2Apex then create apex. You will get the apex code based on your JSON Response and you can alter it based on your requirement. I have altered the auto generated code for the above json response and the code is,

Apex Class

  1. global class SalesforceVersionInfo
  2. {
  3.     public List<SFInstance> sfInstances{get;set;}
  4.    
  5.     public SalesforceVersionInfo()
  6.     {
  7.         String jsonString = '[{\"label\":\"Winter \'11\",\"url\":\"/services/data/v20.0\",\"version\":\"20.0\"},{\"label\":\"Spring \'11\",\"url\":\"/services/data/v21.0\",\"version\":\"21.0\"},{\"label\":\"Summer \'11\",\"url\":\"/services/data/v22.0\",\"version\":\"22.0\"},{\"label\":\"Winter \'12\",\"url\":\"/services/data/v23.0\",\"version\":\"23.0\"},{\"label\":\"Spring\'12\",\"url\":\"/services/data/v24.0\",\"version\":\"24.0\"},{\"label\":\"Summer \'12\",\"url\":\"/services/data/v25.0\",\"version\":\"25.0\"},{\"label\":\"Winter\'13\",\"url\":\"/services/data/v26.0\",\"version\":\"26.0\"},{\"label\":\"Spring \'13\",\"url\":\"/services/data/v27.0\",\"version\":\"27.0\"},{\"label\":\"Summer\'13\",\"url\":\"/services/data/v28.0\",\"version\":\"28.0\"},{\"label\":\"Winter \'14\",\"url\":\"/services/data/v29.0\",\"version\":\"29.0\"},{\"label\":\"Spring\'14\",\"url\":\"/services/data/v30.0\",\"version\":\"30.0\"},{\"label\":\"Summer \'14\",\"url\":\"/services/data/v31.0\",\"version\":\"31.0\"}]';
  8.         sfInstances = (List<SFInstance>) System.JSON.deserialize(jsonString, List<SFInstance>.class);
  9.         sfInstances.sort();
  10.     }
  11.    
  12.     global class SFInstance implements Comparable
  13.     {
  14.         public String label{get;set;}
  15.         public String url{get;set;}
  16.         public String version{get;set;}
  17.        
  18.         public Integer compareTo(Object ObjToCompare)
  19.         {
  20.             return label.CompareTo(((SFInstance)ObjToCompare).label);
  21.         }
  22.     }
  23.    
  24. }

Visualforce Page

  1. <apex:page controller="SalesforceVersionInfo">
  2. <apex:form >
  3. <apex:pageBlock >
  4. <apex:pageBlockTable value="{!sfInstances}" var="sf">
  5. <apex:column headerValue="Label" value="{!sf.label}"/>
  6. <apex:column headerValue="URL" value="{!sf.url}"/>
  7. <apex:column headerValue="Version" value="{!sf.version}"/>
  8.  </apex:pageBlockTable>
  9. </apex:pageBlock>
  10. </apex:form>
  11. </apex:page>

Result



Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...