An Introduction to Force.com Apex Code

Abstract

Force.com Apex Code is a strongly-typed programming language that executes on the Force.com platform. Apex is used to add business logic to applications, to write database triggers, and to program controllers in the user interface layer. It has a tight integration with the database and query language, good web services support, and includes features such as futures and governors for execution in a multi-tenant environment.

This article introduces the Apex language. After providing an overview of the syntax, we look at its database support, followed by testing, web services, and dynamic apex. Finally, we look at other consequences of the Apex language executing on the Force.com platform.

After reading this article you will have a good overview of the language and its use on the platform.

Syntax

Apex has a syntax that will be familiar to Java and C# programmers. It has the usual array of features such as classes, interfaces, constants, class variables, and annotations. Unusually, Apex is not case sensitive.

Classes and Interfaces

Here is a simple example of an interface and a class implementing the interface:
  
public interface PurchaseOrder {
  Double discount();
}

public class CustomPurchaseOrder implements PurchaseOrder {

  static final double DEFAULT_DISCOUNT = .05;
	  
  public Double discount() { 
    return DEFAULT_DISCOUNT; 
  } 
	
} 

The implements keyword is used to indicate interface implementation, the static keyword indicates a class variable (or method) accessible without an instance of the class, and the final keyword indicates a constant.


You can create an instance of a class using the new keyword. For example:

PurchaseOrder po = new CustomPurchaseOrder();

Apex supports the standard Java-like notions of this and instanceOf.

Primitive Data Types

Apex supports a number of different data types:

  • primitive data types such as Integer and Date
  • sObject types that represent persistent objects
  • collections and enumerations
  • user and system-defined Apex classes

The primitive data types include:

  • Blob - for storing binary data
  • Boolean
  • Date, Time and Datetime
  • Decimal - for representing arbitrary precession numbers, including currency
  • ID - the Force.com database record identifier type
  • Integer, Long, Double and String

Here are some variable definitions for primitives that should give you a feeling for using these data types:

DateTime dt = System.now() + 1;
Boolean mustI = true;
String s = 'abc'.toUpperCase();
Decimal d = Decimal.valueOf('123');

Note that the String data type uses single quotes, not double quotes.

Collections

Apex supports Sets, Lists and Maps, as well as enumerations. These are pretty straightforward, so we'll just provide a few examples here. Note that the first element of a collection is at index position zero.

A set is an unordered collection of primitives that does not contain any duplicate elements:

Set<String> s = new Set<String>{'a','b', 'c'}; 
s.add('c');
System.assert(s.contains('b'));
System.assert(s.size() == 3);

A list is a collection of elements. Use a list when the sequence is important. You can have duplicates in a list:

List<Integer> myList = new List<Integer>();
myList.add(47);
myList.get(0);

You can also use the array syntax for lists. For example:

String [] colors = new List<String> ();
colors[3] = 'Green';

Maps are collections of key-value pairs, and support a shortcut syntax for populating the collection:

Map<String,String> myStrings = new Map<String,String>{'a'=>'b','c'=>'d'.toUpperCase()}; 
Map<ID,Contact> m = new Map<ID, Contact>([select id, lastname from contact]); 

That last statement contains a sneak peek of the database integration, populating the map with retrieved contact objects, which in turn only have their id and lastname fields populated.

Apex also supports enumerations. The following code creates a new data type called Season, then declares a variable of that data type, and assigns it a value.

public enum Season {WINTER, SPRING, SUMMER, AUTUMN}
Season s = Season.AUTUMN;

Statements and Expressions

Although we've already seen instances of statements and expressions, it's worth showing a few more examples just to give a taste.

Integer count = 1; 
while (count < 11) { 
  System.debug(count); 
  count++; 
} 
for (Integer i = 0, j = 0; i < 10;i++){ 
  System.debug(i+1); 
}  
Integer[] myInts = new Integer[]{1,2,3,4,5,6,7,8,9,10}; 
for (Integer i : myInts) { 
  System.debug(i); 
} 

The last for loop shows how to iterate over a collection. Apex also supports a special loop for iterating over persisted sObject records returned by a query:

String s = 'Acme'; 
for (Account a : [select id, name from account where name like :(s+'%')]){ 
  //Your code 
} 

Note how the query is embedded in square brackets, and how it makes a reference to the variable s.

Exceptions

Apex uses the familiar throw, try, catch and finally statements to handle exception flows. For example:

try { 
  throw new Exception();
} catch(ListException e) { 
  //List Exception handling code here 
} catch(Exception e) { 
  //Generic exception handling code here 
}

Miscellaneous Syntax

Apex supports a couple of annotations. The @future annotation identifies methods that are executed asynchronously, and the @isTest annotation is a class annotation that indicates that all the methods are test methods (covered later).

The following example shows using @future for making an asynchronous web services call:

@future(callout=true) 
public static void doCalloutFromFuture(){ 
    //Executes when the platform has resources
} 

You cannot create your own annotations.

Finally, Apex also supports an abbreviated property syntax. For example, to declare a variable as well as its associated getters and setters, you can write something like the following:

	
public Integer prop { 
  get { return prop * 2; }
  set { prop = value; } 
}

The simplest property definition takes this form:

public Integer anotherProp {get;set;} 

Database Integration

Apex has strong ties with the Force.com persistence layer, the database. In this section we'll show how the language can be used to create, persist and update database objects (called sObjects), as well as query the database and iterate over results. It is also used for writing database triggers. For a more comprehensive introduction to the Force.com database and the services it offers, see An Introduction to the Force.com Database.

Manipulating sObjects

sObjects refers to any object that can be stored in the Force.com platform database. These are not objects in the sense of instances of Apex classes; rather, they are representations of data that has, or will be, persisted.

These persisted objects can be treated as first class citizens within the Apex language, which makes the database integration particularly intuitive and easy to use.

sObject is also the name of the generic abstract type that can be used to represent any persisted object type. Assuming the database has an Account sObject, with fields for name and billingcity, then either of the following two lines will create an sObject:

sObject s = new Account();
Account a = new Account( name='Acme', billingCity='Edinburgh');

Note the optional use of initial field values in the final example. Apex provides a convenient dot notation for accessing fields within an object, so the following will return the identifier and string of an account object:

ID id = a.ID;
String x = a.name;

Dynamic Apex allows you to employ a loosely typed syntax, for example the last statement could be rewritten as:

String x = a.get('name');

Queries and Embedded Queries

The Force.com platform supports two query languages:

  • Salesforce Object Query Language (SOQL) is a query-only language. While similar to SQL in some ways, it's an object query language that uses relationships, not joins, for a more intuitive navigation of data. You'll see an example in a minute.
  • Salesforce Object Search Language (SOSL) is a simple language for searching across all persisted objects.

These languages can be embedded in your Apex code, making data retrieval easy. This code retrieves an sObject (a record from the database) that has the name field assigned to Acme:

sObject s = [select id, name from account where name='Acme'];

This code retrieves all matching accounts (assuming that there'll be zero or more), assigning them to a list:

String myName = 'Acme';
Account [] accts = [select ID from Account where name=:myName]; 

The following code retrieves the first matching account and assigns the annualRevenue field value to a variable:

Double rev= [select annualRevenue from Account where name = 'Acme'][0].annualRevenue; 

If you have an Account and Contact sObject in a relationship, you can traverse the relationship in the query using the dot notation. This code retrieves the name of the related Account sObject via the Contact sObject:

Contact c= [select Account.name from Contact where id =:id];

SOSL statements evaluate to a list of lists of sObjects, where each list contains the search results for a particular sObject type. Here's an example that searches across the name field of all Account and Contact sObjects:

List<List<SObject>> searchList= [FIND 'map*' IN NAME FIELDS RETURNING Account (id, name),Contact]; 
Account [] accounts= ((List<Account>)searchList[0]); 
Contact [] contacts= ((List<Contact>)searchList[1]);

Queries can also be embedded in the special for syntax. This syntax can also be used to loop through batches of returned records, as shown below:

for (Account[] tmp : [select id from Account where name= 'yyy']){ 
   j = tmp.size(); 
  // perform some actions on the accounts
}

Triggers

Triggers are written in Apex, and execute before or after an insert, update, delete or undelete event occurs on an sObject. The syntax that introduces a trigger definition will look a little familiar, and begins with the trigger keyword:

trigger myAccountTrigger on Account (before insert, before update) { 
  if (Trigger.isInsert) {
   //
  } 
  if (Trigger.isUpdate) {
    for(Account a: Trigger.new)
      if (a.name == 'bad') 
        a.name.addError('Bad name'); // prevent update
     }
}

This example fires before any Account sObject is inserted or updated. The Trigger.new context variable provides access to the list of accounts being inserted or updated. Update and delete triggers can use Trigger.old to refer to the old versions of the objects being updated or deleted.

Triggers make full use of Apex, allowing you to continue using a familiar language for data manipulation. There are a few restrictions - for example it doesn't make sense to allow call outs to web services from within a trigger as that would unduly lengthen the transaction.

Data Manipulation Language

Apex code can also contain data manipulation language (DML) operations to retrieve, insert, delete and update data in the database. You can also create and manipulate save points. Here are some examples:

Account[] accounts = new account[] {new Account(name='foo'), new Account(name='bar')};
insert accounts;
Savepoint sp = Database.setSavepoint(); 
delete accounts;
Database.rollback(sp);

Testing

The Force.com platform requires that at least 75% of your Apex classes are covered by testing before code can be deployed to a production system. Ideally, you should strive for 100% coverage. As a result, Apex development usually goes hand in hand with unit test development. This restriction isn't in place in the development edition organization.

In Apex, test methods are denoted with the testMethod keyword. Here's an example of a class that provides a test method:

public class myClass {
  static testMethod void myTest() {
    Account a = new Account(name='foo');
    insert a;
    System.assertEquals('foo', [select name from Account where id=:a.id].name);
    System.assertEquals(1, [select count() from Account where id=:a.id]);
    try {
      delete a;
      Foo.myMethod(a); //call some method
    } catch (DmlException e) {
      System.assert(false);  // assert that we should never get here
    }
 }
}

These tests typically use the System.assert() series of methods to flag code behavior and catch all exceptions.

Apex as a Controller Language

Visualforce is the user interface layer on the Force.com platform. It provides a model-view-controller (MVC) paradigm to creating user interfaces, where incoming web requests (from a browser, say) can be routed to a controller that can perform certain actions, and then display a result.

Visualforce controllers and extensions must be written in Apex. These controllers typically look no different to the Apex code appearing in this article, though they do have access to additional data types, classes, methods and properties. For example, in Visualforce controllers you have access to page parameters, and Visualforce pages themselves are first-class citizens within Apex, allowing you to redirect to various pages, render the pages as PDF and email as a blob, and so on.

Here's a simple controller:

 public class MyController { 
   PageReference where;
   public PageReference dynamicJump() {
     if (ApexPages.currentPage().getParameters().get('p') != null) {
       where = Page.foo;                                           
       where.setRedirect(true);
     } else {
       where = Page.bar;
       where.getParameters().put('p', 'nowHasParam');
     }
     return where;
 }

PageReference is the data type of a Visualforce page, and this code examines the parameters of the current page and redirects to one of two other pages depending on what it finds. Such controllers can also execute DML, query the database and return results for example.

Web Services

Apex makes it easy to invoke external web services, and to expose methods in an externally-available web service endpoint.

Exposing Web Services

Apex methods can easily be exposed as custom Force.com SOAP API calls, allowing them to be called from external applications. For example, the following code exposes a method as a service using the webService keyword:

global class MyWebService { 
  webService static Id makeContact(String lastName, Account a) { 
   Contact c= new Contact(lastName= 'Weissman',AccountId = a.Id); 
   insert c; 
   return c.id; 
  } 
} 

The global access modifier declares that the class is visible to all Apex code, everywhere. Any class that uses Web services must be defined as global.

The web service will be immediately available on the Force.com platform. To call it, you'll simply need the automatically generated WSDL for the service, which can be found by navigating to the class using the Force.com Builder environment, clicking on the name of the class and then clicking the Generate WSDL button. You can feed the generated WSDL document to your language of choice, and call into the Apex Web service on the Force.com platform.

Calling External Web Services

The Force.com platform makes it easy to call an external SOAP web service by automatically generating the support Apex for you. To do this you need to feed the platform the WSDL of the external web service that you want to access, using the Force.com builder environment.

As an example, navigate to Setup-> Develop->Apex Classes and click the Generate from WSDL button. Specify a WSDL document in the wizard. The platform uploads and parses the WSDL. If the WSDL contains supported schemas and types, you can use the resulting Apex classes to call that web service.

For example, the following code makes a call to an external web service:

AcmeServices.grabService gs = new AcmeServices.grabService(); 
String result = gs.doLookup('some data');

As you can see, this is standard Apex - all the hard work lies in the generation of the Apex classes from the WSDL document, which is done for you by the platform.

Dynamic Apex

Dynamic Apex refers to two mechanisms that allow you to construct programs that provide dynamic behavior. In particular, you can, at runtime, introspect sObjects and use this information to react dynamically to the particular object. You can also write dynamic SOQL and SOSL queries, where the queries are constructed at runtime. This section looks at these two items in a little more depth.

Introspecting sObject Information

sObjects contain metadata, such as their name, field names and types, default field values and so on. You can write applications that introspect this information and change the behavior based on what is discovered. There are two types of introspection: compile time introspection, and runtime introspection.

In compile time introspection you create tokens that represent information about the metadata of an sObject. These tokens are serializable and lightweight, and are ideal for many programming needs. Here's an example that determines whether an sObject or list of sObjects is of a particular data type:

//Create a generic sObject variable s 
SObject s = [select id from account limit 1];  
//Verify if that sObject variable is an Account token 
System.assertEquals(s.getSObjectType(), Account.sObjectType); 
//Create a list of generic sObjects 
List<sObject> l = new Account[]{}; 
//Verify if the list of sObjects contains Account tokens 
System.assertEquals(l.getSObjectType(),Account.sObjectType);

You can similarly retrieve a token for a field (in this case AccountNumber) of an sObject:

Schema.SObjectField F = Account.AccountNumber;

Instead of the compile time tokens, you can use runtime calls to a set of getDescribe() methods to introspect sObjects. For example, if you had an Account object that has an Industry field that is a picklist, you can determine the possible values of the picklist, and all child relationships of the sObject, like so:

Schema.DescribeFieldResult f = Account.Industry.getDescribe(); 
List<Schema.PicklistEntry> p = f.getPicklistValues();  
List<Schema.ChildRelationship> c = f.getChildRelationships();

The returned information is quite comprehensive. For example, for a field you can determine everything from whether the field is an ID or a Name field, to whether it's nillable, unique, case sensitive or its precision (if it's of type Double).

Dynamic Queries

Dynamic SOQL refers to the creation of a SOQL string at runtime from within Apex. This article typically uses the compile-time construction, for example:

Contact c = [select Account.name from Contact]; 

You can create a dynamic query by making a call to the query() method on the database, passing in a string (which you can dynamically construct of course). For example, you can run the following dynamic SOQL query:

sObject c = Database.query('select Account.name from Contact limit 1');

The same method can be used for dynamic SOSL queries.

Miscellanea

While Apex provides a very familiar programming environment, with tight integration to a web services stack and database, it's also somewhat unusual in the sense that it runs in the cloud. Compiling an Apex class means somehow getting that class to the Force.com platform, which will in turn compile it and either produce the compiled code (which you don't generally access) or error messages. There are also consequences of running in the cloud in a multi-tenant environment. This section examines these aspects of Apex.

Force.com

You can think of Apex itself as metadata in a complete application, together with the definition of sObjects, workflow rules, triggers and so on. The platform can be fed all this metadata, and it is turned into an application running in the cloud.

Force.com provides a suite of features enabling the use of traditional development practices for creating applications. When creating an application on the Force.com platform you have two main conduits:

  • Force.com lets you configure the platform and all aspects of your application. For example, you can use this Force.com to create sObjects, create Apex classes and view the result.
  • The Metadata API - a web service that lets you access all the metadata associated with your application through a special web services endpoint.

This latter option opens up a host of opportunities. For example, the Force.com IDE provides a traditional Eclipse-based integrated development environment for writing Apex (and other components of the platform). The IDE provides syntax highlighting, compilation, error handling, test method execution and so on, and it does it by invoking the metadata API under-the-hood. To compile a piece of code, it sends the code to the platform, which compiles it and returns any resulting metadata to the IDE.

You can access this metadata more directly through the Force.com Migration Tool or by writing your own interface to the metadata API, providing a number of interesting opportunities. For example, you can migrate code from one environment to another (Force.com provides sandboxes and development environments, as well as production environments) simply by pulling all the metadata from the metadata API, and then pushing it to the other environment using the same API.

Multi-tenancy

The Force.com platform is a multi-tenant platform, which means that the resources used by your application (such as the database) are shared with many other applications. This multi-tenancy has a lot of benefits, and it comes with a small promise on your part. In particular, if you write Apex code, the platform needs to do its best to ensure that it is well behaved.

For example, Apex code that simply loops will not benefit you (or the cloud). This is the reason why Apex, when deployed to a production server, needs 75% code coverage in tests.

The Apex runtime engine also enforces a number of limits to ensure that runaway Apex does not monopolize shared resources. These limits, or governors, track and enforce various metrics. An example of these limits include (at the time of writing): the total stack depth for an Apex invocation in a trigger is 16, the total number of characters in a single String may not exceed 100000, and the total number of records processed as a result of DML statements in a block may not exceed 10000.

There are various precautions that can be taken to ensure that the limits are not exceeded. For example, the batched for loop described earlier lifts certain limits, and different limits apply depending on what originated the execution of the Apex. For example, Apex originating from a trigger is typically more limited than Apex that started running as part of a web services call.

Summary

This article provides an overview of Apex, a modern object-oriented programming language for on-demand computing.

Apex provides a tight integration with the Force.com database, making it very easy to manipulate and query data right from within Apex itself. Apex is also used to code triggers that fire when database objects are manipulated, and it is used in the controllers of the user interface layer provided by Visualforce. Apex also provides simple access to web services. The platform can automatically generate Apex to access external web services, and it's particularly easy to write a web service in Apex itself.

As a language for programming a platform in the cloud, Apex also provides additional facilities such as a comprehensive Metadata API for manipulating Apex and other metadata, as well as traditional development environments such as the Force.com IDE. It also provides governors and limits, and a testing framework to ensure that Apex code is well-behaved in a multi-tenant environment.

For a more comprehensive introduction see the Apex Language Reference below.

References

About the Author

Jon Mountjoy is the community manager and editor-in-chief at Developer Force. He gets kicks out of learning new things and communicating these to the community. You can find Jon on the Developer Force blog, Twitter, FriendFeed and more. Jon says "Thank you to the awesome Platform Documentation team for their great documentation. Thanks also to Rick Greenwald, Leah Cutter and Taggart Matthiesen for their great suggestions and corrections. Please send me any feedback about the article!"