How can I include the JSZip library?

AlejandraEs
Tera Contributor

How can I include the JSZip library in servicenow to configure the portal, I need to download a client level table in a zip file

1 ACCEPTED SOLUTION

Ratnakar7
Mega Sage

Hi @AlejandraEs ,

 

You can follow these steps:

  1. Obtain the JSZip library: Download the JSZip library from the official website (https://stuk.github.io/jszip/) or from a reliable source. You'll typically find the library as a JavaScript file (e.g., jszip.min.js).

  2. Upload the JSZip library to ServiceNow: In your ServiceNow instance, navigate to System Definition > Script Includes. Create a new Script Include record and provide a unique name (e.g., "JSZip"). In the script field, copy and paste the content of the JSZip library file you downloaded. Save the record to upload the library to ServiceNow.

  3. Create a client script: Navigate to System UI > Scripts - Client Scripts and create a new client script. Provide a unique name and specify the table(s) where you want to use the script. In the script field, write the JavaScript code to download the table data as a zip file using JSZip.

    Here's an example code snippet that demonstrates the basic structure:

 

function downloadTableDataAsZip() {
  // Initialize JSZip
  var zip = new JSZip();
  
  // Add files to the zip
  // ... (code to fetch and add table data files to the zip)
  
  // Generate the zip file
  zip.generateAsync({ type: "blob" }).then(function(content) {
    // Create a download link for the zip file
    var link = document.createElement("a");
    link.href = URL.createObjectURL(content);
    link.download = "table_data.zip";
    link.click();
  });
}

 

        4. Note: In the above code, you need to replace // ... (code to fetch and add table data files to the zip) with the actual code to fetch the table data and add it to the zip file using JSZip.

       5. Save the client script and test: Save the client script record, and then you can include the script in the desired UI page or UI action to trigger the downloading of the table data as a zip file. You can use UI actions, UI macros, or UI pages to incorporate the script based on your requirements.

Make sure to test the functionality thoroughly to ensure that the table data is correctly fetched, added to the zip file, and downloaded as expected.

Remember to handle any necessary security and access controls when accessing and downloading table data.

 

 

Thanks,

Ratnakar

View solution in original post

4 REPLIES 4

Ratnakar7
Mega Sage

Hi @AlejandraEs ,

 

You can follow these steps:

  1. Obtain the JSZip library: Download the JSZip library from the official website (https://stuk.github.io/jszip/) or from a reliable source. You'll typically find the library as a JavaScript file (e.g., jszip.min.js).

  2. Upload the JSZip library to ServiceNow: In your ServiceNow instance, navigate to System Definition > Script Includes. Create a new Script Include record and provide a unique name (e.g., "JSZip"). In the script field, copy and paste the content of the JSZip library file you downloaded. Save the record to upload the library to ServiceNow.

  3. Create a client script: Navigate to System UI > Scripts - Client Scripts and create a new client script. Provide a unique name and specify the table(s) where you want to use the script. In the script field, write the JavaScript code to download the table data as a zip file using JSZip.

    Here's an example code snippet that demonstrates the basic structure:

 

function downloadTableDataAsZip() {
  // Initialize JSZip
  var zip = new JSZip();
  
  // Add files to the zip
  // ... (code to fetch and add table data files to the zip)
  
  // Generate the zip file
  zip.generateAsync({ type: "blob" }).then(function(content) {
    // Create a download link for the zip file
    var link = document.createElement("a");
    link.href = URL.createObjectURL(content);
    link.download = "table_data.zip";
    link.click();
  });
}

 

        4. Note: In the above code, you need to replace // ... (code to fetch and add table data files to the zip) with the actual code to fetch the table data and add it to the zip file using JSZip.

       5. Save the client script and test: Save the client script record, and then you can include the script in the desired UI page or UI action to trigger the downloading of the table data as a zip file. You can use UI actions, UI macros, or UI pages to incorporate the script based on your requirements.

Make sure to test the functionality thoroughly to ensure that the table data is correctly fetched, added to the zip file, and downloaded as expected.

Remember to handle any necessary security and access controls when accessing and downloading table data.

 

 

Thanks,

Ratnakar

Thank you so much! this was useful to me!

ruslan
Tera Contributor

Hi @Ratnakar7 ,

I am trying to create script include with the code from downloaded file (jszip.min.js) and it shows error

Screenshot 2023-08-19 at 15.33.06.png

Am I right, I don't need to create any function in new script include, just copy-paste code?

Thanks!

Is the issue fixed?