Variables are the most basic of all programming concepts but are the foundation
upon which everything else is build. A Variable is a store for some data that can
change (or vary) over time.
Apex DataTypes
==============
1. Primitive Data Type - Integer, Double, Long, Date, Datetime, String, ID, or
Boolean)
2. Non Primitive Data Type - Collections (List, Set, Map)
3. SObject - Account, Contact, Opportunity, Lead, Task, User,..... (Any Salesforce
Obejct - Standard/Custom)
Primitive Data Type
===================
Primitive data types are the foundational data types that are used to hold data we
are most familiar with, define a primitive data type using the following syntax
pattern:
Syntax:
DataType VariableName = value;
Numerical Data Types Without Decimal Points (I.E., Whole Numbers)
1. Integer -
The Integer datatype can hold any whole number between 2,147,483,647 and -
2,147,483,648.
A 32-bit number that does not include any decimal point.
Integer barrelNumbers = 1000;
[Link](' value of barrelNumbers variable: '+barrelNumbers);
2. Long -
A Long values is a very big Integer, which is a value without a decimal point
between 2^63 - 1 and 2^63.
Eg: Long revenue = 3147483647L;
Numerical Data Types With Decimal Points (I.E., Fractional Numbers)
3. Decimal
The Decimal data type is used for storing numerical data including decimals but
that have a fixed scale that can either be set explicitly or from the data creating
the variable.
Eg: Decimal itemCost = 1.99;
Decimal percentageComplete = 15.266543;
4. Double
Similar to Decimal is Double, again a number involving a decimal point, but in this
instance a 64-bit number with a maximum value of 2^63 - 1 and a minimum value of
2^63.
Eg: Double goldenRatio = 1.61803398875;
5. Boolean -
Boolean values are simple true or false values used for logical processing or
holding some state which is either on or off.
This variable can either be true, false or null. Many times, this type of variable
can be used as flag in programming to identify if the particular condition is set
or not set.
Eg: 1 Boolean shipmentDispatched;
shipmentDispatched = true;
[Link]('Value of shipmentDispatched '+shipmentDispatched);
Output: Value of shipmentDispatched true
Eg: 2 Boolean shipmentDispatched;
[Link]('Value of shipmentDispatched '+shipmentDispatched);
Output: Value of shipmentDispatched null
6. Date
The Date data type stores information on a particular day without holding any
information on the time of day.
This variable type indicates a date. This can only store the date and not the time.
For saving the date along with time, we will need to store it in variable of
DateTime.
Eg: 1
Date christmas = [Link](2023, 12, 25);
Date todaysDate = [Link]();
Date newYearsEve = [Link]('31/12/2023')
newInstance which takes in the year, month and day parameters
today which returns today's date and parse which takes in date as some text
Eg : 2
//ShipmentDate can be stored when shipment is dispatched.
Date ShipmentDate = [Link]();
[Link]('ShipmentDate '+ShipmentDate);
Today() - Return the Date
7. DateTime
Datetime is a primitive holding both a date and a particular time on that date,
commonly used for either time-dependent information such as appointment time, or
for storing auditing and logging information such as the day and time on which an
action occured.
Date along with time
Now()- Return the Date and Time
Eg: 1
Datetime ShipmentDate = [Link]();
[Link]('ShipmentDate '+ShipmentDate);
Eg: 2
Datetime meetingStart = [Link](2023, 11, 6, 7, 30, 0);
Format for newInstance (YYYY, MM, DD, HRS, MINS, SECS)
8. Time
Time is very similar to Date and DateTime and represents a timeStamp that is not
dependent on a day.
Eg: 1 Time coffeeTime = [Link](10, 25, 23, 400);
Format for newInstance (HRS, MINS, SECS, MSECS)
Eg : 2
DateTime now = [Link]();
//To Fetch only the time
Time t =[Link]();
//To Fetch only the date
date d = [Link]();
[Link](t);
[Link](d);
9. String
The String datatype is for any collection of characters enclosed within single
quotation marks such as:
String myName = 'Adnan';
String favouriteIceCream = 'Vanilla';
String empty = ' ';
String greeting = 'Hello' +myName;
In the final variable, we declare a variable called greeting. You can see we are
able to concatenate sgtrings together using the + symbol so that greeting has the
value Hello Adnan
Other Examples
1. String tab = 'My\ttab string';
Output: My tab string
2. String carriageReturn = 'My\rcarriage return string';
Output:
My
carriage return string
3. String linefeed = 'My\nline feed string';
Output:
My
line feed string
4. String singleQuote = 'My\'single quote string\'';
Output:
My 'single quote string'
5. String doubleQuote = 'My\"double-quote string\"';
Output:
My "double-quote string"
6. String backslash = 'My\\blackslash string';
Output:
My\blackslash string
You can use the following escape sequences for special characters in string values.
SEQUENCE MEANING
\b One backspace character
\n New line
\r Carriage return
\t Tab
\Z CTRL+Z (ASCII 26)
\” One double-quote character
\\ One backslash character
\0 One ASCII null character
10. Id
This is a special data type available to Apex and Salesforce programmers, and is
the 18 character Id for a particular record. Salesforce verifies the Id at runtime
when a variable of type Id is assigned a value and will error if the value is
invalid. It will also convert a 15 character Id into an 18 character Id as part of
this Process;
ID accountId = '0012w00001R7LpQAAV';
11. Blob
A blob is a set of binary data stored in a single object. Blob values are commonly
used in processes where you are either encrypting to a Blob or decrypting from a
Blob to add some additional security to a set of data.
// Create a Blob from a string
String text = 'Hello, this is a Blob example';
Blob blobData = [Link](text);
// Display the Blob data as a string
String decodedText = [Link]();
[Link]('Decoded Text: ' + decodedText);
// Create a Blob from a base64 encoded string
String base64Data = 'SGVsbG8sIHRoaXMgaXMgYSBCbG9iIGV4YW1wbGU=';
Blob base64Blob = EncodingUtil.base64Decode(base64Data);
// Convert the Blob back to a base64 encoded string
String encodedBase64Data = EncodingUtil.base64Encode(base64Blob);
[Link]('Base64 Data: ' + encodedBase64Data);
12. Object
(It can store any data)
Eg:
Object data = 'Adnan';
[Link](data);
Object age = 29;
[Link](age);
Object dt = [Link]();
[Link](dt);
Object name = 'Patil Ji';
Object age = 29;