1

I have emails attached in notes and attachments object.

When i try to read the body of Email i get Blob values suggest me ways to convert the blob into text.

2

You can use toString() of Blob class.

Example:

String myString = 'StringToBlob';
Blob myBlob = Blob.valueof(myString);
System.assertEquals('StringToBlob', myBlob.toString());

Other option is by using String.valueOf() of String class but not suggested as this returns useless string like Blob[X] where X is the number of bytes in the blob.

There is one answer of sfdcfox which you might want to check before doing anything with blob. It says:

Blob.toString may result in an exception if the contents are not a Unicode String.

So use toString() with Try-Catch block. Check comments too for more details.

| improve this answer | |
  • 1
    String.valueOf also won't get the content of the blob, but instead give a useless string like Blob[X], where X is the number of bytes in the blob. For this case, I'd advise a try-catch block if the user isn't sure it's a valid string. – sfdcfox Jun 21 '18 at 6:17
  • @sfdcfox ohh I didn't know that, so i suppose toString with Try-catch is better in this case, right? – Mr.Frodo Jun 21 '18 at 6:19
  • Yes, I'd suggest taking that route in this case. The main purpose of String.valueOf is when you're trying to debug/log something. – sfdcfox Jun 21 '18 at 6:22
  • @sfdcfox When i tried using try catch the field i am trying to populate is not getting updated – Ajay Jun 22 '18 at 6:37
  • @Ajay can you provide your code? Also you can try debugging your code and provide the exception message which you are facing. Then probably we can help you better. – Mr.Frodo Jun 22 '18 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.