Variables Data Types – Apex Programming – Salesforce Certified Platform Developer I Study Guide

4.3  Variables Data Types

As the data types of variables in Apex are generally strongly typed, you must declare the data type when you declare a variable. Declaring a variable means defining a container for holding data. When you declare a variable, you are essentially creating a named storage location in the computer’s memory. In Apex, you can declare a variable like this:

DataType variableName;

Here, DataType represents the type of data the variable can hold, such as Integer, String, Boolean, etc., and variableName is the name you give to the variable. Assigning a value to a variable means storing a specific piece of data, of the appropriate data type, into the variable.

If you do not assign a value to a variable in Apex, it defaults to null. It essentially means the variable is empty or undefined. Apex starting calculations from 0, it’s important to note that null is not the same as 0. Null means the variable has no value assigned to it, whereas 0 is a specific numerical value.

Data types define the kind of values that variables can hold. Data types help ensure that values are stored and processed correctly in your code. Apex supports several types of data, including primitive data types, collections, and special types for working with Salesforce records.

You can test the following provided code examples for the variables in Apex Anonymous Window. Setup ➤ Developer Console ➤ in Developer Console go to menu Debug ➤ Open Execute Anonymous Window put the code block and press on Execute button.

Primitive data types represent basic values including

Integer: A 32-bit number that does not contain any decimal points. Its value ranges between -2,147,483,648 and 2,147,483,647.

Integer myNumber;

myNumber = 5;

System.debug( myNumber);

Long: 64-bit number without any decimal point used when a range of values greater than an integer. It has a minimum value of -2^63 and a maximum value of 2^63-1.

Long myLongNumber;

myLongNumber = 1234567890L;

System.debug( myLongNumber);

Decimal: A decimal number 32-bit number with a decimal point. Decimal is used as default type by currency fields for financial calculations because it provides more precision than Double.

Decimal myDecimalNumber;

myDecimalNumber = 123.45;

System.debug( myDecimalNumber);

Double: A 64-bit number that can contain decimal points. Used for very large numbers which can include a decimal, with minimum value of -2^63 and a maximum value of 2^63-1.

Double myDoubleNumber;

myDoubleNumber = 3.14159;

System.debug( myDoubleNumber);

Boolean: Represents true or false values. You can use Boolean variables in conditional statements and logical operations to control the flow of your Apex code. Boolean variables are fundamental in programming and are commonly used for decision-making in various contexts within your code.

Boolean isTrue;

isTrue = true;

if (isTrue) {

// Code to be executed if isTrue is true

}

else {

// Code to be executed if isTrue is false

}

System.debug( istrue);

String: Textual data type used to store sequences of characters, such as text or words. String myString;

myString = ‘Hello, World!’;

System.debug( myString);

You can manipulate strings in various ways, such as concatenation, substring extraction, and searching for specific characters or patterns.

Date: Represents a date value.

Date myDate;

myDate = Date.today();

System.debug( myDate);

You can perform various operations with date variables in Apex, such as comparing dates, calculating the difference between dates, and formatting dates into strings for display purposes.

Datetime: Represents a date and time. The myDatetime variable will hold the current date and time in the format “yyyy-MM-dd HH:mm:ss.SSSZ”.

Datetime myDatetime;

myDatetime = Datetime.now();

System.debug( myDatetime);

Blob: A collection of binary data including images, files, or any other type of data that is not text-based, stored as a single object.

Blob myBlob;

// Create an empty Blob

Blob emptyBlob = Blob.valueOf(”);

System.debug(myBlob);

Remember the difference between null or 0 as we discussed at the beginning of the chapter.

ID: As alphanumeric identifier is set to a 15-character value but Apex automatically converts it to the corresponding 18-character ID.

ID MyID = ‘001xx000003DGbKAAW’;

System.debug(MyID);

Object: The main purpose of using the Object variable in Apex is to provide a generic type that can hold any type of object. It allows for flexibility in handling different data types without specifying a specific type upfront. This variable is useful for casting operations and scenarios where you need a generic type that can hold any object.

  • Assign an Integer to an Object variable Object obj = 42;
  • Cast the Object variable to an Integer and perform operations Integer num = (Integer)obj;

Integer square = num * num;

System.debug(‘Number: ‘ + num);

System.debug(‘Square: ‘ + square);

SObject: In Apex, the sObject is a fundamental data type that represents an object in Salesforce’s data mode in connection with metadata. It’s a generic type used to work

with records of any standard or custom object in the Salesforce database. An sObject is a data type in Salesforce Apex that represents a row of data and can only be declared using the SOAP API name of the object. Every Salesforce record is natively represented as an sObject in Apex. SObjects are used to hold record information in the Force.com database. Developers refer to sObjects and their fields by their API names. The standard objects have typically more standardized system predefined API names to use:

Lead myLead = new Lead();

myLead.FirstName = ‘Max’;

myLead.LastName = ‘James’;

System.debug(myLead);

The API name for custom objects is based on the object’s API name when it was created. For example, if you create a custom object named “Project,” its API name might be Project__C.

It’s important to note that API names for custom objects and fields are often suffixed with __c, while standard objects and fields typically do not have this suffix.

For standard fields, the API name usually combines the object’s API name with the field’s API name, and for custom fields separated by double underscores. For example:

Standard Field on Lead: API Name: Lead.LastName

Custom Field on Account: API Name: Account.CustomerPriority__c

Additionally, you need to ensure that the field is accessible and visible to the user or context in which your Apex code is executing.