Salesforce Apex Data Types with Examples

By | December 27, 2022

What type of data is going to store in variable it’s called data types. The apex language is strongly typed so every variable in apex will be declared with the specific data type.

What is Apex Data Types?

Data types will specify the following

i) How much memory for a given variable.
ii) What type of data is stored in a variable.
iii) What is the range of the data that is stored in a variable?
iv) Primitive will create by the compiler. Non-primitive will create by the user.

Apex data types are:-

  • Primitive data types
  • Collections (List/Set/Map)
  • sObject (Generic object account, Contact, Customer_c)
  • Enums

Primitive data types in salesforce

Apex supports the following primitive data types: Boolean, Integer, Long, Double, Date, Datetime, String, ID, and Decimal.

Boolean

Boolean values represent true or false and contain the literals TRUE and FALSE.

Example:
Boolean b;
b = true;
System.debug(‘Value of shipmentDispatched: ‘+b);

Integer

Integer data types are whole numbers that range from -2,147,483,648 to 2,147,483,647.

Example:
Integer i = 1000;
system.debug(‘ Integer value is: ‘+i);

Long

Long data types are whole numbers that range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Example
Long productRevenue = 21474838973344648L;
system.debug(‘companyRevenue:’+productRevenue);

Double

Double data types are numbers that contain decimal points and range from -1.79769313486231E+308 to -4.94065645841247E-324 for negative values and 4.94065645841247E-324 to 1.79769313486232E+308 for positive values.

Example
Double d = 3.14159;
System.debug(‘D value is:’ + d )

Date

Date data types contain only date information, without time information.

Example
Date deliveryDate = date.today();
System.debug(‘ShipmentDate: ‘+deliveryDate);

DateTime

Datetime data types contain both date and time information.

Example
Datetime currDateTime = Datetime.now();
System.debug(‘ShipmentDate: ‘+currDateTime);

String

String data types are text values and can contain any character.

Example
String str = ‘Rahul’;
System.debug(‘My name is:’ + str);

ID

ID data types are used to store Salesforce record ID values and are 18 characters long.

Example
Id myclass = [select Id from Account Limit 1];
System.debug(‘ID:’ +myclass);

Decimal

Decimal data types are numbers that contain decimal points and range from -10^-130 to 10^125.

Example
Double d = 356.32;
System.debug(‘Double is:’ +d);

Sobject

Apex sObjects are a special data type in Salesforce that are used to hold data in memory and are closely related to database tables. They are used to represent an object such as an Account, Contact, or Opportunity.

An sObject can either be a standard object or a custom object and each record in the sObject is represented as a single row in the database. Apex sObjects allow developers to access and manipulate data in Salesforce in a secure and efficient way.

Example
Account accountObj = new Account();
accountObj.Name = ‘Rohit’;
System.debug(‘Account name is:’ +accountObj);
insert accountObj;

Enums

  • An Enums is an abstract data type whose values consist of a finite set of identifiers.
  • Hiding the implementation prevents any possible misuse of the values of performing arithmetic and so on.
  • To define an Enum, use the ‘enum’ keyword in our declaration and use curly braces to demarcate the list of possible values.

Example
public enum Color {Red, Pink, Blue, yellow}
Color objC = Color.pink;
System.debug(‘objC value: ‘+objC);