0

I am developing a custom application in "ServiceNow" which requires Javascript and HTML coding. So, I have a field say, "description" on my form. How may I save this field's value to a word document on the desktop?

Kavya Dev
  • 3
  • 4

3 Answers3

1

While JavaScript cannot create a file for you to download by itself, ServiceNow does have a way for you to create one. Creating a Word document is impossible without the use of a MID server and some custom Java code, but if any file type will do you can create an Excel file using an export URL. To test this out, I made a UI Action in a developer instance running Helsinki on the Problem table. I made a list view that contains only the field that I wanted to save, and then used the following code in the UI action:

    function startDownload() {
    window.open("https://dev13274.service-now.com/problem_list.do?EXCEL&sysparm_query=sys_id%3D" + 
        g_form.getUniqueValue() + "&sysparm_first_row=1&sysparm_view=download");
}

When the UI action is used, it opens a new tab that will close almost immediately and prompt the user to save or open an Excel file that contains the contents of that single field.

If you want to know more about the different ways you can export data from ServiceNow, check their wiki-page on the subject.

  • Thank you so much for the help. I have another question. Do you know how to configure servicenow to integrate with microsoft outlook. I need to do this because our users want to be able to create meeting invitation on their own also they want to be able to look at all participants availability and rooms availability. I have permission to access exchange server but struck up on configuring snow. @caputLupinum – Kavya Dev Jun 02 '16 at 20:15
1

You can use the HTML5 FileSystem API to achieve that

window.requestFileSystem(window.PERSISTENT, 1024*1024, function (fs) {
  fs.root.getFile('file.txt', {create: true}, function(fileEntry) {
    fileEntry.createWriter(function(fileWriter) {
      var blob = new Blob([description.value], {type: 'text/plain'});
      fileWriter.write(blob);
    });
  });
});

FYI, chrome supports webkitRequestFileSystem.

Alternatively, use a Blob and generate download link

var text = document.getElementById("description").value;
var blob = new Blob([text], {type:'text/plain'});
var fileName = "test.txt";

var downloadLink = document.createElement("a");
downloadLink.download = fileName;
downloadLink.href = window.webkitURL.createObjectURL(textFile);
downloadLink.click();
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
0

Javascript protects clients against malicious servers who would want to read files on their computer. For that reason, you cannot read or write a file to the client's computer with javascript UNLESS you use some kind of file upload control that implicitely asks for the user's permission.