Salesforce Tutorial# 16: Learning Salesforce Apex and Deployment Tools
Apex is the backbone of Salesforce development. In this article, we will see a few points that will help you in Learning Salesforce Apex.
Introduction of Salesforce Apex Classes:
Apex is more similar to Java and C# programming language with a set of features like constants, classes, class variables, interfaces, and annotations.
Classes and Interfaces
Following snapshot of code explains the sample example of an interface, remember Apex is not case sensitive.
1 2 3 4 5 6 7 |
public interface Order{ Double TotalBill(); Double getDiscount(); } |
Let’s define one class implementing this interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Customer implements Order { public double Total_Bill = 0.0; static final double discount = 0.7; public double TotalBill() { return Total_Bill; } public double getDiscount() { return discount; } } |
Similar to Java implement keyword is used for interface implementation. The meaning static, final is similar to java programming language. The class instance can be created as follows:
1 |
Order my_order = new Customer(); |
The data in apex is divided into the following types:
- Primitive data types like Integer, double, long, String, Blob – for storing binary data, Boolean, Date, Time and Date-time, Decimal – for representing arbitrary procession numbers, including currency, ID – the Force.com database record identifier type.
- sObject types that represent persistent objects
- Collections (Sets, Lists, Maps) and enumerations
- User and system-defined Apex classes
- Statements (While, For loops, if) and Expression
- Property to declare a variable with its getter and setter
- Apart from this Apex supports Exception handling with the same try, catch and finally mechanism.
Miscellaneous Syntax
Following annotation are used with methods:
@future: – this is used to represent asynchronous executions of methods
@isTest:- This is class annotation and represents that all the methods are a test
Invoke a class or its method via Triggers
This is similar to call a native method from other programming languages. The method is defined in the Apex class can be a static or non-static method. We will create an instance of the class inside trigger to call its method or static methods that can be invoked by class name only.
1 2 3 4 5 6 7 8 |
public class triggerInvokeExample { public static void yourMethod(){ // method definition here } } |
Trigger definition:
1 2 3 |
Trigger testTrigger on Order{before insert) { triggerInvokeExample.yourMethod(); } |
Difference between Controller & Extension
A user define controller is known as a custom controller, this is an Apex class having a logic that doesn’t enforce the permissions and take care of field-level security of the current user.
Extensions are used to extend the functionality of the controller. Following are a few scenarios to use controller extensions:
- You want to make flexible the built-in functionality of a standard controller but already override one or more actions like edit, view, save, or delete.
- If new actions need to be added
- Build a Visualforce page that secures user permissions. Let’s consider the scenario that a controller extension class executes in system mode, and controller extension extends a standard controller, still, the logic from the standard controller does not execute in system mode but it executes in user mode and the permissions, level of security, sharing rules will be applied as per current user.
Introduction of test class & methods to cover Apex
Apex Code has a facility for the creation and execution of unit tests for robust and error-free code development. These unit tests are composed of test methods and classes which execute the verification for a piece of code working properly.
Deployment Tools:
Salesforce support following deployment tool to make data migration more comfortable. A tool like Change Set is used to migrate data between sandbox and production.
Force.com IDE (Eclipse Plugin)
The Force.com IDE is based on Eclipse platform and supports many API and integrated with the development environment letting you code, compile, and deploy your application.
Force.com Migration Tool (Ant-Based)
The Ant Migration Tool is a command-line utility developed in Java for moving metadata between a local directory and a Salesforce. This tool can be used in the following scenarios.
- When a project needs a lot of setup changes and it’s a time-consuming activity to make these changes using the web interface, then we need a migration tool
- When your product has multistage release processes. The normal development process requires iterative building, testing, and staging before releasing a product.
- The migration tool plays an important role when there is repetitive deployment using the same parameters
- Migrating from stage to production is a need to done.
- When we need to schedule batch requirements.
Summary
This article gives you head start in Learning Apex. It has briefly covered the deployment tools offered by Salesforce. If you wish to refer previous articles then do check our other articles in the series.
Tutorial Index
- Introduction to Cloud Computing (Salesforce.com and Force.com)
- Overview of Database Concepts (Salesforce.com)
- Introduction to Force.com
- Building Salesforce Custom App and Objects
- Object Relationships and Formula Field in Salesforce
- Salesforce Security Model and Overview
- Automation in Salesforce
- Approval Process in Salesforce
- Introduction to SOQL and SOSL
- Introduction to Apex
- Salesforce Data Management
- Visualforce MVC Architecture on Cloud
- Salesforce Reports and Dashboards
- Building a Visualforce (Custom) Page for the Salesforce App
- Salesforce Sandbox and Overview of Force.com capabilities
- Learning Apex and Deployment Tools