How to change Encoding for GlideSysAttachment.write()

Sven Bauer
Tera Contributor

Hi,

I'm writing a function in a script include that exports me some data and adds it as an Attachment to a record.

Example Code is like this

var data = "Name;Firstname;Phone";

data += "\n";

data += "Bauer;Sven;+492408xxx";

data += "\n";

data += "Meier;Karl-Heinz;+492408xxx";

data += "\n";

data += "Müller;Joachim;+492408xxx";

var company = new GlideRecord('core_company');

company.get('ee4fab735f6512006d8ddb765a069c99');

var attachment = new GlideSysAttachment();

var newFile = attachment.write(company, 'Telefonliste.csv', 'text/csv', data);

My Problem now is that the Attachment is encoded in UTF-8 but without BOM.

This makes the Umlaut 'ü' to not be shown correctly in Excel.

find_real_file.png

Any Ideas how to change the Enocoding to ANSI or add the bom-Marker?

      Sven

1 ACCEPTED SOLUTION

Oliver D_sereck
Giga Expert

Hi Sven,



I just had the same issue. Unfortunately UTF-8 hates all special characters, even if this encoding should happily display them.


I have tried encoded/decoding into UTF-8 characters, without success.



But I have some good news, and this may be a little hacky but it works for me:


Add "\uFEFF" right at the start of the content.


This is the character encoding for BOM.



So in your case:




var data = "\uFEFFName;Firstname;Phone";  


data += "\n";  


data += "Bauer;Sven;+492408xxx";  


data += "\n";  


data += "Meier;Karl-Heinz;+492408xxx";  


data += "\n";  


data += "Müller;Joachim;+492408xxx";  


 


var company = new GlideRecord('core_company');  


company.get('ee4fab735f6512006d8ddb765a069c99');  


 


var attachment = new GlideSysAttachment();  


var newFile = attachment.write(company, 'Telefonliste.csv', 'text/csv', data);  


View solution in original post

13 REPLIES 13

for export: export to excel instead of CSV helps...


Oliver D_sereck
Giga Expert

Hi Sven,



I just had the same issue. Unfortunately UTF-8 hates all special characters, even if this encoding should happily display them.


I have tried encoded/decoding into UTF-8 characters, without success.



But I have some good news, and this may be a little hacky but it works for me:


Add "\uFEFF" right at the start of the content.


This is the character encoding for BOM.



So in your case:




var data = "\uFEFFName;Firstname;Phone";  


data += "\n";  


data += "Bauer;Sven;+492408xxx";  


data += "\n";  


data += "Meier;Karl-Heinz;+492408xxx";  


data += "\n";  


data += "Müller;Joachim;+492408xxx";  


 


var company = new GlideRecord('core_company');  


company.get('ee4fab735f6512006d8ddb765a069c99');  


 


var attachment = new GlideSysAttachment();  


var newFile = attachment.write(company, 'Telefonliste.csv', 'text/csv', data);  


Hi Oliver,

this works fine.

Thanks a lot

 

    Sven

HI Oliver,

The solution can multiple sheets with test/xls?

Hi Hung,

no, I only write plain Text (with Olivers help now with BOM) into a csv-Attachment.

If you want to write an Excel File, "data" must contain the Base64 Encoded Content of your Excel-File. If you know how to create this, you might post it here.

 

    Sven