Export MRVS data to CSV file from portal

krishna761
Tera Contributor

Hi,

when click on order to quote widget, rows added in the MRVS should be exported to a CSV file.

 

krishna761_0-1729581177285.png

 

Please help me on this.

 

Thanks 

2 REPLIES 2

Pratiksha Kalam
Kilo Sage

Hello,

You can export from below table :

Navigate to "sc_multi_row_question_answer.list" . you will get export option for CSV file

Or Are you looking for any other option?
If my answer is helpful please mark it as correct or helpful.
Br,
Pratiksha.k

Moin Kazi
Kilo Sage
Kilo Sage

Hi @krishna761 ,

 

Step 1: Create the Scripted REST API

  1. Navigate to Scripted REST APIs:

    • Go to System Web Services > Scripted REST APIs in the ServiceNow application navigator.
  2. Create a New API:

    • Click on New to create a new Scripted REST API.
    • Fill in the following fields:
      • Name: MRVS Export API
      • API ID: mrvs_export
      • API Namespace: (leave blank or specify if needed)
    • Save the record.
  3. Create a Resource:

    • In the API record, go to the Resources tab and click New.
    • Fill in:
      • Name: Export
      • HTTP Method: GET
      • Path: /export
    • Click Submit.
    • After creating the resource, click on it to open the record.
    • In the Script field, enter the following code:

      Add the Script:

 

 

(function process(/* GlideHttpRequest */ request, /* GlideHttpResponse */ response) {
    var catalogItemId = request.queryParams.catalog_item_id; // Get the catalog item ID from request
    var csvData = "Column1,Column2,Column3\n"; // Set your CSV headers
    
    // Retrieve the MRVS values from the catalog item
    var gr = new GlideRecord('sc_item_option'); // Table for catalog item options
    gr.addQuery('catalog_item', catalogItemId);
    gr.addQuery('name', 'your_mrvs_variable_name'); // Replace with your MRVS variable name
    gr.query();

    while (gr.next()) {
        // Assuming the MRVS data is in JSON format
        var mrvsData = JSON.parse(gr.value); // Adjust as necessary to retrieve MRVS data

        for (var i = 0; i < mrvsData.length; i++) {
            var row = mrvsData[i];
            csvData += row.column1 + "," + row.column2 + "," + row.column3 + "\n"; // Adjust as necessary
        }
    }

    // Set response headers for file download
    response.setContentType('text/csv');
    response.addHeader('Content-Disposition', 'attachment; filename="MRVS_Export.csv"');
    response.write(csvData);
})(request, response);

 

 

Step 2: Modify the Order to Quote Widget

      1. Locate the Widget:

        • Navigate to Service Portal > Widgets and find the "Order to Quote" widget.
      2. Add the JavaScript Code:

        • In the widget's JavaScript section, you can add a function to call the REST API when the relevant action (like a button click) occurs.

        Here's an example of how you can do this: Server Script

         

         

 

function downloadMRVSCsv() {
    var catalogItemId = /* Logic to get the current catalog item ID */;

    var url = '/api/your_namespace/mrvs_export/export?catalog_item_id=' + catalogItemId; // Adjust the namespace
    
    // Create a temporary link to trigger the download
    var link = document.createElement('a');
    link.href = url;
    link.download = 'MRVS_Export.csv';
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

 

  • Bind the Function to a Button:

    • Add a button in the widget's HTML template that calls the downloadMRVSCsv function when clicked.

    Example HTML:

     

 

<button ng-click="c.downloadMRVSCsv()">Download CSV</button>

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you found my response **helpful**, I’d appreciate it if you could take a moment to select **"Accept as Solution"** and **"Helpful"** Your support not only benefits me but also enriches the community.

 

Thank you!
Moin Kazi

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~