[SalesForce] Save text in Document object of Salesforce in ISO-8859-1/ANSI/ASCII format

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!

Best Answer

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('');
System.debug(urlEncoded);
for(Integer i = 0; i < chars.size(); i++) {
    if(chars[i] == '%') {
        chars[i] = EncodingUtil.convertToHex(EncodingUtil.convertFromHex(chars[i+1]+chars[i+2]));
        chars.remove(i+2);
        chars.remove(i+1);
    } 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.

Related Topic