Wednesday, 14 June 2017

Batch class salesforce

Batch apex is an interface given by salesforce to process large amount of data.
As we know, salesforce provides governer limits on data extraction and DML operations in classes.
In a SOQL query we can get only 50000 records and at once we can use only 10000 records in one DML operation.
Total number of DML operations allowed in apex class is 150 hence we can process upto 1500000 records in an apex class.
But to work on these many records in apex class we need to implement various complex loopings that will be tiring and time consuming job.
Also for large scale industries the amount of data is more than 1500000 hence this approach cannot be used effectively.

To overcome this problem, salesforce provides batch apex interface.
This interface allows to process 50 million records without any complex logic and looping to implement.

Batch is divided into 3 methods:- 
1. Query Locator :- this methods runs as soon as batch stats running. This method is used to query the required records from database using database.getQueryLocator.

global Database.queryLocator start(database.batchablecontext BC)
 {
   string query= 'select id, name from account';
   return Database.getqueryLocator(query);
 }

 In the above example first a dynamic query will be created which will then be passed in Database.getqueryLocator as parameter to get records from database.
 
2. Execute :- after fetching th records from database, the records are divided in batches and then each batch is processed in the execute.
   By default the batch size is 200 records but it can increased upto 2000 records in each batch.
   For example if the we get 1000 in query locator and batch size is defined as 200 records, then 5 batches will be created and execute method will process 5 times.
   
   global void execute(database.batchablecontext BC , List<SObject> scope)
{
  List<Account> acc = scope;
  for(Account a: acc)
  {
 
a.name= a.name+'batch class';
 
  }
  
  update acc;
}
In the above example scope variable contains list of records which will be processed in that execute method.
3. Finish :- Once all records are processed and execute is completely over, finish method will be called. This methods can be used in various scenarios like sending the batch status to a user after the records are processed.
    global void finish (database.batchablecontext bc)
{   
}
 
This method executes at the end of each batch process.

Thursday, 1 June 2017

autocomplete in visualforce page

In this blog i will explain how we can use HTML datalist tag in visual force page to create a dropdown for input text box and capture the selected value in controller.
Recently i faced a scenario where i need to show drop down for a input text box and if i write in  text box then relevant options should display in drop down.
This can be achieved using <datalist> tag of html.

A datalist will be created which will store the options in it.

<datalist id="states">
<option value="Delhi"/>
<option value="Kashmir"/>
<option value="UP"/>
<option value="Gujarat"/>
<option value="Karnataka"/>
</datalist>

An input text will be created with list attribute. This attribute bind the input text to the datatable.

<input type="text" list="states" id="inputValue"/>


Now to pass the selected value value to the controller, an inputhidden tag will be used which will bind the selected value to the controller variable.

<apex:inputhidden id="hidden" value="{!selectedState}"/>

on input tag, oninput attribute will be used to call javascript when we select a value from drop down.

<input type="text" list="states" oninput="setValue();"/>
<script>
function setValue(){
   var options = document.getElementById("states").options;
   for(var i=0; i<options.length ; i++)
   {
    if(document.getElementById("inputValue").value == options[i].value
   document.getElementById("hidden").value = document.getElementById("inputValue").value;
   }
   
}
</script>
In the javascript above we are assigning the inputhidden field value to the selected value. This will set the selectedState variable value which further can be used in controller.




Create record in lightning

With the advancement of lightning, we are getting introduced to new features of lightning that helps in creating a better functionality in ...