Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

This works like charm! Thanks for the great solution, helped a lot!

Sarah Powell
Mega Expert

Hello all,

Does anyone mind sharing what version of ServiceNow they are running? I am trying to accomplish something similar but I am receiving the following error:

     Can't find method com.glide.ui.SysAttachment.write

I thought maybe I had typed something incorrectly so I copied the code above, changed the record sys_id, and it still gives the same error:

Javascript compiler exception: Can't find method com.glide.ui.SysAttachment.write(string,string,string,org.mozilla.javascript.ConsString). (null.null.script; line 12) in:
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 = "a8b7f3880ff013000da306bce1050e01";
   
var attachment = new GlideSysAttachment();   
var newFile = attachment.write(company, 'Telefonliste.csv', 'text/csv', data);  

I am running Jakarta Patch 6. Has anyone seen this error? Any ideas why it's failing?

Thanks,

Sarah

Hi Sarah,

I'm running Jakarta Patch 6 as well and get the same Error while running in Scope.

SysAttachment.write is only available in global scope. You need to run your script in global Scope.

Maybe a little tricky to run global Scripts out of scoped activities...

Regards

    Sven

Sven,

Thanks for the quick reply! I read the doc article and saw where it mentioned Scoped a few times. I was actually trying to write the contents to a requested item, which is global. I couldn't make sense of why it wasn't working globally if the API was defined by ServiceNow themselves.

To start with, I changed as little as I could in the example code to try an eliminate my own lack of knowledge. Looks like I still caused my own issue... I change the sys_id value to match what was provided and then entered the sys_id in the get statement and that WORKED! Here's what I used and the attachment was created and added to the requested item.

 

var emailStr = '';

emailStr += 'To: \n';
emailStr += 'Subject: \n';
emailStr += 'From: \n';
emailStr += 'abcdefghijklmnopqrstuvwxyz';

gs.print('\n\n**emailStr**\n' + emailStr + '\n**emailStr**\n');

var ritm = new GlideRecord('sc_req_item');
ritm.get('a8b7f3880ff013000da306bce1050e01'); //RITM0010069

var attachment = new GlideSysAttachment();
var newFile = attachment.write(ritm, 'email.txt', 'text/plain', emailStr);

 

find_real_file.png

find_real_file.png

 

I looked at this all day yesterday and was stuck! Thanks for the input 🙂

Sarah