Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Export Excel sheet from table using custom labels

Are Kaveri
Tera Contributor

Hi 

 

I am working on below script building script to export data in form of excel and send it .

 

but i used one method GlideExcel workbook . when i run in background script it throwing errors..

Any one has idea how to export excel without using below method.

var ExportHelper = Class.create();
ExportHelper.prototype = {
    initialize: function() {},

    exportTableToExcel: function() {
        this.exportTable('table_1', 'exportdata.xlsx', [{
                field: 'sys_id',
                label: 'ID'
            },
            {
                field: 'type',
                label: 'TYPE_C'
            },
            {
                field: 'group',
                label: 'GROUP__C'
            },
               ]);
       
        this.exportTable('x_customer_case', 'CaseExportData.xlsx', [{
                field: 'sys_id',
                label: 'ID'
            },
            {
                field: 'number',
                label: 'CASENUMBER'
            },
            {
                field: 'sys_created_on',
                label: 'CREATEDDATE'
            },
            {
                field: 'sys_updated_on',
                label: 'LASTMODIFIEDDATE'
            },
            {
                field: 'substate',
                label: 'STATUS'
            },
          
            {
                field: 'order_date',
                label: 'INCIDENT_DATE1__C'
            },
          
            {
                field: 'order_time',
                label: 'TIME_OF_DAY__C'
            },
          
         
            {
                field: 'description' + '' + 'work_notes',
                label: 'DESCRIPTION'
            },
            
            {
                field: 'assigned_to',
                label: 'Assigned_to_C'
            }
        ]);
    },
    exportTable: function(tableName, fileName, fields) {
        var workbook = new global.GlideExcelWorkbook();
        var sheet = workbook.createSheet('Export');
        // Add header row
        var headerRow = [];
        for (var i = 0; i < fields.length; i++) {
            headerRow.push(fields[i].label);
        }
        sheet.addRow(headerRow);
        // Query and add data
        var gr = new GlideRecord(tableName);
        gr.setLimit(100); // Adjust as needed
        gr.query();
        while (gr.next()) {
            var row = [];
            for (var j = 0; j < fields.length; j++) {
                row.push(gr.getValue(fields[j].field));
            }
            sheet.addRow(row);
        }
        // Write Excel file as attachment
        var stream = new global.GlideExcelStreamWriter(workbook);
        var attachment = new GlideSysAttachment();
        attachment.write(current, fileName, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', stream.toStream());
    },



    type: 'ExportHelper'
};

 

Anyone has any approach for below scenario.

To export data from a table with Custom header other than column names.

tables can be more than 3 tables data.

0 REPLIES 0