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
lingannalinganna 

System.StringException: BLOB is not a valid UTF-8 string

Hi all

 

when I uplaod csv file Iam getting the above error .

anybody help me

 

System.StringException: BLOB is not a valid UTF-8 string 

sfdcfoxsfdcfox

You can't convert "BINARY" data into a String. A String only supports valid UTF-8 strings. Use EncodingUtil.Base64Encode if you need the binary data in a string you can work with.

bp-devbp-dev

I'm getting that error when I try to uplaod a UTF-8 file as an attachment. The strange thing is that when I try it as a document, it works fine.  So I wanted to parse a txt file (UTF-8) on a trigger and since you can do triggers on attachments but not on documents, I thought I'd upload it as an attachment and parse it on insert. No such luck. Is there a difference between the body of an attachment and that of a document?

sfdcfoxsfdcfox

Interesting. It's a limitation of the design of attachment triggers. In a trigger, "attachment.body" is truncated or something... it's not null, but it fails on the Blob.toString() function. So, you have to actually query the db to get the contents. I got something like that working using this basic trigger:

 

trigger parser on Attachment (after insert) {
    for(attachment a:[select id,body from attachment where id in :trigger.new]) {
        parse.fileContents(a.body.toString());
    }
}

Depending on your needs, you may want to check the content type first and/or ignore bulk updates.

IvanWuIvanWu
please try the code blow,hope it can solve your problem 
 
HttpRequest tmp = new HttpRequest();
    tmp.setBodyAsBlob(csvFileBody);
    csvAsString = tmp.getBody();


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