Anyone know a way to change the response type in a...
# suitescript
c
Anyone know a way to change the response type in a CSV? Trying to export a csv from a suitelet. This worked in 1.0, but I can't find a way in 2.0... context.response.setContentType('CSV',filename);
b
create a csv file and write that file to the response
c
I tried that (just the header row), and it displays on the screen, does not export a file... 😕
b
set isInline
c
k, let me try that.
no luck there. I can create the CSV formatted data in my suitelet. But, I can't get it to export from a button click on my main suitelet (button press triggers a function in the associated client script). Essentially, I'm just trying to export the current data that is visible in a suitelet to a CSV, when they click the export button...
b
what does your code look like
c
very crazy right now... lol... One sec, I'll post the stuff I have right now.
Untitled
b
you probably dont want to use https.post if you want to download the file
its not going to give you a download prompt
c
you thinking just http.post? or something else?
b
most primitive way is to use window.open
c
yeah, I had that... Let me try again, with this setup
b
there are more advanced techniques like using iframes or anchor elements
or opening a blob
c
yeah... Wish they would ahve just left it where you could spit out a file simply... lol
Definitely still missing something... Getting the URL works. And trying the window.open to the suitelet works (and shows the text ont he page). But, it's still just a page, not a CSV download... 😕
Screw it, I'm coming back to this tomorrow... lol...
b
Copy code
/**
 *@NApiVersion 2.0
 *@NScriptType Suitelet
 */
define(["N/ui/serverWidget", "N/file"], function (serverWidget, file) {
  return {
    onRequest: function (context) {
      if (context.request.parameters.download === "T") {
        context.response.writeFile({
          file: file.create({
            name: "Testing.csv",
            fileType: file.Type.CSV,
            contents: "blahblahj\nblahblah",
            encoding: file.Encoding.UTF8,
          }),
        });
      } else {
        var battkWroteThis = serverWidget.createForm({
          title: "Battk chose my title",
        });
        battkWroteThis.addButton({
          id: "battk_button",
          label: "Battk Says click me",
          functionName: 'window.open(window.location.href + "&download=T")',
        });
        context.response.writePage({ pageObject: battkWroteThis });
      }
    },
  };
});
there are much better ways to write that, but it should give an idea of what is needed
c
hmmm, that looks like I may have beeing trying to overthink it... I'll check out your solution.
Yep... Got it... I was way overthinking it... Thanks!