Server Side Script - To export data in to excel.

kailasb
Tera Contributor

Hi,

 

I am looking for the solution to export Servicenow table columns values into excel using server side script.

 

Thanks, KB

 

5 REPLIES 5

YogB
Tera Expert

Hi please go through this discussion. This also has the code to export the table as excel. 

https://www.servicenow.com/community/developer-forum/export-to-csv-or-xml-using-script/m-p/1870097/p...

Mark as helpful if this helps in your solution

kailasb
Tera Contributor

@YogB 

I am looking for the server side script to populate table date into excel where I can read the cell values of excel and do formatting like color and font. Refer the sample file

kailasb_0-1726121555212.png

 

Eshwar Reddy
Kilo Sage

Hi @kailasb 

 

var output = "Number,Company,Short_Description"; //header for csv file

 

var table = "incident";

 

var recordId = ""; //using for attachment file




var gr = new GlideRecord(table);

 

gr.addEncodedQuery("assigned_to=46d44a23a9fe19810012d100cca80666"); //Incident was assigned to Beth Anglin

 

gr.query();

 

var count = 0;

 

while (gr.next()) {

 

      count++;

 

      output += "\n" + gr.number + "," + gr.company.getDisplayValue() + "," + gr.short_description;

 

      if (!recordId) {

 

              recordId = gr.sys_id;

 

      }

 

}




gs.print(recordId);

 

writeAttachmentFile(output);




function writeAttachmentFile(data) {

 

      var attachment = new Attachment();

 

      var attachmentRec = attachment.write(table, recordId, "export.csv", "text/csv", data);

 

}
Please mark this response as Correct and Helpful if it helps you can mark more that one reply as accepted solution

 

Mani A
Tera Guru
//  Export the 'incident' table with specific columns
exportTableToCSV('incident', 'active=true', ['number', 'short_description', 'priority']);

// Function to generate CSV from a incident table
 
function exportTableToCSV(tableName, query, columns) {
    // Initialize the CSV content with the column headers
    var csvContent = columns.join(",") + "\n";
 
    var gr = new GlideRecord(tableName);
    gr.addEncodedQuery(query);
    gr.query();
    while (gr.next()) {
        var row = [];
        for (var i = 0; i < columns.length; i++) {
            var columnName = columns[i];
            row.push(gr.getValue(columnName));
        }
        csvContent += row.join(",") + "\n";
    }

 
    var filename = tableName + "_" + new GlideDateTime().getNumericValue() + ".csv";

    // Create a GlideSysAttachment record to store the CSV file
    var attachment = new GlideSysAttachment();
    var record = new GlideRecord('sys_attachment');
    record.initialize();
    record.table_name = 'sys_attachment';
    record.table_sys_id = 'your_target_sys_id'; // Provide appropriate sys_id or target record
    record.file_name = filename;
    record.content_type = 'text/csv';
    record.insert();
   
    // Write the CSV content to the attachment
    attachment.write(record.sys_id, filename, 'text/csv', new GlideText(csvContent).toString());
}