Quick Reference Study Notes for Salesforce Apex Programming (Advanced)


Salesforce Apex Programming

An Apex is an Object-oriented programming language developed by Salesforce.com that allows developers to build their custom functionality and enhance the inbuilt/Standard Salesforce functionality.

Features of Apex Language:

  • It has Java like syntax. For example: Variable Declarations, loop syntax etc.

  • I has built in support for DML operations such as  INSERT, UPDATE, DELETE etc. and it execute multiple queries or DML operations easily. It is also used to query the Schema of the Objects and their fields details etc.

  • It has built in Exception Handling functions which are used to recognize the exceptions occur during the execution of code.

  • It updates automatically whenever the Salesforce updates releases. We don’t need to upgrade it manually.

  • We can execute an Apex class from Button, Custom Link, Visualforce Page, Trigger, Process Builders, Flows and web services etc.

  • Salesforce provide built in support for testing the apex code. We can also recognize the code coverage while testing the particular apex class.


 

Syntax:

private | public | global [virtual | abstract | with sharing | without sharing]
class ClassName [implements InterfaceNameList] [extends ClassName]
{
// The body of the class(Variables and methods)
}

 

Example:

 

public class testClass {
Public String testVar {get; set;}  // Variable Declaration

 

Public void testMethod(){  // function definition

system.debug(‘New Apex Class’);  // print something in the Debug console

}   

}

 

  • Access Modifiers: There are three Modifiers which are used to maintain the Apex class visibility/accessibility:

    • Private: This access modifier declares that the class is not visible/accessible to another apex class. It is the default access modifier for the inner apex class(class within another class).

    • Public: It declares that the class is visible/accessible within our application.

      • Global: It declares that the class is accessible to all Apex code/classes everywhere.If we declare a global method or inner class, then outer(top-level) class must also be defined as global.

 

  • With sharing and without sharing keywords: Generally, the Apex class is always execute in System mode. Which means when this class is executed, it allows user to execute all the code and modify all the data(user can even update those field/objects for which user has no permissions to edit. ). These keywords specify the sharing mode for this class:

    • With sharing: It is used when declaring a class to enforce the sharing rules that apply to the current user. That means class is executed in user mode.

    • Without sharing: It is used when declaring a class to ensure that the sharing rules for the current user are not enforced. That means class is executed in system mode. It is the default keyword of the Apex class.

 

Invoke Apex class from another class:

To invoke the apex class from another class, first, we have to create an object of the class and then we can access the variables and methods of that class. For example: we are creating the object of above class(testClass):

 

testClass classObj = new testClass();  // create object of apex class

classObj.testMethod();  // execute method of that class

Batch Apex Class:

Batch class is an apex class that uses Salesforce standard Interfaces like Database.Batchable, Schedulable, etc. A Batch apex class must implement Database.Batchable Interface and It must be global class. Database.Batchable Interface also provides the BatchableContext variable which we can use to get more information about the Batch which is executing. Batch class include three methods as shown below:

  1. Start: This method collects all the batches of the records that are passed to execute. When we invoke the the batch class, then it executes start method and this method is executed only once.

  2. Execute: This method executes one batch at a time and each batch is processed independently. Every Batch transaction starts with a new set of governor limits. If one batch fails to process successfully, all other successful batch transactions aren’t rolled back.The default batch size is 200 record.

  3. Finish: This method executes after execution of all batches(i.e execution of the Execute Method) and it is also executed only once.

Batch Apex Class Syntax and Example:

Syntax:

 

global class batchExample implements Database.Batchable<sObject> {

//describe all batchable class method

 global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {

      // collect the batches of records or objects to be passed to execute

  }

 

  global void execute(Database.BatchableContext bc, List<P> records){

      // process each batch of records

  }

  global void finish(Database.BatchableContext bc){

      // execute any post-processing operations

  }  

}

Example: To update the contact records:

Global class batchExample implements Database.Batchable<sObject> {

  global Database.QueryLocator start(Database.BatchableContext BC) {         

       String query = 'SELECT Id,Name, Email FROM Contact;

       return Database.getQueryLocator(query); // execute SOQL query to get all contact records

   }

       global void execute(Database.BatchableContext BC, List<Contact> contactList) {

       // process each batch of contact records   

       for(Contact con : contactList){    

              // Update the Contact Email

              acc.Email = ‘test@gmail.com’;

  }

       try {

    // Update the Contact Records

           update contactList;

       } catch(Exception e) {

           System.debug(‘ERROR=====’+e);

       }

   }

   global void finish(Database.BatchableContext BC) {

       // execute any post-processing operations.

 }

}

 

How to invoke Batch apex class from normal apex class:

To invoke a batch class, create the object/instance of that class and then call Database.executeBatch with that instance:

batchTestClass bt = new batchTestClass();

database.executeBatch(bt);

Or

database.executeBatch(bt, 100); // you can also set the batch size in the second parameter of this method i.e number of records to be passed in each batch.


*NOTE : "This study material is collected from multiple sources to make a quick refresh course available to students."

This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy. More info. I Agree