Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Custom Coding with Apex

Save for later
  • 18 min read
  • 27 Apr 2015

article-image

In this article by Chamil Madusanka, author of the book Learning Force.com Application Development, you will learn about the custom coding in Apex and also about triggers.

We have used many declarative methods such as creating the object's structure, relationships, workflow rules, and approval process to develop the Force.com application. The declarative development method doesn't require any coding skill and specific Integrated Development Environment (IDE).

This article will show you how to extend the declarative capabilities using custom coding of the Force.com platform. Apex controllers and Apex triggers will be explained with examples of the sample application. The Force.com platform query language and data manipulation language will be described with syntaxes and examples. At the end of the article, there will be a section to describe bulk data handling methods in Apex.

This article covers the following topics:

  • Introducing Apex
  • Working with Apex

(For more resources related to this topic, see here.)

Introducing Apex

Apex is the world's first on-demand programming language that allows developers to implement and execute business flows, business logic, and transactions on the Force.com platform. There are two types of Force.com application development methods: declarative developments and programmatic developments. Apex is categorized under the programmatic development method. Since Apex is a strongly-typed, object-based language, it is connected with data in the Force.com platform and data manipulation using the query language and the search language.

The Apex language has the following features:

  • Apex provides a lot of built-in support for the Force.com platform features such as:
    • Data Manipulation Language (DML) with the built-in exception handling (DmlException) to manipulate the data during the execution of the business logic.
    • Salesforce Object Query Language (SOQL) and Salesforce Object Search Language (SOSL) to query and retrieve the list of sObjects records.
    • Bulk data processing on multiple records at a time.
    • Apex allows handling errors and warning using an in-built error-handling mechanism.
    • Apex has its own record-locking mechanism to prevent conflicts of record updates.
    • Apex allows building custom public Force.com APIs from stored Apex methods.
  • Apex runs in a multitenant environment. The Force.com platform has multitenant architecture. Therefore, the Apex runtime engine obeys the multitenant environment. It prevents monopolizing of shared resources using the guard with limits. If any particular Apex code violates the limits, error messages will be displayed.
  • Apex is hosted in the Force.com platform. Therefore, the Force.com platform interprets, executes, and controls Apex.
  • Automatically upgradable and versioned:
    • Apex codes are stored as metadata in the platform. Therefore, they are automatically upgraded with the platform. You don't need to rewrite your code when the platform gets updated. Each code is saved with the current upgrade version. You can manually change the version. It is easy to maintain the Apex code with the versioned mechanism.
  • Apex can be used easily. Apex is similar to Java syntax and variables. The syntaxes and semantics of Apex are easy to understand and write codes.
  • Apex is a data-focused programming language. Apex is designed for multithreaded query and DML statements in a single execution context on the Force.com servers. Many developers can use database stored procedures to run multiple transaction statements on the database server. Apex is different from other databases when it comes to stored procedures; it doesn't attempt to provide general support for rendering elements in the user interface.

    The execution context is one of the key concepts in Apex programming. It influences every aspect of software development on the Force.com platform.

  • Apex is a strongly-typed language that directly refers to schema objects and object fields. If there is any error, it fails the compilation. All the objects, fields, classes, and pages are stored in metadata after successful compilation.
  • Easy to perform unit testing. Apex provides a built-in feature for unit testing and test execution with the code coverage.

Apex allows developers to write the logic in two ways:

  • As an Apex class: The developer can write classes in the Force.com platform using Apex code. An Apex class includes action methods which related to the logic implementation. An Apex class can be called from a trigger. A class can be associated with a Visualforce page (Visualforce Controllers/Extensions) or can act as a supporting class (WebService, Email-to-Apex service/Helper classes, Batch Apex, and Schedules). Therefore, Apex classes are explicitly called from different places on the Force.com platform.
  • As a database trigger: A trigger is executed related to a particular database interaction of a Force.com object. For example, you can create a trigger on the Leave Type object that fires whenever the Leave Type record is inserted. Therefore, triggers are implicitly called from a database action.

Apex is included in the Unlimited Edition, Developer Edition, Enterprise Edition, Database.com, and Performance Edition. The developer can write Apex classes or Apex triggers in a developer organization or a sandbox of a production organization. After you finish the development of the Apex code, you can deploy the particular Apex code to the production organization. Before you deploy the Apex code, you have to write test methods to cover the implemented Apex code.

Apex code in the runtime environment

You already know that Apex code is stored and executed on the Force.com platform. Apex code also has a compile time and a runtime. When you attempt to save an Apex code, it checks for errors, and if there are no errors, it saves with the compilation. The code is compiled into a set of instructions that are about to execute at runtime.

Apex always adheres to built-in governor limits of the Force.com platform. These governor limits protect the multitenant environment from runaway processes.

Apex code and unit testing

Unit testing is important because it checks the code and executes the particular method or trigger for failures and exceptions during test execution. It provides a structured development environment. We gain two good requirements for this unit testing, namely, best practice for development and best practice for maintaining the Apex code. The Force.com platform forces you to cover the Apex code you implemented. Therefore, the Force.com platform ensures that you follow the best practices on the platform.

Apex governors and limits

Apex codes are executed on the Force.com multitenant infrastructure and the shared resources are used across all customers, partners, and developers. When we are writing custom code using Apex, it is important that the Apex code uses the shared resources efficiently. Apex governors are responsible for enforcing runtime limits set by Salesforce. It discontinues the misbehaviors of the particular Apex code. If the code exceeds a limit, a runtime exception is thrown that cannot be handled. This error will be seen by the end user. Limit warnings can be sent via e-mail, but they also appear in the logs.

Governor limits are specific to a namespace, so AppExchange certified managed applications have their own set of limits, independent of the other applications running in the same organization. Therefore, the governor limits have their own scope. The limit scope will start from the beginning of the code execution. It will be run through the subsequent blocks of code until the particular code terminates.

Apex code and security

The Force.com platform has a component-based security, record-based security and rich security framework, including profiles, record ownership, and sharing. Normally, Apex codes are executed as a system mode (not as a user mode), which means the Apex code has access to all data and components. However, you can make the Apex class run in user mode by defining the Apex class with the sharing keyword. The with sharing/without sharing keywords are employed to designate that the sharing rules for the running user are considered for the particular Apex class.

Use the with sharing keyword when declaring a class to enforce the sharing rules that apply to the current user.

Use the without sharing keyword when declaring a class to ensure that the sharing rules for the current user are not enforced. For example, you may want to explicitly turn off sharing rule enforcement when a class acquires sharing rules after it is called from another class that is declared using with sharing.

The profile also can maintain the permission for developing Apex code and accessing Apex classes. The author's Apex permission is required to develop Apex codes and we can limit the access of Apex classes through the profile by adding or removing the granted Apex classes.

Although triggers are built using Apex code, the execution of triggers cannot be controlled by the user. They depend on the particular operation, and if the user has permission for the particular operation, then the trigger will be fired.

Apex code and web services

Like other programming languages, Apex supports communication with the outside world through web services. Apex methods can be exposed as a web service. Therefore, an external system can invoke the Apex web service to execute the particular logic. When you write a web service method, you must use the webservice keyword at the beginning of the method declaration. The variables can also be exposed with the webservice keyword. After you create the webservice method, you can generate the Web Service Definition Language (WSDL), which can be consumed by an external application. Apex supports both Simple Object Access Protocol (SOAP) and Representational State Transfer (REST) web services.

Apex and metadata

Because Apex is a proprietary language, it is strongly typed to Salesforce metadata. The same sObject and fields that are created through the declarative setup menu can be referred to through Apex. Like other Force.com features, the system will provide an error if you try to delete an object or field that is used within Apex. Apex is not technically autoupgraded with each new Salesforce release, as it is saved with a specific version of the API. Therefore, Apex, like other Force.com features, will automatically work with future versions of Salesforce applications. Force.com application development tools use the metadata.

Working with Apex

Before you start coding with Apex, you need to learn a few basic things.

Apex basics

Apex has come up with a syntactical framework. Similar to Java, Apex is strongly typed and is an object-based language. If you have some experience with Java, it will be easy to understand Apex. The following table explains the similarities and differences between Apex and Java:

Similarities

Differences

Both languages have classes, inheritance, polymorphism, and other common object oriented programming features

Apex runs in a multitenant environment and is very controlled in its invocations and governor limits

Both languages have extremely similar syntax and notations

Apex is case sensitive

Both languages are compiled, strongly-typed, and transactional

Apex is on-demand and is compiled and executed in the cloud

 

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at $19.99/month. Cancel anytime

Apex is not a general purpose programming language, but is instead a proprietary language used for specific business logic functions

 

Apex requires unit testing for deployment into a production environment

This section will not discuss everything that is included in the Apex documentation from Salesforce, but it will cover topics that are essential for understanding concepts discussed in this article. With this basic knowledge of Apex, you can create Apex code in the Force.com platform.

Apex data types

In Apex classes and triggers, we use variables that contain data values. Variables must be bound to a data type and that particular variable can hold the values with the same data type.

All variables and expressions have one of the following data types:

  • Primitives
  • Enums
  • sObjects
  • Collections
  • An object created from the user or system-defined classes
  • Null (for the null constant)

Primitive data types

Apex uses the same primitive data types as the web services API, most of which are similar to their Java counterparts. It may seem that Apex primitive variables are passed by value, but they actually use immutable references, similar to Java string behavior. The following are the primitive data types of Apex:

  • Boolean: A value that can only be assigned true, false, or null.
  • Date, Datetime, and Time: A Date value indicates particular day and not contains any information about time. A Datetime value indicates a particular day and time. A Time value indicates a particular time. Date, Datetime and Time values must always be created with a system static method.
  • ID: 18 or 15 digits version.
  • Integer, Long, Double, and Decimal: Integer is a 32-bit number that does not include decimal points. Integers have a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647. Long is a 64-bit number that does not include a decimal point. Use this datatype when you need a range of values wider than those provided by Integer. Double is a 64-bit number that includes a decimal point. Both Long and Doubles have a minimum value of -263 and a maximum value of 263-1. Decimal is a number that includes a decimal point. Decimal is an arbitrary precision number.
  • String: String is any set of characters surrounded by single quotes. Strings have no limit on the number of characters that can be included. But the heap size limit is used to ensure to the particular Apex program do not grow too large.
  • Blob: Blob is a collection of binary data stored as a single object. Blog can be accepted as Web service argument, stored in a document or sent as attachments.
  • Object: This can be used as the base type for any other data type. Objects are supported for casting.

Enum data types

Enum (or enumerated list) is an abstract data type that stores one value of a finite set of specified identifiers. To define an Enum, use the enum keyword in the variable declaration and then define the list of values. You can define and use enum in the following way:

Public enum Status {NEW, APPROVED, REJECTED, CANCELLED}

The preceding enum has four values: NEW, APPROVED, REJECTED, CANCELLED. By creating this enum, you have created a new data type called Status that can be used as any other data type for variables, return types, and method arguments.

Status leaveStatus = Status. NEW;

Apex provides Enums for built-in concepts such as API error (System.StatusCode). System-defined enums cannot be used in web service methods.

sObject data types

sObjects (short for Salesforce Object) are standard or custom objects that store record data in the Force.com database. There is also an sObject data type in Apex that is the programmatic representation of these sObjects and their data in code. Developers refer to sObjects and their fields by their API names, which can be found in the schema browser.

sObject and field references within Apex are validated against actual object and field names when code is written. Force.com tracks the objects and fields used within Apex to prevent users from making the following changes:

  • Changing a field or object name
  • Converting from one data type to another
  • Deleting a field or object
  • Organization-wide changes such as record sharing

It is possible to declare variables of the generic sObject data type. The new operator still requires a concrete sObject type, so the instances are all specific sObjects. The following is a code example:

sObject s = new Employee__c();

Casting will be applied as expected as each row knows its runtime type and can be cast back to that type. The following casting works fine:

Employee__c e = (Employee__c)s;

However, the following casting will generate a runtime exception for data type collision:

Leave__c leave = (Leave__c)s;

sObject super class only has the ID variable. So we can only access the ID via the sObject class.

This method can also be used with collections and DML operations, although only concrete types can be instantiated. Collection will be described in the upcoming section and DML operations will be discussed in the Data manipulation section on the Force.com platform. Let's have a look at the following code:

sObject[] sList = new Employee__c[0];
List<Employee__c> = (List<Employee__c>)sList;
Database.insert(sList);

Collection data types

Collection data types store groups of elements of other primitive, composite, or collection data types. There are three different types of collections in Apex:

  1. List: A list is an ordered collection of primitives or composite data types distinguished by its index. Each element in a list contains two pieces of information; an index (this is an integer) and a value (the data). The index of the first element is zero. You can define an Apex list in the following way:
    List<DataType> listName = new List<DataType>();
    List<String> sList = new List< String >();
  2. There are built-in methods that can be used with lists adding/removing elements from the end of the list, getting/setting values at a particular index, and sizing the list by obtaining the number of elements. A full set of list methods are listed at http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_list.htm. The Apex list is defined in the following way:
    List<String> sList = new List< String >();
    sList.add('string1');
    sList.add('string2');
    sList.add('string3');
    sList.add('string4');
    Integer sListSize = sList.size(); // this will return the   value as 4
    sList.get(3); //This method will return the value as   "string4"

    Apex allows developers familiar with the standard array syntax to use that interchangeably with the list syntax. The main difference is the use of square brackets, which is shown in the following code:

    String[] sList = new String[4];
    sList [0] = 'string1';
    sList [1] = 'string2';
    sList [2] = 'string3';
    sList [3] = 'string4';
    Integer sListSize = sList.size(); // this will return the   value as 4

    Lists, as well as maps, can be nested up to five levels deep. Therefore, you can create a list of lists in the following way:

    List<List<String>> nestedList = new List<List<String>> ();
  3. Set: A set is an unordered collection of data of one primitive data type or sObjects that must have unique values. The Set methods are listed at http://www.salesforce.com/us/developer/docs/dbcom_apex230/Content/apex_methods_system_set.htm.

    Similar to the declaration of List, you can define a Set in the following way:

    Set<DataType> setName = new Set<DataType>();
    Set<String> setName = new Set<String>();

    There are built-in methods for sets, including add/remove elements to/from the set, check whether the set contains certain elements, and the size of the set.

  4. Map: A map is an unordered collection of unique keys of one primitive data type and their corresponding values. The Map methods are listed in the following link at http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_map.htm.

    You can define a Map in the following way:

    Map<PrimitiveKeyDataType, DataType> = mapName = new   Map<PrimitiveKeyDataType, DataType>();
    Map<Integer, String> mapName = new Map<Integer, String>();
    Map<Integer, List<String>> sMap = new Map<Integer,   List<String>>();

    Maps are often used to map IDs to sObjects. There are built-in methods that you can use with maps, including adding/removing elements on the map, getting values for a particular key, and checking whether the map contains certain keys. You can use these methods as follows:

    Map<Integer, String> sMap = new Map<Integer, String>();
    sMap.put(1, 'string1'); // put key and values pair
    sMap.put(2, 'string2');
    sMap.put(3, 'string3');
    sMap.put(4, 'string4');
    sMap.get(2); // retrieve the value of key 2

Apex logics and loops

Like all programming languages, Apex language has the syntax to implement conditional logics (IF-THEN-ELSE) and loops (for, Do-while, while). The following table will explain the conditional logic and loops in Apex:

IF

  • Conditional IF statements in Apex are similar to Java.
  • The IF-THEN statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true.
  • The IF-THEN-ELSE statement provides a secondary path of execution when an IF clause evaluates to false.
if (Boolean_expression){
statement;
statement;
statement;
statement;}
else {
statement;
statement;}

For

  • There are three variations of the FOR loop in Apex, which are as follows:
FOR(initialization;Boolean_exit_condition;increment)
{
    statement;
}
 
FOR(variable : list_or_set)
{
    statement;
}
 
FOR(variable : [inline_soql_query])
{
    statement;
}
  • All loops allow for the following commands:
  • break: This is used to exit the loop
  • continue: This is used to skip to the next iteration of the loop

While

  • The while loop is similar, but the condition is checked before the first loop, as shown in the following code:
while (Boolean_condition) {
code_block;
};

Do-While

  • The do-while loop repeatedly executes as long as a particular Boolean condition remains true.
  • The condition is not checked until after the first pass is executed, as shown in the following code:
do {
//code_block;
}
while (Boolean_condition);

Summary

In this article, you have learned to develop custom coding in the Force.com platform, including the Apex classes and triggers. And you learned two query languages in the Force.com platform.

Resources for Article:


Further resources on this subject: