Salesforce Apex Developer's Guide

This guide is written per SFv31, so if you're working on a future version -- you may have a slightly different menu structure. If you are new to Salesforce, you may want to create an account at Salesforce Developer site. This will allow you to create a developer ORG, which you may use to develop SF applications.

What is Apex?

Apex is a case-insensitive and stripped version of Java programming language without standard Java libraries (except for Math) but adds execution of SOQL database language within the Apex language. Apex is designed to run within the Force.com platform, and it enables developers to add business logic to vents, buttons, triggers, and Visualforce pages.

Development Environment

As a developer, you may develop Apex code in the Salesforce UI, Developer Console, or in the Force.com IDE (Eclipse with force.com plugin). Apex cannot be authored in a production environment, so a Dev or Sandbox ORG should be created to author the Apex code.

When do you use Apex?

Things that can't be done or are very complicated to use declarative interface such as Workflow is a good starting point for using Apex code. Also, adding a visual interface and handling click events on the Visualforce pages as well as providing Web Service API to external interfaces. The list below is the typical use cases of Apex.

  1. Field update on related records
  2. Data-driven sharing to grant, modify, or revoke user/group access
  3. Multi-step wizards through Visualforce pages and controllers.
  4. Callout to external web service
  5. Provide custom web service
  6. Automated record creation/deletion and cleanse/Repair of existing records
  7. Custom Email Handler
  8. Custom Button Actions
  9. Complex Validation Rules

Apex Language Constructs

1. Primitive Data Types - Unlike Java programming language, Apex has the following "object"-like primitive data types.

    Blob, Boolean, Date, Datetime, Decimal, Double, ID, Integer, Long, String, Time  (Date and Time data type are essentially the Datetime type with Time or Date portion set to 00:00:00 or 00-00-0000).
    *Note: A string is enclosed in a single quote like 'Web Traffic Exchange' which is different than Java's double quote.

2. Objects - sObject is the parent class of all standard and custom Apex objects.

3. Collections - Collections include List, Set, and Map. A list is NOT an array, but it can be referenced as an array.

Apex Class Definition

  private | public | global   [virtual | abstract]   [with sharing | without sharing]   class ClassName [implements InterfaceNameList | (none)]          [extends ClassName | (none)] { }

Note 1: By default, Apex classes are NOT extendable so use the virtual keyword to make the class extendable.

Note 2: Use the override keyword to create a new definition of a method defined as a virtual method in a base class.

Access Modifiers For Classes

  1. private - private classes cannot be referenced outside of a class
  2. public - public classes are available within the same ORG
  3. global - global classes can be called anywhere including web service API

Access Modifiers for Class Members

  1. global
  2. public
  3. protected
  4. private (default)

For more information about Apex Language syntax, please consult Apex Code Cheatsheet.

Variable Scope

Apex doesn't really have an Application, Session, or Page scope. Instead, Apex codes are run within the Transaction Scope. An Apex Transaction represents a set of operations that are executed as a single synchronous unit and also represents the unit of rollback for DML operations. Static variables are transaction-scoped, and they do go out of scope at the end of each transaction.

Governor Limits

Apex runs within the Cloud, and the platform offers multi-tenancy. To limit a single ORG from consuming large resources, a Governor Limit is placed to limit run-time resources for each ORG and transaction. Governor Limit monitors platform resources assigned to each ORG such as memory, database, and transaction. Governor Limit enables multi-tenancy by ensuring that resources are fairly allocated to each tenant (per their SF License) and terminates Apex execution with run-time exceptions when the limit is reached. For more information on governor limits, please refer Salesforce Governor Limits Quick Reference Guide.

Examples of Apex Governor Limits:

  1. Total number of SOQL queries that can be executed within a transaction
  2. Number of DML Statements that can be executed within a transaction
  3. Total heap size per transaction
  4. Total number of HTTP or Web Service callouts in a transaction

The first two things we need to do before we begin our development are (1) update the email address on your profile, and (2) modify the valid IP ranges for the administrator profile.

1. Reset your email address on your Salesforce profile - SF generates various emails for notification purposes. By updating your email address, you will be getting all those notifications.

    (A)  Login to your salesforce account by using (live/developer) http://login.salesforce.com or (sandbox) http://test.salesforce.com URL.
    (B)  Click Your Name | My Settings | Personal | Advanced User Details | Edit, then update your profile details including your email address.

2. Modify the valid IP ranges for the administrator profile. This will enable you to access the Salesforce from the whitelisted IP address(es).

    (A)  Click Setup | Manage Users | Profiles | System Administrator.
    (B)  Scroll down to Login IP Ranges and click New.
    (C)  If you wish to login from anywhere, use START: 0.0.0.0 and END: 255.255.255.255

FAQ

I was able to make the change in Sandbox and tested working but I'm having a problem with SF deployment in production. Here is the screenshot of the deployment error:

In order to deploy the change from Sandbox to Production, you'll have to set up Setup -> Deploy -> Deployment Connections. Set up an Inbound connection to Production from Sandbox as shown in the screenshot below.

You will also have to SELECT a company in order to transfer change from Sandbox to Production. **Note: This isn't taught in the DEV-541 class, but it was necessary for us to successfully transfer the change without failures. This option is not available on Sandbox.

(a) Navigate to + menu on the tab, and click on the Select Company.

(b) Select Company screen appears as below. Checkmark the box, and Save.

(c) In the case above, the error is from the FinancialForce managed package. The package couldn't have been deployed if there was an error, so it's possible that the person deploying the new change may not have licenses to "Managed Packages". Please ensure that you have all necessary licenses to carry out the deployment. To verify your licensing requirements, go to Setup -> Build -> Installed Packages. Click on the Manage Licenses next to the Package Name.

Share this post

Comments (0)

    No comment

Leave a comment

All comments are moderated. Spammy and bot submitted comments are deleted. Please submit the comments that are helpful to others, and we'll approve your comments. A comment that includes outbound link will only be approved if the content is relevant to the topic, and has some value to our readers.


Login To Post Comment