Ad Code

blob is not a valid utf-8 string salesforce apex

BLOB is not a valid UTF-8 string

Have you encountered ‘BLOB is not a valid UTF-8 string’ issue while working on document parsing in the salesforce lightning? 

Well, it may be because of multiple reasons. In this post, we will discuss one of the root causes.


Apex decoding logic works well and good when the input Blob is in the correct format. In case if you try to upload xlsx document, it will throw an error in the apex because of special characters in the text format. You can notice by opening xlsx file in the notepad as a text which shows special characters.

Please don't ask me to read that file for you. 😃

The below screenshot shows Apex error in the Salesforce logs window.

BLOB is not a valid UTF-8 string salesforce apex 1


As a next step, change the format of the file with the below steps.


Open the xlsx file, click on File > Save as and enter the file name, select format as CSV (comma delimited) > click Save


BLOB is not a valid UTF-8 string salesforce apex 2


Try to import the CSV file and test upload functionality.

I hope it resolved your issue. Thank you for using our services. In case if anything else solved your issue, appreciate your time to post it in the comments section below.


You may also like




Post a Comment

4 Comments

  1. Shouldn't we select the format as "CSV UTF-8" ?
    Sometimes even in the csv files encoding other than UTF-8 can be present like ANSI.

    ReplyDelete
    Replies
    1. I think it works for CSV UTF-8 format as well.

      Delete
  2. Hi, am decoding base64 encoded string. The string format is ?utf-8?b?QW5vbWFseSBkZXRIY3Rpb24g4oCTIERFIFNIQVc?=
    Decoding is working fine in apex class but when am trying to insert record in test class am getting the error : blob is not valid utf-8 string.
    Could you pls help

    ReplyDelete
    Replies
    1. After decoding, are you mapping the decoded text to any field? And which object record you are inserting? Please share the code of test class method

      Delete