Apex is the official programming language for Salesforce Developers. It is strongly typed object-oriented programming language. The syntax of Apex is very similar to Java programming language.
Features of Apex:
- Apex is a strongly typed object oriented programming language.
- Apex is compiled, stored and it runs on Force.com platform.
- Apex provides built-in support for unit test and execution. In order to deploy any code to production, take note that Salesforce requires 75% code coverage for any code pushed to the Production.
Whenever salesforce developers write and save code on the force.com platform, the apex code is compiled and it is stored in the form of metadata.
Capabilities of Apex:
- Apex provides built in support for the following:
- Apex provides support for DML Calls. DML stands for Data Manipulation language. You can insert, update and delete records in salesforce using DML statements.
- Apex provides support for SOQL and SOSL queries. These statements are used to retrieve data in Salesforce.
- Apex has support for record locking which prevents conflicts during record update.
- Salesforce has in-built support for email services which can be used to send and receive emails.
- Apex can also be generated in web service that can be consumed by 3rd party application
If you want to modify a field name or field type of a field which is being referenced by Apex, Salesforce will send you warnings and errors to prevent the field from getting modified.
When can we use Apex Programming?
- You should only use Apex if functionality cannot be achieved using out-of-the-box features of Salesforce. If something can be done using the point and click application development, you should not write apex code for those things.
- You can use apex to write complex business logic.
- You can use apex if you need to call out an external web-service and process the results.
- If you need to handle emails (incoming or outgoing emails) in way more complex manner than the declarative features, in that case, you can use apex.
Data Types in Apex
Blob – a collection of binary data. Typically used to represent the data of files. Can be converted to String (see String) and vice versa as needed.
Boolean – variables of this type can only be assigned as true, false, or null (see Null).
Date – represents a date and contains no time data.
DateTime – represents a specific point in time. For example, timestamps. Contains information about both date and time.
Note: DateTime values are stored in the force.com database in UTC time. However, when querying this data and displaying on a Visualforce page using the outputField component, the value is calculated based on the current user’s timezone setting on their profile.
Decimal – A number with a decimal point. The default type for Currency fields. Decimal variables have an arbitrary precision (number of digits after the decimal point) which is set when the decimal value is first assigned, unless explicitly set.
Double-A very large number which can include a decimal, with a minimum value of -263 and maximum value of 263-1. Should only be used for very large numbers and complex calculations. See also Long.
ID – Any valid 18-character Force.com record ID
Note: If you set a variable of type ID to a 15-character value, Apex will convert it to the corresponding 18-character ID automatically. Be careful though, this will not work if you set a variable of type string to a 15-character ID, Apex will leave that alone.
Integer – A whole number, meaning there is no decimal point, which can range in value from -2,147,483,648 through 2,147,483,647.
Long – Similar to double, but does not contain a decimal point; it’s the same as an integer, but can be used to represent much larger numbers than an integer. Use this if you need to represent a number outside the range of integers only. Long values can range from -263 through 263-1 (that is a really big number!).
Object – This is a special variable type which can be used to represent any other type, including user-defined objects (we will see this later). However, before actually performing any action on an Object variable, it must be converted (cast) to a specific type.
String – Any set of characters, such as a word, surrounded by quotes.
Note An empty string (no characters) is not the same as a null string. It is perfectly acceptable to assign the value of ” (empty string, not even space) to a string variable—this is simply an empty value.
Time – represents a particular time. Can only be used with system methods (very limited use cases).
Null – Although null is technically not a primitive type per se, it is a special constant value that is used to represent “nothing”. All variables declared, but not assigned a value, have the value of null assigned automatically.
What is Apex class?
A class is a template or blueprint from which objects are created. An object is an instance of a class.
Let’s talk in detail about this class. Think of a class as a blueprint/design for a house, in the blueprint you define your bedrooms, bathrooms, layout of the house but nothing have been constructed yet. +Similarly Class is just a blueprint and just like you use the blueprint to construct houses, similarly we use class to create objects. Every class has three parts: Attributes, Methods and Constructors.
How to create a class in Apex?
public class Employee { //define attributes //define methods //define constructors }
Data Types and Variables:
Variables are typically used to store information. Every variable has a data type which tells us about the type of data we will store in the variable.
Below is the list of data types in Apex:
Primitive data types (Integer,String, Boolean,…etc)
Enum (an enumerated list)
sObjects (sObject, like Account, contact, opportunity or custom object)
Learn more about datatypes here
In order to use a variable in salesforce, we have to first declare the variable.
How to declare a variable in salesforce?
Integer a; //declaring the variable and its type a=25; //intializing the variable with value of 25
You can declare and initialize the variable in the same line as shown below:
integer a=25;
Array & Map in Apex
Arrays are defined by following the basic type name with the [] characters.
Account[] accs
2
=
new
Account[]{};
Maps are what are known as “key/value” pairs. They are collections which index their values using a specific key. So where lists are ordered in the order they are built, with the first element being at index 0, the second at index 1, and so on, maps are indexed by a key you specify.
Both the keys and values in a map can be any data type, meaning they can be primitive, composites, collections, and user-defined.
For example, if you had a map that had a String type as both the key and value, you could add an element to the collection with key “firstName” and value of “Mike”. Then, when you wanted to retrieve the value of “firstName”, you would use “firstName” as the index. Maps are declared by using the keyword map followed by the type of key and the type of value, like map<key, value>.
To declare our map containing the first name value, we would then use
map<String,String> myStringMap.
For example, with the same Account query as used in the list example:
map<ID, Account> myAccountMap = new map<ID, Account>([select ID, name from Account]); //would create a map of your Account records indexed by their corresponding IDs.
Map<String, String> country_currencies = new Map<String, String>(); Map<ID, Set<String>> m = new Map<ID, Set<String>>();
Access Modifiers in Apex
Apex has 4 different types of access modifiers.
- Public
- Private
- Protected
- Global
Global
The method or variable with global access modifier can be used by any apex code that has access to the apex class, not just the apex code in the same application. If you want a method which can be referenced outside of the application, either in SOAP API or by other apex code, in that case, you will use global access modifier. If you have declared a method or variable with the global keyword, you must declare the class with the global keyword as well.
Public
if you are using public access modifier with a method or variable, that means that variable or method can be used by any apex in this application or namespace.
Protected
If you are using protected access modifier with a variable or method then, that method or variable is visible to any inner classes in the defining apex class and also visible to the classes that extend from the defining apex class. You can use this protected modifier with instance methods and member variables.
Private
This is the default access modifier and the method or variable defined with private access modifier is only visible inside the apex class in which they are defined. When you don’t specify any access modifier then private is the default access modifier.