saveResponseBodyAsAttachment works but stops next script code.

jacc_99
Giga Guru

Hi,

I get a response file through RestMessage call. Next I copy this response in CSV file as attachment in a data source record already created. Then I need check the response in log and execute the data source and transform map with script.

var req = new sn_ws.RESTMessageV2('rest_name', 'method_name');
req.saveResponseBodyAsAttachment('target_table', 'sys_id_record', 'file_name.csv');
var response = req.execute();
response.waitForResponse(500);
gs.info('&&&Test response \n' + response.getBody());

// Trigger data source and transform map with script

The problem is if I copy the csv file through saveResponseBodyAsAttachment the next part of my code doesn't execute.

If I comment the line req.saveResponseBodyAsAttachment('target_table', 'sys_id_record', 'file_name.csv') and attach the .csv file to the data source manually, when I execute the code, I can see in log the response, response.getBody(), and the part of code that I named in the code above like "Trigger data source and transform map with script" works perfectly.

Why the method saveResponseBodyAsAttachment stop everthing after it in my code?

There is some way to solve this or another solution with script code? 

 

Thanks in advance

1 ACCEPTED SOLUTION

jacc_99
Giga Guru

I found the problem.

In gs.info('&&&Test response \n' + response.getBody()); getBody() is not supported.

That was the error!

View solution in original post

5 REPLIES 5

Community Alums
Not applicable

I think it should be below, check this article:

https://developer.servicenow.com/blog.do?p=/post/saving-restmessagev2-responses-as-attachments/

Thanks for your answer Joro!

 

I checked this article before I need attach the response on another record.

I understand that code. In my case, saveResponseBodyAsAttachment works perfectly, with this method I achieve save the response with a csv file in the data source record. But the code below saveResponseBodyAsAttachment doesn't execute and I don't understand why.

Community Alums
Not applicable

May be try this way?

var req = new sn_ws.RESTMessageV2('rest_name', 'method_name');
//req.saveResponseBodyAsAttachment('target_table', 'sys_id_record', 'file_name.csv');
var response = req.execute();
response.waitForResponse(500);

req.saveResponseBodyAsAttachment('target_table', 'sys_id_record', 'file_name.csv');
gs.info('&&&Test response \n' + response.getBody());

Thanks a lot, Joro!