2

Hope you can help. Something which appears so small is bugging the heck out of me!!

All I want to do in SuiteScript 2.0 is directly download a CSV file to the client as soon as I hit the SUBMIT button on a Suitelet. My code downloads to the client, but the content in the file is incorrect.

The following code creates the file (successfully):

    //creating CSV file
    var fileObj = FILEMODULE.create({
        name: 'ThreePLreport.csv',
        fileType: FILEMODULE.Type.CSV,
        contents: contents
    });  

The last snippet of code writes the file to the client side:

   context.response.writeFile(fileObj,true);

Now - when I come to open the file, it looks like this:

CSV file output from suitelet

When I save the same file on the filing cabinet, it looks correct:

enter image description here

I have tried adding HEADERS to the code, like:

context.response.addHeader({
        name: 'Content-Type:',
        value: 'text/csv' //text/csv
        });
context.response.addHeader({
        name: 'Content-Disposition',
        value: 'attachment; filename="report1.csv"'
        });

This doesn't seem to help in correcting the content inside the CSV file. Still displays the HTML code as you saw above.

Any ideas?

Thanks in advance!

Arif WB
  • 186
  • 1
  • 13

3 Answers3

6

I've done almost exactly this many times. It looks like you are not returning after you write the file. e.g.:

context.response.writeFile({
    file:fileObj,
    isInline:false
});
return; 
bknights
  • 14,408
  • 2
  • 18
  • 31
  • This was the reason. @bknights - please share more about why I have to return. I never needed to do this in Suitescript 1.0 – Arif WB May 25 '20 at 23:27
  • 1
    No idea why NS let you get away with that in the past. I suspect that in SS1 they ignored anything written to the stream after you wrote the file. Now it looks like the stream is reset. Either case should have resulted in an error not a silent unexpected result. – bknights May 26 '20 at 00:26
1

I had the same problem, I thought it was the header but no. I realized that I was submitting the wrong content. The content must be an array separating each column by commas. The end of the line is '\ n'.

ex. ['title1','title2','title3','\n','contentline1','contentline1','contentline1','\n','contentline2','contentline2','contentline2','\n']

1.- Build the array 2.- Create the csv file

var downFile = file.create({
            name: somename + '_' + qty + '.csv',
            fileType: file.Type.CSV,
            contents: csvBody,
 });

3.- Download the file

context.response.writeFile({ file: downFile });

Mirna
  • 11
  • 1
  • 1
    Hey Mirna - I suspect that the contents are the array after it has had toString() applied to it. As for your original content, could you modify your post and add that so that we can learn from it? – Gerard ONeill Aug 13 '22 at 04:49
  • As far as I'm aware the contents needs to be of type string, not array. You also wouldn't want the newlines as a data input, so would just add them to the end of the current data and remove superfluous commas. ie: "title1,title2,title3\ncontentline1,contentline1,contentline1\ncontentline2,contentline2,contentline2\n" – CCC Sep 16 '22 at 06:19
0

Try removing the inline option (display in browser) so change:

context.response.writeFile(fileObj,true);

to

context.response.writeFile(fileObj);
Adolfo Garza
  • 2,966
  • 12
  • 15
  • Thanks for responding! Nop - this doesn't do the job unfortunately. Still spitting out a file with HTML code. Any other ideas? – Arif WB May 25 '20 at 12:06