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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sven,



I think by default ServiceNow encodes characters in UTF-8 format.


Not sure whether any other character set can be user or not.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hello Ankur,



I encounter the same problem. Attachments that have characters like ä,ö,ü are not shown correctly.   Is there no possible in ServiceNow to solve this problem?  



Best regards,
Felipe


Hi Felipe,



Not sure on this part. If I get any details will post here.



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

The similar issue:



in datasource:   CSV (with BOM) attachment. Import: the first column always broken (BOM included to the first column name)...



How to import CSV-file with BOM attachment correctly?