In Salesforce development, understanding primitive data types is essential for handling information effectively. These fundamental building blocks form the basis of storing, processing, and manipulating data within your Apex code. This blog post provides a quick reference guide to the most common primitive data types in Salesforce.
By reading this blog, you will learn:
- The key data types used in Salesforce.
- How to distinguish between different data types for optimal usage.
- Basic use cases for each data type.
- Where to find further documentation for advanced applications.
Let’s get started!
Different Apex Primitive Data types
Apex variables, including class member variables and number variables, are initiated to null when declared. The developer should assign appropriate values according to their type during declaration. Here is the list of all primitive data types available in Salesforce.
Blob
Blob is a collection of binary data (0 and 1) stored as a single object. It is accepted as web service arguments, stored in a document (body of the document is stored as blob), or sent as an attachment.
Methods: toString() valueOf() to convert blob into string.
Example:
Blob csvBlob = blob.valueOf(generatedCSVFile);
String csvName = ‘company details which doesn’t have members.csv’;
csvAttachment.setFileName(csvName);
csvAttachment.setBody(csvBlob);
email.setFileAttachments(new Messaging.EmailFileAttachment[|{csvAttachment});
Link Crypto Class
Boolean
The value that can be assigned to the variable of type Boolean is true, false, or null.
Example:
Boolean flag=true;
If(flag) (
System.Debug(‘Value of flag is ‘+flag)
}
Link: Boolean Class
Date
Date indicates the value of a particular day. No time details are stored in the Date datatype. It is used with system static functions.
Methods: adddays(noOfdays), day), format (), etc.
Example:
Date myDate = Date.newInstance(1960, 2, 17);
Date newDate = mydate.addDays(2);
Integer day = myDate.day);
Link: Date Class
DateTime
DateTime indicates the value of a particular date and time. Create datetime values using the static system method.
Methods: addDays(noOfdays),addHours(noOfHours) , addMinutes(noOfMinutes),
addMonths(noOfMonths), etc.
Example:
Datetime myDateTime = Datetime.newInstance(1960, 2, 17);
Datetime newDateTime = myDateTime.addDays(2);
DateTime newDateTimeHrs = myDateTime.addHours(3);
Link: DateTime Class
Decimal
Numerical values contain decimal points. Currency fields are by default assigned decimal values.setScale method is used to define the number of decimal places. If the setscale method is not explicitly used, the number of decimal places is determined by the item from which the decimal is created.
Methods: abs), int Value),longValue), etc
Example:
Decimal myDecimal = -6.02214129;
System.assertEquals(6.02214129, myDecimal.abs);
Decimal decimalNumber = 19;
Decimal result = decimalNumber.divide(100, 3);
Link: Decimal Methods
Double
Doubles are 64-bit value numbers. It includes decimal points. It has a minimum value of -263 and a maximum value of 263-1.
Methods: round(),format() , intValue(), LongValue(), etc.
Example:
Double myDouble = 1261992;
system.assertEquals(‘1,261,992’,myDouble.format());Double DD1 = double.valueOf(‘3.14159’);
Integer value = DD1.int Value);
stem.assertEquals(value, 3);
Link: Double Class
ID
Any valid 18 character lightning and force.com record identifier. If an ID is set to 15 characters, Salesforce automatically converts it into 18 characters for representation. A runtime exception is thrown for the invalid characters.
Methods: to15(), addError(errormsg), valueOf(IdValue), etc.
Example:
ID id=’00300000003T2PGMwP’
public class MyException extends Exception()
Trigger.new[Oj.ld.addError(new myException(Invalid Id’));
Link: ID Class
Integer
A 32-bit numerical value. It does not include decimal values. Minimum value in integer is -2,147,483,648 and maximum is 2,147,483,648.
Methods: valueOf(stringTolnteger), format(),etc.
Example:
Integer j=100;
String strNum=’345′;
Integer num= Integer.valueOf(strNum);
Link: Integer Class
Long
A 64-bit numerical value that does not include decimal value. the minimum value of -263 and a maximum value of 263-1. It is used for a wider range of values that cannot be accommodated in integers.
Methods: format(), int Value(), valueOfLong(stringToLong)
Example:
Long | = 2147485558L;
Long myLong = 7191991;
Integer value = myLong.intValue;
system.assertEquals(7191991, myLong.intValue());
Long L1 = long.valueOf(1);
Link: Long Class
Object
All the data types in Apex are inherited from Object. We can cast object data type to specific data type like integer, string, sObject, etc.
Example:
//cast Object to integer
Object obj=567;
Integer num=(Integer)num;
//cast Object to Custom class
Object objClass=new CustomClass();
istomClass cc=(CustomClass)objClass;
String
Any set of characters enclosed within single quotes.
Size: There is no limit on the number of characters in a string. Heap size limit is used to make sure that the apex program does not grow too large.
SObject string fields cannot contain null values and no trailing and leading whitespaces, while Apex string can contain empty or null and leading and trailing white spaces.
String uses escape sequences like \b (backspace), \t (tab), In (line feed), \f (form feed), \r (carriage
return), \” (double quote), \’ (single quote), and || (backslash).
Comparison operators can be used =, !=, <, <=, >, and >=.
Methods: contains), equals(StringOrld), indexOf(subString), etc
Example:
String message=’Have a good day.
String myString1 = ‘abcdabcd’;
String myString2 = ‘ab’;
Integer result = myString1.indexOf(mystring2, 1);
System.assertEquals(4, result);
link: String Class
Time
A value that indicates a particular time. Always create time values with a system static method.
Methods: minute(), second(), addHour), etc.
Example:
Time myTime = Time.newInstance(1, 2, 3, 4);
Time expected = Time.newInstance(4, 2, 3, 4);
System.assertEquals(expected, myTime.addHours(3));
Link: Time Class
Conclusion
Now that you’re armed with a better understanding of Salesforce primitive data types, it’s time to put your knowledge into practice! Experiment, build solutions, and see how these data types empower your development projects.
Ready to take your Salesforce skills to the next level? Unlock your full potential with saasguru. Start your free trial today and access 18+ Salesforce Certification Courses, 50+ Mock Exams, and 50+ Salesforce Labs for hands-on mastery.
Let’s make your Salesforce aspirations a reality!