The CreatorCon Call for Content is officially open! Get started here.

How to save response Rest Message in CSV file and copy it in a record.

jacc_99
Giga Guru

Hi,

I trigger a Rest Message like that:

var request = new sn_ws.RESTMessageV2('NameRestMessage', 'NameRestFunction');
var response = request.execute();

gs.info('Check response: \n' + response.getBody());

In log I can see this response:

Header1|Header2|Header3|Header4|Header5|Header6|Header7
data01|data02|data03|data04|data05|data06|data07
data11|data12|data13|data14|data15|data16|data17
data21|data22|data23|data24|data25|data26|data27

How can I save this response in a .csv file and copy it in a record of servicenow table?

Somebody can help me with this problem?

 

Thanks in advance

1 ACCEPTED SOLUTION

jacc_99
Giga Guru

Hi,

One solution is:

var nameFile = 'name_file.csv';
var req = new sn_ws.RESTMessageV2('rest_message_name', 'method_name');
req.saveResponseBodyAsAttachment('sys_data_source', datasource_sys_id, nameFile);
var resp = req.execute();
resp.waitForResponse(1000);

View solution in original post

3 REPLIES 3

Tony Chatfield1
Kilo Patron

Hi, without details of your payload or exactly how the data ended up in the format you have identified, there is not a lot of information to base a response on.

I assume that your log entry is not a copy of a string, but a concatenation of values from your payload using a | separator?
If it is a string then you need to replace '|' with ',' which gives you a csv format, if it is not a string then you need to concatenate your data to create a csv type form (probably using the same basic script used to create your log entry). Once you have csv data you can create a file and add save to sys_attachment.

These post should be a good starting point.
https://community.servicenow.com/community?id=community_article&sys_id=e5ad70d61b1d4490d01143f6fe4bcb21

https://community.servicenow.com/community?id=community_article&sys_id=02468652dbc904506064eeb5ca961962

Thanks for your answer, Tony!

jacc_99
Giga Guru

Hi,

One solution is:

var nameFile = 'name_file.csv';
var req = new sn_ws.RESTMessageV2('rest_message_name', 'method_name');
req.saveResponseBodyAsAttachment('sys_data_source', datasource_sys_id, nameFile);
var resp = req.execute();
resp.waitForResponse(1000);