Sunday 29 May 2016

HOW TO WRITE BATCH CLASS IN SALESFORCE.COM APEX

In order for us to write batch classes in apex, let us 1st understand what is batch class , when and where it should be used? This is an extensive blog going into details.
Main sections of this post are
  1. What is batch Apex
  2. When to use batch apex
  3. Advantages of using batch apex
  4. Batch Apex Governor limits
  5. Sample Code


What is Batch Apex ?

Batch as the name suggests, is used when a large data (bulk) volume is involved and it has to be redundantly processed using a particular logic.
The Batch apex, can be used to conveniently perform time to time task and some real complex job ranging from data cleansing, archiving the data to the other quality improvements.  

When to use Batch Apex?

The typical use of batch apex is when you want to do a bulk job, but if you do it in a regular apex you’re bound to hit the governor limits. Batch Classes process the set of records you pass to it in batches of maximum 200 per batch. To  learn more about the governor limits go here



Advantages of using Batch Apex?


  • Higher Governor Limits
  • Can be used to process in batches
  • Can be scheduled to run at different time. (read more)
  • Work around to other governor limits e.g. Send More than 10 E-mails blog by Ankit Arora

  • here is the governor limit difference in using batch
    Area
    Normal Context
    Batch Context
    SOQL queries100 SOQL per cycle200 SOQL per cycle
    records retrieved by SOQL queries50,00050,000,000 (getQueryLocator)
    executed code statements200,0001,000,000 (Winter '14)
    Heap size6 MB12 MB


    Batch Apex Governor Limits

    These are the governor Limits you need to keep in mind when dealing with Batch Apex
    • Up to five queued or active batch jobs are allowed for Apex.
    • A user can have up to 50 query cursors open at a time. For example, if 50 cursors are open and a client application still logged in as the same user attempts to open a new one, the oldest of the 50 cursors is released. Note that this limit is different for the batch Apex startmethod, which can have up to five query cursors open at a time per user. The other batch Apex methods have the higher limit of 50 cursors. Cursor limits for different Force.com features are tracked separately. For example, you can have 50 Apex query cursors, 50 batch cursors, and 50 Visualforce cursors open at the same time.
    • A maximum of 50 million records can be returned in the Database.QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed.
    • If the start method returns a QueryLocator, the optional scope parameter ofDatabase.executeBatch can have a maximum value of 2,000. If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 2,000 records. If the start method returns an iterable, the scope parameter value has no upper limit; however, if you use a very high number, you may run into other limits.
    • If no size is specified with the optional scope parameter of Database.executeBatch, Salesforce chunks the records returned by the start method into batches of 200, and then passes each batch to the execute method. Apex governor limits are reset for each execution of execute.
    • The startexecute, and finish methods can implement up to 10 callouts each.
    • Batch executions are limited to 10 callouts per method execution.
    • The maximum number of batch executions is 250,000 per 24 hours.
    • Only one batch Apex job's start method can run at a time in an organization. Batch jobs that haven’t started yet remain in the queue until they're started. Note that this limit doesn't cause any batch job to fail and execute methods of batch Apex jobs still run in parallel if more than one job is running.


    Sample Code

    Batch Class :
       global class ExampleBatchClass implements Database.Batchable<sObject>{
    
            global ExampleBatchClass(){
                       // Batch Constructor
            }
           
            // Start Method
            global Database.QueryLocator start(Database.BatchableContext BC){
             return Database.getQueryLocator(query);
            }
          
          // Execute Logic
           global void execute(Database.BatchableContext BC, List<sObject>scope){
                  // Logic to be Executed batch wise      
         
           }
         
           global void finish(Database.BatchableContext BC){
                // Logic to be Executed at finish
           }
        }
    


    Call the Batch Class : 

    ?
    1
    2
    3
    ExampleBatchClass b = new ExampleBatchClass();
    //Parameters of ExecuteBatch(context,BatchSize)
    database.executebatch(b,10);
    Note : if batch size is not mentioned it is 200 by default. 

    For More About Batch Job: For More Info's:

    Saturday 28 May 2016

    Visualforce Form Tags with Examples

    Good Link Study all VF Tags

    http://sfdcsrini.blogspot.com/2014/06/visualforce-form-tags-with-examples.html

    Import csv file using apex visualforce

    Import csv file using apex visualforce

    Normally we use Apex data loader to import data in salesforce from CSV file. Now a days some other tools are also available to load data in salesforce like Jitterbit data loader. But sometime there is requirement when end users do not want to use Apex Data loader. They want some custom page to load data in salesforce. Then in that case we can use custom code to load data in salesforce from csv file.
    Also sometime using single CSV file we want to load data in multiple objects. Then in that case we can not use data loader to load data. In that case we can use custom code to load data in salesforce.
    In the example below we are loading data from CSV file for account objects.
    First of all we need CSV file format. Click here to download CSV file. You can modify CSV file to insert or import account records. CSV file attached has following format:
    Import csv file using apex visualforce
    Now we need to write code in Apex and visualforce which will  read csv file and insert records in account object.
    Click on choose file, then select csv file and then click on ‘Import Account’ button. All records from csv file will be inserted on account records. I have commented insert account line. So this example will only show account records on visualforce page. You can uncomment insert account list line in below code if you want to insert account list.
    Import csv file using apex visualforce
    Visualforce Page:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <apex:page controller="importDataFromCSVController">
        <apex:form >
            <apex:pagemessages />
            <apex:pageBlock >
                <apex:pageBlockSection columns="4">
                      <apex:inputFile value="{!csvFileBody}"  filename="{!csvAsString}"/>
                      <apex:commandButton value="Import Account" action="{!importCSVFile}"/>
                </apex:pageBlockSection>
            </apex:pageBlock>
            <apex:pageBlock >
               <apex:pageblocktable value="{!accList}" var="acc">
                  <apex:column value="{!acc.name}" />
                  <apex:column value="{!acc.AccountNumber}" />
                  <apex:column value="{!acc.Type}" />
                  <apex:column value="{!acc.Accountsource}" />
                  <apex:column value="{!acc.Industry }" />
            </apex:pageblocktable>
         </apex:pageBlock>
       </apex:form>
    </apex:page>
    Apex Code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    public class importDataFromCSVController {
    public Blob csvFileBody{get;set;}
    public string csvAsString{get;set;}
    public String[] csvFileLines{get;set;}
    public List<account> acclist{get;set;}
      public importDataFromCSVController(){
        csvFileLines = new String[]{};
        acclist = New List<Account>();
      }
      
      public void importCSVFile(){
           try{
               csvAsString = csvFileBody.toString();
               csvFileLines = csvAsString.split('\n');
                
               for(Integer i=1;i<csvFileLines.size();i++){
                   Account accObj = new Account() ;
                   string[] csvRecordData = csvFileLines[i].split(',');
                   accObj.name = csvRecordData[0] ;            
                   accObj.accountnumber = csvRecordData[1];
                   accObj.Type = csvRecordData[2];
                   accObj.AccountSource = csvRecordData[3];  
                   accObj.Industry = csvRecordData[4];                                                                            
                   acclist.add(accObj);  
               }
            //insert acclist;
            }
            catch (Exception e)
            {
                ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');
                ApexPages.addMessage(errorMessage);
            
      }
    }