In this article, we will cover different types of variables we use in Apex and some examples of them.
- Primitives –
- Primitive datatypes are foundational datatypes that are used to hold data we are most familiar with.
- We define a variable with a primitive data type using the following syntax pattern:
- Datatype variableName = value;
- Camel Case is followed while writing a code in Apex.
- Blob –
- Blob is set of binary data stored in a single object.
- You can create Blob by either assigning it the value of attachment or document body, or through converting a string to Blob object.
- For Example: Blob myDataBlob = Blob.valueOf(“My text to convert”);
- Blob values are commonly used in process of encrypting and decrypting.
- Boolean –
- Boolean values are simple true or false values, used for logical processing or holding some state which is either on or off.
- Example:
- Boolean isChecked = false;
- Boolean values golds default value as null. Therefore it is best practice to assign it either true or false, else it will throw nullPointerException.
- Date –
- Date data type stores information on a particular day without holding any information on the time of day.
- We can define date using following syntax:
- Date christmas = Date.newInstance(2024,12,25);
- Date todayDate = Date.today ( );
- Date newYearEve = Date.parse(’31/12/2024′);
- DateTime –
- Datetime is primitive holding both a date and a particular time on that date.
- We can define Datetime using following syntax:
- Datetime thisMoment = Datetime.now();
- Datetime meetingStart = Datetime.newInstance(2024,9,1,9,30,0);
- Decimal –
- The decimal data-type is used for storing numerical data including decimals, but that have a fixed scale that can set explicitly or form data creating variable.
- Example:
- Decimal itemCost = 1.99;
- Decimal percentComplete = 15.2665345;
- Double –
- Similar to decimal, double again a number involving decimal point,but in this instance a 64-bit with a maximum value of 263 -1, and minimum value of -263.
- Example:
- Double goldenRatio = 1.618023548975;
- Id –
- This is special data-type available to Apex and Salesforce programmers, and is 18-character Id for particular record.
- Example:
- Id accountId = ‘00004QRSVp8BQE152o’;
- Integer –
- The integer day-type can hold ny whole number between 2,147,483,647 and -2,1147,483,648.
- Example:
- Integer count= 100;
- Long –
- A long value is very big integer, which is value without decimal point between 263-1 and -263.
- Example:
- Long revenue = 3147483647L;
- Object –
- All other data types, including primitives, inherit from object data type.
- Example:
- Object myData = 1618;
- Integer myIntegerData = (Integer)myData;
- String-
- The string data-type is for any collection of characters enclosed within single quotation marks.
- Example:
- String myName = ‘Paul’;
- String favoriteIceCream = ‘Mint Choco Chip’;
- String empty = ‘ ‘;
- String greeting = ‘Hello; + myName;
- More Examples of String:
- String tab = ‘My \t tab String’; (My tab String)
- String carriageReturn = ‘My\r carriage return string’; (My [/n] carriage return string)
- String lineFeed = ‘My \n line feed string’; (My [\n] line feed string)
- String singleQuote = ‘My \’single quote string\”; (My ‘single quote string’)
- String doubleQuote =’My \”double-quote string\”;’ (My “double-quote string”)
- String backSlash = ‘My \\backslash string’ (my \backslash string)
- Time –
- It represents time-stamp that is not dependent on day.
- Example:
- Time coffeeTime = Time.newInstance(10,35,23,400);
- Nulls –
- If variable is declared without a value then it is given a value of null.
- Null is special data-type meaning nothing or absence of data.
- If we ask apex to perform some action on value that does not exist (null value) then it will throw nullPointerException.
- Constants –
- To store value that will be going to stay fixed, we use contants.
- For Example:
- static final double PI = 3.14;
- Naming convention for constants is that we declare them all in uppercase.
- sObject, sObjects & sObject Instances –
- In order to work with records within Apex, Salesforce provides the sObject (Salesforce object) data-type.
- For Example:
- Account acc = new Account();
- acc.Name = ‘Acme’;
- Job__c job = new Job__c();
Thank you for reading the article till the end, in next article we will discuss DML in Salesforce.
Leave a comment