SF Learners Hub


Share This Post

Apex code typically contains many things that you might be familiar with from other programming languages.

Programming elements in Apex

This Image will describe the basic functionality of Apex, as well as some of the core concepts.

Using Version Settings

This setting indicates not only the version of SOAP API to use but which version of Apex as well. You can change the version after saving. Every class or trigger name must be unique. You cannot save the same class or trigger against different versions.

You can also use version settings to associate a class or trigger with a particular version of a managed package that is installed in your organization from AppExchange.

Naming Variables, Methods, and Classes

Classes start with a capital letter, methods start with a lowercase verb, and variable names should be meaningful.

Using Variables and Expressions

Apex is a strongly-typed language. Apex data types include basic types such as Integer, Date, and Boolean, as well as more advanced types such as lists, maps, objects, and sObjects.

Variables are declared with a name and a data type. You can assign a value to a variable when you declare it. You can also assign values later. Use the following syntax when declaring variables:

// The following variable has the data type of Integer with the name Count
// and has the value of 0.
Integer Count = 0;
// The following variable has the data type of Decimal with the name Total
// that no value has been assigned to it.
Decimal Total;
// The following variable is an account, which is also referred to as an sObject
Account MyAcct = new Account();

In Apex, all primitive data types arguments, such as Integer or String, are passed into methods by value. When the method returns, the changes to the arguments are lost.

Non-primitive data type arguments, such as sObjects, are passed into methods by reference. Therefore, when the method returns, the passed-in argument still references the same object as before the method call. Within the method, the reference can’t be changed to point to another object, but the values of the object’s fields can be changed.

Using Collections

Apex has the following types of collections:

  • Lists (arrays): A list is a collection of elements, such as Integers, Strings, objects, or other collections. Use a list when the sequence of elements is important. You can have duplicate elements in a list.

To create a list:

  1. Use the new keyword
  2. Use the List keyword followed by the element type contained within <> characters.

Use the following syntax for creating a list:

List<Account> listOfAccount = new List<Account>();

For more information, see Lists.

  • Maps: A map is a collection of key-value pairs. Keys can be any primitive data type. Values can include primitive data types, as well as objects and other collections. Use a map when finding something by key matters. You can have duplicate values in a map, but each key must be unique.

To create a map:

  1. Use the new keyword
  2. Use the Map keyword followed by a key-value pair, delimited by a comma and enclosed in <> characters.

Use the following syntax for creating a map:

Map<Integer, String> My_Map = new Map<Integer, String>{};
  • Sets: A set is a collection of unique, unordered elements. It can contain primitive data types, such as String, Integer, Date, and so on. It can also contain more complex data types, such as sObjects.

To create a set:

  1. Use the new keyword
  2. Use the Set keyword followed by the primitive data type contained within <> characters

Use the following syntax for creating a set:

Set<String> setOfString = new Set<String>{};

For more information, see Sets.

Using Loops

Apex supports the following types of loops:

  • Do-while -: A Do-while loop checks the condition after the code has been executed.
  • While -: A While loop checks the condition at the start before the code executes.
  • For -: A For loop enables you to more finely control the condition used with the loop. For loops that use lists and SOQL queries as part of the condition.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

The Power of Salesforce Integration

What You’ll Learn What Is Salesforce Integration? Salesforce integration involves combining the data and features of Salesforce with another application, creating a cohesive experience for