Hot answers tagged

17 votes
Accepted

How to combine multiple Blob in apex to get single Blob value

It is possible to do it by converting Blob to Hex using EncodingUtil.convertToHex() method, concat the Hex Sequence and then convert concatenated Hex string back to Blob using EncodingUtil....
Amit Jain's user avatar
  • 817
13 votes
Accepted

Why is String.valueOf(Blob) different that Blob.toString()?

String.valueOf returns Blob[X] for Blob values. I believe it does it this way because System.debug internally uses String.valueOf, and debugging a potentially non-Unicode stream into a Unicode String ...
sfdcfox's user avatar
  • 481k
12 votes
Accepted

MD5 Blob Cannot Convert to String

Instead of toString, you need to actually use EncodingUtil.convertToHex to get the output you expect: String myString = 'Some String'; system.debug(myString); Blob myBlob = Blob.valueOf(myString); ...
sfdcfox's user avatar
  • 481k
10 votes
Accepted

How to generate a >5 MB blob for an Apex test class

You can use the string repeat method to build a long string, and then pass that to a Blob. Blob data = Blob.valueOf('a'.repeat(5242880));
Adrian Larson's user avatar
  • 149k
7 votes
Accepted

Returning a blob to a lightning component split error

You must use base-64 encoding if you want to do it this way. The reason why is that internally, Lightning uses JSON, so passing binary data through either way may have unexpected results. If necessary,...
sfdcfox's user avatar
  • 481k
6 votes
Accepted

Body not a variable of ContentVersion?

For ContentVersion, the property that contains the blob data is 'VersionData'. Line 6 should be changed to: controller.file.VersionData = Blob.valueOf('populated'); See more here: https://...
Ryan Rudd's user avatar
  • 139
5 votes
Accepted

Save base64 data as Attachment

Did you set the contentType as application/pdf? I tried this following code and it is working for me. String jsonStr = '<Base64 String>'; Attachment attach = new Attachment(); attach....
Saroj Bera's user avatar
  • 14.6k
5 votes

Send Blob file via Apex Rest Callout

You can't use both setBody and setBodyAsBlob in the same request. Also, if you're using multipart/form-data, you have to build a multipart payload. That means that you'll need to do something like the ...
sfdcfox's user avatar
  • 481k
5 votes
Accepted

What Field Type should be used to assign Blob primitive data type?

Alas, Salesforce doesn't support custom fields of the Blob type. Internally SF use Blob fields though (e.g. attachments, content files). Anyways, you have several options here. (1) Save your Blob as ...
Eduard's user avatar
  • 5,067
5 votes

Unsupported mime Type when creating Blob instance into lightning community

We had the same problem and we opened a case: we've been told that the LockerService whitelisted the following MIME Types: application/octet-stream application/json video/ audio/ image/ font/ text/...
AlfaCold's user avatar
  • 111
5 votes
Accepted

How to check Blob objects for equality?

The equals operator == does work as expected with Blob, so there's no reason to jump through extra hoops, such as your eq method. However, one possibility is that you may have had a different case in ...
sfdcfox's user avatar
  • 481k
5 votes
Accepted

How to display downloaded PDF blob in an iframe?

You have to alter your getPDF() method and prepend data:application/pdf;base64, public String getPDF() { return 'data:application/pdf;base64,'+EncodingUtil.base64Encode([SELECT VersionData ...
Pranay Jaiswal's user avatar
5 votes
Accepted

Merging Docx files into one - Apex

Aside from plain text files (e.g. source code or README.TXT type files), most files follow an internal structure. This means that two similar files (e.g. two GIF or DOCX) being "added together&...
sfdcfox's user avatar
  • 481k
4 votes
Accepted

visualforce to create a attachment for an object

Declare an Id variable. In the constructor, retrieve the Id of the record as follows: controller.getId(); With this Id, try to insert the attachment record. Change VFP code like this: <apex:...
Santanu Boral's user avatar
4 votes

Send Blob file via Apex Rest Callout

Proper multipart/form-data POST requests from Apex are tricky. As @sfdcfox mentioned, you need to do some very careful manipulations between the 8-bit bytes of the input file and the 6 bit groups used ...
Daniel Ballinger's user avatar
4 votes

Why does a variable of blob type, when printed gives out it's size?

Use EncodingUtil.base64Encode for the contains for the Blob. Refer EncodingUtil Class String str = 'myString'; Blob b = Blob.valueOf(str); System.debug(b); //it returns Blob[8] String base64String = ...
Santanu Boral's user avatar
4 votes
Accepted

Convert Int Array to Blob

Apex doesn't have the notion of "byte arrays", unfortunately, so there's no one-liner that can do this. The best you can do if you start from this array would be something like: Integer[] ...
sfdcfox's user avatar
  • 481k
3 votes
Accepted

How to view attachment content?

You can get the string value of a Blob using its toString method: system.debug(a.Body.toString());
Adrian Larson's user avatar
  • 149k
3 votes
Accepted

Trying to show the content field (blob) from contentNote in Visualforce

There are two ways to convert a Blob to a string in Apex. Depending on how the Blob was created, you'll use one or the other: blobVar.toString(); encodingUtil.base64encode(blobVar); For content, ...
Matt Kaufman's user avatar
3 votes
Accepted

Send Blob file via Apex Rest Callout

Thanks for all of your valuable feedback. Below example worked for me with some modifications. How can I compose a multipart/form-data request? Here is my code. File Upload Method public void ...
MalindaL's user avatar
3 votes
Accepted

How do I convert a Blob into an alphanumeric string and back?

Use EncodingUtil.base64encode to convert to a String, and base64decode to get back to a Blob.
sfdcfox's user avatar
  • 481k
3 votes

Select body Of attachments

I know this is four months old, but I just ran into this myself, and may have a reason as to why this occurs. There is a very small blurb at the bottom of this page on the developers site : You can ...
X3074861X's user avatar
  • 131
3 votes

Merging pdf attachments

PDF is a binary format that, like most binary formats, simply can't be squashed together directly. It contains magic headers, object graphs, images, compression, and a bunch of other stuff that you ...
sfdcfox's user avatar
  • 481k
3 votes

Alternative to renderas PDF for converting an HTML to PDF in apex

There are a number of AppExchange packages that can generate a PDF using a template- Conga, Drawloop, and Thunderhead are three well known ones. Generally these templates are built in Word or another ...
Alex McDonald's user avatar
3 votes
Accepted

List out of bound exception at List<Blob>

Instead counter use b.size() for(integer i = 0; i <b.size(); i++) { Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); efa.setFileName('attachment.pdf'); efa.setBody(...
Ratan Paul's user avatar
  • 22.5k
3 votes
Accepted

Multipart form data post file with multiple form parameters?

I'd avoid manually handling the Base64 encoding yourself and the application of Grant Wickman's solution to avoid padding characters on the binary data before the multipart boundary. I looked at this ...
Daniel Ballinger's user avatar
3 votes

MD5 Blob Cannot Convert to String

This is a useful cryptography helper class I often use: public class CryptoUtils { public static String SHA1Hash(String value) { return hash('SHA1', value); } public static ...
Robs's user avatar
  • 9,177
3 votes
Accepted

Get visualforce page pdf blob in apex

You can use any of the PageReference methods. The most straightforward would be: Blob pdfData = Page.myPageName.getContent(); Note that this technically still counts as a callout (meaning, you ...
sfdcfox's user avatar
  • 481k
3 votes
Accepted

Formatting Excel Column from Apex

This is an locale + Excel issue. You cannot apply formatting in a CSV file as such. You're outputting dates in DD/MM/YY format. Excel is reading them in MM/DD/YY. If you notice which dates are and ...
David Reed's user avatar
  • 92.1k
3 votes

Why Blob equals doesn't work if one of the Blobs is retrieved from ContentDocument?

As guessed in the comments, .equals() behaves as the === (exact equality operator), which generally checks to see if two objects are the exact same object in memory. Instead, you'd want to use == ...
sfdcfox's user avatar
  • 481k

Only top scored, non community-wiki answers of a minimum length are eligible