Blob : primitive data type in Salesforce

Feedback

Primitive data types in salesforce are the basic data type which can’t be broken further into lower data types. In salesforce we have blob,Integer, Double, Long, Date, Datetime,Time,Decimal,String, ID,Boolean and object, we will discuss all these primitive data types in details with example and scenario where they can be use in salesforce, so Let start discussing these data types in details,

Blob

A blob is data type which collection of binary data, now if i will you I world definitely want to know what is binary data, so you may aware that information in computer is stored in the form of 0 or 1, so to construct a message in particular format whether it’s image or PDF file, it will be store in the form of series of binary data and that form or collection is known as blob.

Blob is part of System namespace which provide methods and classes for apex core functionality, so we have separate blob class, which include some functions that help us to deal with multimedia in salesforce, i.e PDF . Let see all the methods that blob class provide us,

valueOf(stringToBlob)

Blob is mainly use in apex to deal with creating PDF files or strings, it doesn’t used to deal with other multimedia objects like images and music, we have a function which valueOf which convert the convert/cast the string into blob object, as we are casting the string into blob the return type of it will be of blob type, see example below.

String testString='test string';
Blob myBlob=blob.valueOf(testString);
toString()

This method will convert the blob into back to string, it doesn’t take any parameter you just need to call this function through you blob variable, as it convert back blob to string the return type of the data will be of string type, see the below code snippet.

String myString = 'test string';
Blob myBlob = Blob.valueof(myString);
String newString=myBlob.toString();
System.debug(newString);            // This will print the text 'test string'<span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span>
size()

This method will count the number of characters in blob object, as number of characters must be in integer so the return type of this will be integer type, see below code snippet.

String testString='test string';
Blob myBlob=blob.valueOf(testString);
Integer size=myBlob.size();
system.debug(size);
toPdf(String)

The last method the blob class does have is to convert the text into a PDF, the return type will be of a blob type but encoded in a PDF type, as discussed earlier blob is collection of binary data, which represent the PDF, images, music e.t.c, so


String pdfContent = 'This is a test string';
Blob myBlob=blob.toPdf(pdfContent);
System.debug(myBlob);

Related Articles :

Speak Your Mind

*