function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ajay_SFDCAjay_SFDC 

BLOB is not a valid UTF-8 string Error while reading Excel file

Hi there ,

I have written an Inbound Email service of salesforce . I am sending excel file to this salesforce service . Below is ocde in my method of InboundEmailHandler class :

     Messaging.Inboundemail.Binaryattachment[] binaryAttachement = email.binaryAttachments;
        
         if(binaryAttachement !=null && binaryAttachement.size() > 0)
         {
        for(Messaging.Inboundemail.Binaryattachment bAttach :binaryAttachement )
            {
                if(bAttach.filename.endsWith('.xls'))
                {
                        String bodyEncoded = EncodingUtil.base64Encode(bAttach.body);
                        system.debug('&&&&&&&&bodyEncoded'+bodyEncoded);
                        Blob decoded4Bytes = EncodingUtil.base64Decode(bodyEncoded );
                       system.debug('&&&&&&&&bodyEncoded'+decoded4Bytes.toString() );
                }
            }
         }

I am getting exception "BLOB is not a valid UTF-8 string"  while converting this binary data into string :(In system.debug)

Excel file always gives body in binary format . I want to parse/get contents of the excel file . Is there any way to get the contents of excel file ? Has anyone written parser for excel body ?

Thanks ,
 Ajay


EnreecoEnreeco
This happens because this line
system.debug('&&&&&&&&bodyEncoded'+decoded4Bytes.toString() );
gives out a non String value so it cannot be displayed in the debug log as a string
Ajay_SFDCAjay_SFDC
Hi ForceLogic ,

I dont think its the problem due to system.debug. Even if I remove it I am getting the same error .

Let me know if anyone written the excel parser .


Thanks ,
 Ajay
EnreecoEnreeco
Which is the line that gives you the exception?
Ajay_SFDCAjay_SFDC
Hi there ,

Below line :

Blob decoded4Bytes = EncodingUtil.base64Decode(bodyEncoded );

String strCSVData = decoded4Bytes.toString(); // This line causing error


Thanks
EnreecoEnreeco
This is the same problem as the System.debug. The Blob object you are trying to "stringify" with "toString()" is not a valid String (UTF-8 Strings are the strings you can use with String variables) but it is actually a binary string.

Ajay_SFDCAjay_SFDC
So what I need to do to get contents of this excel file in a string . As its in binary format and I need to get it in a string for further processing !!
EnreecoEnreeco
The problem is that the binary stuff isn't properly encoded and you cannot manipulate this string.
You can try using the HttpRequest class:
HttpRequest tmp = new HttpRequest();
tmp.setBodyAsBlob(blobValue);
String value = tmp.getBody(); //this seem to be a valid utf-8 string

This is kind a workaround to get a valid utf-8 string from a value (I haven't found any other way).

This comes from a blog post I wrote some months ago (http://enreeco.blogspot.it/2013/01/salesforce-apex-post-mutipartform-data.html) when I was playing with blob coming from an HTTP request (you are not making any callout in this 3 lines, but just using the HttpRequest encoding conversion features).
The problem is that it is difficult to then handle the binary part in your file.
Salesforce is not really binary friendly.

Hope this helps
Enrico
DixitDixit
Enreeco's answer (above this one) is the correct for this topic. 

also tried the  EncodingUtil.base64Encode(csvfile); and didn't work.but this one works great. 
Also the explanation in the blog is very clear.
Thanks.

 
Naveen KNNaveen KN
Check this link for the issue 
https://www.codengine.in/2019/06/blob-is-not-a-valid-utf-8-string-salesforce-apex.html