3

ID & Blob Datatypes in APEX

The ID data type in Apex is a unique, immutable identifier used to represent records in Salesforce uniquely. Whereas, the Blob data type in Apex is used for storing binary data.

It is a collection of binary data stored as a single entity in the database. IDs are essential for tasks such as querying records, linking related data, and performing updates or deletions.

Apex provides several methods for manipulating Blob data, including converting strings to Blob objects and vice versa, as well as generating binary content like PDFs. Mastering the ID and Blob data type is essential for developers dealing with file storage, data encryption, and other advanced data handling scenarios in Salesforce.

What is ID in Apex?

Any valid 18-character Lightning Platform record identifier is called an ID in Apex. If you set an ID to a 15-character value, Apex converts the value to its 18-character representation. All invalid ID values are rejected with a runtime exception. An ID is required for associating data with specific records, such as accounts, contacts, or custom objects. Each ID is unique, ensuring precise identification and secure handling of Salesforce records within the platform.

Id Methods

The following are methods for ID.

S.No Function Example
1 addError(errorMsg): Marks a trigger record with a custom error message and prevents any DML operation from occurring. // Example in a Trigger
trigger AccountTrigger on Account (before update) {
   for (Account acc : Trigger.new) {
      if (acc.Name == 'Invalid') {
         acc.Id.addError('Account name cannot be "Invalid".');
      }
   }
}
2 addError(errorMsg, escape): Marks a trigger record with a custom error message, specifies if the error message should be escaped, and prevents any DML operation from occurring. trigger AccountTrigger on Account (before update) {
   for (Account acc : Trigger.new) {
      if (acc.Name == 'Invalid') {
         acc.Id.addError('Account name cannot be "Invalid".', true);
      }
   }
}
3 addError(exceptionError): Marks a trigger record with a custom error message and prevents any DML operation from occurring. trigger AccountTrigger on Account (before update) {
   for (Account acc : Trigger.new) {
      try {
         if (acc.Name == 'Invalid') {
            throw new CustomException('Account name cannot be "Invalid".');
         }
      } catch (CustomException e) {
         acc.Id.addError(e);
      }
   }
}
4 addError(exceptionError, escape): Marks a trigger record with a custom error message and prevents any DML operation from occurring. trigger AccountTrigger on Account (before update) {
   for (Account acc : Trigger.new) {
      try {
         if (acc.Name == 'Invalid') {
            throw new CustomException('Account name cannot be "Invalid".');
         }
      } catch (CustomException e) {
         acc.Id.addError(e, true);
      }
   }
}
5 getSObjectType(): Returns the token for the sObject corresponding to this ID. This method is primarily used with describe information. Id accountId = '0015g00000B5MVJAA3';
System.debug(accountId.getSObjectType()); // Output: Account
6 to15(): Converts an 18-character Id value to a 15-character case-sensitive string. Id caseInsensitiveId = '0015g00000B5MVJAA3';
String caseSensitiveId = caseInsensitiveId.to15();
System.debug(caseSensitiveId); // Output: 0015g00000B5MVJ
7 valueOf(toID): Converts the specified String into an ID and returns the ID. String idString = '0015g00000B5MVJAA3';
Id recordId = Id.valueOf(idString);
System.debug(recordId); // Output: 0015g00000B5MVJAA3
8 valueOf(str, restoreCasing): Converts the specified string into an ID and returns the ID. If restoreCasing is true, and the string represents an 18-character ID that has incorrect casing, the method returns an 18-character ID that is correctly aligned with its encoded casing. String idString = '0015g00000B5MVJAA3';
Id originalId = Id.valueOf(idString, true);
System.debug(originalId); // Output: 0015g00000B5MVJAA3

What is Blob in Apex?

Blob Data Type in Apex is a collection of binary data stored as a single object. You can convert this data type to String or from String using the toString and valueOf methods, respectively. A blob is typically used to store images, audio, or other multimedia objects, and sometimes binary executable code is also stored as a blob. Salesforce supports Blob manipulation only with Apex class methods that are supplied by Salesforce.

Blob Methods

The following are methods for Blob.

S.No Function Example
1 size() Returns the number of characters in the Blob. // Creating a Blob from a string
Blob blobData = Blob.valueOf('Hello, World!');
// Getting the size of the Blob
Integer blobSize = blobData.size();
// Outputting the size
System.debug('Blob size: ' + blobSize); // Output: Blob size: 13
2 toPdf(stringToConvert) Creates a binary object out of the given string, encoding it as a PDF file. // Converting a string to a PDF Blob
String myString = 'This is a PDF content';
Blob pdfBlob = Blob.toPdf(myString);
// Outputting the Blob as a PDF
System.debug('PDF Blob: ' + pdfBlob);
3 toString() Casts the Blob into a String. // Creating a Blob from a string
Blob blobData = Blob.valueOf('Hello, World!');
// Converting the Blob back to a string
String stringData = blobData.toString();
// Outputting the string
System.debug('Blob as String: ' + stringData);
// Output: Blob as String: Hello, World!
4 valueOf(stringToBlob) Casts the specified String to a Blob. // Creating a Blob from a string
String myString = 'Hello, Salesforce!';
Blob blobData = Blob.valueOf(myString);
// Outputting the Blob
System.debug('Blob: ' + blobData);
Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

Book Free15-Minutes Career Counselling