Experience Salesforce

ID & Blob Datatypes in APEX

What You’ll Learn

  • What is ID in Apex?
  • What is Blob 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.NoFunctionExample
1addError(errorMsg)Marks a trigger record with a custom error message and prevents any DML operation from occurring.// Example in a Triggertrigger AccountTrigger on Account (before update) {    for (Account acc : Trigger.new) {        if (acc.Name == ‘Invalid’) {            acc.Id.addError(‘Account name cannot be “Invalid”.’);        }    }}
2addError(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);        }    }}
3addError(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);        }    }}
4addError(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);        }    }}
5getSObjectType()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
6to15()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
7valueOf(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
8valueOf(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.NoFunctionExample
1size()Returns the number of characters in the Blob.// Creating a Blob from a stringBlob blobData = Blob.valueOf(‘Hello, World!’);
// Getting the size of the BlobInteger blobSize = blobData.size();
// Outputting the sizeSystem.debug(‘Blob size: ‘ + blobSize);  // Output: Blob size: 13
2toPdf(stringToConvert)Creates a binary object out of the given string, encoding it as a PDF file.// Converting a string to a PDF BlobString myString = ‘This is a PDF content’;Blob pdfBlob = Blob.toPdf(myString);
// Outputting the Blob as a PDFSystem.debug(‘PDF Blob: ‘ + pdfBlob);
3toString()Casts the Blob into a String.// Creating a Blob from a stringBlob blobData = Blob.valueOf(‘Hello, World!’);
// Converting the Blob back to a stringString stringData = blobData.toString();
// Outputting the stringSystem.debug(‘Blob as String: ‘ + stringData);  // Output: Blob as String: Hello, World!
4valueOf(stringToBlob)Casts the specified String to a Blob.// Creating a Blob from a stringString myString = ‘Hello, Salesforce!’;Blob blobData = Blob.valueOf(myString);
// Outputting the BlobSystem.debug(‘Blob: ‘ + blobData);
Next Topic

Need Extra Support? Our FREE study materials have got you covered.

Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.

cts-img
Rakshabandhan Sale