+ Start a Discussion
SIVAJI G 1SIVAJI G 1 

Please Give Diffrence between Apex And Batch Apex

Hi 
All 
Best Answer chosen by SIVAJI G 1
EldonEldon
hi sivaji,

Apex is a proprietary language which has been developed by Salesforce.com. Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Force.com platform server in conjunction with calls to the Force.com API.

Apex offers multiple ways for running your Apex code asynchronously.Batch apex is a feature that comes in the asynchronous apex section. 

Batch Apex operates over small batches of records, covering your entire record set and breaking the processing down to manageable chunks. For example, a developer could build an archiving solution that runs on a nightly basis, looking for records past a certain date and adding them to an archive. Or a developer could build a data cleansing operation that goes through all Accounts and Opportunities on a nightly basis and updates them if necessary, based on custom criteria.

Refer these links : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_intro.htm
                            https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch.htm

Regards

All Answers

EldonEldon
hi sivaji,

Apex is a proprietary language which has been developed by Salesforce.com. Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Force.com platform server in conjunction with calls to the Force.com API.

Apex offers multiple ways for running your Apex code asynchronously.Batch apex is a feature that comes in the asynchronous apex section. 

Batch Apex operates over small batches of records, covering your entire record set and breaking the processing down to manageable chunks. For example, a developer could build an archiving solution that runs on a nightly basis, looking for records past a certain date and adding them to an archive. Or a developer could build a data cleansing operation that goes through all Accounts and Opportunities on a nightly basis and updates them if necessary, based on custom criteria.

Refer these links : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_intro.htm
                            https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch.htm

Regards
This was selected as the best answer
buggs sfdcbuggs sfdc
HI

Apex is a development platform for building software as a service (SaaS) applications on top of Salesforce.com's customer relationship management (CRM) functionality. Apex allows developers to access Salesforce.com's back-end database and client-server interfaces to create third-party SaaS applications.

Batch Apex:

Using batch Apex classes, you can process records in batches asynchronously. If you have a large number of records to process, for example, for data cleansing or archiving, batch Apex is your solution. Each invocation of a batch class results in a job being placed on the Apex job queue for execution. 

It divides the whole process in batches (each batch handles 200 records at a time).
To handle large number of data we have to use batch apex.
Also used for overcome the governing limits.

Batch apex syntax
//Database is Class provided by Salesforce.
//Batchable is an global interface which is inside of the Database class.
//Batchable include 3 method prototypes: 1. start 2. execute 3. finish
//start and finish methods will execute only one time.
//execute method executes multiple times.
global class BatchUsage implements Database.Batchable <sobject>, Database.Stateful {
 //at a time we can inherit multiple interfaces but we cannot inherit multiple classes.
 //By default batch class is stateless (variable info. store in one method cannot be remembered in other method), 
 //to make it stateful we should inherit Database.Stateful interface.
 String name = '';
 global Database.queryLocator start(Database.BatchableContext bc) {
   
  //From asynchronous to asynchronous we cannot call 
  //(from batch class we cannot call future mehthod vice versa.).
  name += 'start';
  system.debug('@@@Start Method: '+name);
  //Collects the records to process. It will forward these records to execute method.
  //If we use Iterable<sobject> as return type for this start method it can hold 
  //max. of 50k records only.
  //If we use Database.queryLocator as return type for this start method it can hold 
  //max. of 50 million records.
  return Database.getQueryLocator('select id, name, Location__c from Department__c where Location__c = \'\'');
 }
 global void execute(Database.BatchableContext bc, LIST<sobject> sObjLst) {
  name += 'execute';
  system.debug('@@@Execute Method: '+name);
  //Split the records forwarded by start method into batches.
  //Default batch size is: 200.
  //Max. batch size is: 2000.
  List<department__c> depUPdLst = new List<department__c>();
  for(SObject sObj: sObjLst) {
   Department__c dept = (Department__c) sObj;//Type Casting.
   dept.Location__c = 'Bangalore';
   depUPdLst.add(dept);
  }
  if(depUPdLst.size() > 0)
   update depUPdLst;
 }
 global void finish(Database.BatchableContext bc) {
  name += 'finish';
  system.debug('@@@Finish Method: '+name);
  //Post Commit logic like sending emails for the success or failures of the batches.
  AsyncApexJob a = [select id, Status, NumberOfErrors, JobItemsProcessed,
  TotalJobItems, CreatedBy.Email from AsyncApexJob where id =: bc.getJobId()];
   
  Messaging.singleEmailMessage mail = new Messaging.singleEmailMessage();
  mail.setToAddresses(new String[]{a.CreatedBy.Email});
  mail.setSubject('Batch Class Result');
  mail.setHtmlBody('The batch Apex job processed ' + '<b>' + a.TotalJobItems + '</b>' +
  ' batches with '+ '<b>' + a.NumberOfErrors + '</b>' + ' failures.');
  //sendEmail methods
  Messaging.sendEmail(new Messaging.singleEmailMessage[]{mail});
   
  /*** Scheduling in minutes or hours ***/
  /*//Create object for schedulable class
  SchedulableUsage su = new SchedulableUsage();
  //Preparing chron_exp
  Datetime sysTime = System.now();
  sysTime = sysTime.addminutes(6);
  String chron_exp = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' +
  sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();            
  System.schedule('Dep Update'+sysTime.getTime(),chron_exp, su);*/
 }
}
 </department__c></department__c></sobject></sobject></sobject>





 
learn_cloudsflearn_cloudsf
what is the batch class doing ? is it updating  depUPdLst where location is Location__c = \'\'' ? Can you explain in simple terms plz
SIVAJI G 1SIVAJI G 1
Thanks 
Eldon