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.

1 Answer 1

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.

13
  • 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
    Commented Jun 21, 2018 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
    Commented Jun 21, 2018 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
    Commented Jun 21, 2018 at 6:22
  • @sfdcfox When i tried using try catch the field i am trying to populate is not getting updated
    – Ajay
    Commented Jun 22, 2018 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
    Commented Jun 22, 2018 at 6:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .