Text in Document object of salesforce are being saved in UTF-8 now, but I need to save it in ISO-8859-1/ANSI format.

When I do the following, and try to download the document and look at it's encoding in Notepad++, it is in UTF-8:

Document doc = new Document();
String str = 'Kå Ægir';
Blob bl = Blob.valueOf(str);
doc.FolderId = 'dummyFolderID';
doc.Name = 'dummyName';
doc.Body = bl;
doc.ContentType = 'text/plain; charset=ISO-8859-1';
doc.Type = 'txt';
insert doc;

But I need it in ISO-8859-1/ANSI. I tried to give 'text/plain; charset=ISO-8859-1' in 'ContentType' field but did not work.

For example, we have option to set 'Encoding' for Email Templates and Reports to 'General US & Western Europe (ISO-8859-1, ISO-LATIN-1)'

Is there a way to do the same with Documents? Or any other similar object?

All I need is, a text file generation in Salesforce(preferably with Apex code) with text like 'Kå Ægir' to be in ANSI/ISO-8859-1/ASCII and NOT in UTF-8 when I download.

Thanks in advance!

1 Answer 1


The String type in Salesforce is always a UTF-8 String. The most practical way to make sure it's an actual ISO-8859-1 string is to use a base-64 encoded string and send it directly into the blob via EncodingUtil.base64Decode:

String str = 'SyBnaXI=';
Blob bl = EncodingUtil.base64Decode(str);
doc.Body = bl;
insert doc;

Aside from that, the next most practical way to do this would be to use EncodingUtil.urlEncode, do some post-processing on it, then you'd be ready to go:

String source = 'Kå Ægir';
String urlEncoded = EncodingUtil.urlEncode(source, 'iso-8859-1');
String[] chars = urlEncoded.replaceAll('\\+',' ').split('');
for(Integer i = 0; i < chars.size(); i++) {
    if(chars[i] == '%') {
        chars[i] = EncodingUtil.convertToHex(EncodingUtil.convertFromHex(chars[i+1]+chars[i+2]));
    } else {
        chars[i] = EncodingUtil.convertToHex(Blob.valueOf(chars[i]));
Blob body = EncodingUtil.convertFromHex(String.join(chars,''));

As you can tell, this isn't particularly elegant or efficient, but it should be enough to get the job done.

  • Thank you very much!! Second piece of code worked perfectly for me. Saved the day!
    – Divya
    Jun 22, 2018 at 14:05
  • 1
    @Divya I'm glad it helped. It actually took me a few minutes to figure this one out, since, unlike Java, it's not easy to work in anything other than UTF-8.
    – sfdcfox
    Jun 22, 2018 at 14:21
  • Right @sfdcfox ! I found straight forward methods in java but not in apex. I am having another small problem here, please see if you could help me with this too. This code that you mentioned worked like charm for characters like 'å Æ', but when I download a file with English-characters-only, it is still in UTF-8. Is there a way/ code that could work for both? Thanks!!
    – Divya
    Jun 22, 2018 at 20:16
  • 1
    @Divya The lower 128 symbols (0-127) are identical in ANSI/UTF-8, and therefore impossible to tell them apart. They should be byte-wise identical, but whatever software you're using might prefer to be in UTF-8 mode.
    – sfdcfox
    Jun 22, 2018 at 20:19
  • Mother of god thanks for this answer. Apr 13, 2021 at 20:15

This site is temporarily in read-only mode and not accepting new answers.

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