Convert An Incident Record To CSV Via Script

Convert An Incident Record To CSV Via Script

ServiceNow Series

Introduction

ServiceNow is a cloud-based platform that provides comprehensive solutions for IT Service Management (ITSM) and Human Resources Service Delivery (HRSD). In ITSM, ServiceNow streamlines IT operations through features such as Incident Management, Change Management, Asset Management, and a Service Catalog. On the other hand, in HRSD, ServiceNow offers automation for HR processes, including an Employee Self-Service Portal for accessing HR services and efficient Case Management for handling HR-related inquiries and requests.

Prerequisites

  1. Basic idea about ServiceNow Scripting

  2. Basic Knowledge of ServiceNow Flow Designer

ServiceNow Journey Starts Here

Please Note: I am going to create this logic in Flow Designer Action, so it can be used in flow. One can copy the script to script include and use it accordingly.

  1. Navigate to ServiceNow Flow Designer.

  2. Click On New Button and Select Action Option to Create A New Action

  1. Add the Action Name and fill in the other fields accordingly.

  1. Create 2 Action Inputs
LabelNameTypeMandatory
Incident Recordincident_recordReference.IncidentTrue
File Namefile_nameStringTrue
  1. Click on Add a new step to add a Script Action Step.

  2. (Optional Step) Rename it to Generate Attachment.

  3. Set the input variables as the following

  1. Copy and Paste the following code:
(function execute(inputs, outputs) {

    /**
     * inputs {Object} input data received
     * outputs {Object} output data received
     * Get Inputs Data
     */
    var record_sys_id = inputs.incidentRecord;
    var fileName = inputs.fileName.toString() + '.csv';

    var record = new GlideRecord('incident');
    record.addEncodedQuery('sys_id=' + record_sys_id);
    record.query();

    var csv_headers = '', csv_values = '';

    if (record.next()) {

        /**
         * FETCHING HEADERS FOR CSV
         * HEADERS WILL BE THE FIELD LABELS
        */

        for (var field in record) {
            if (field != 'sys_meta') {
                var label = record[field].getLabel();
                // var label = field;  // use this to make label same as field backend name
                csv_headers += label + ',';
            }
        }

        /**
         * FETCHING VALUES FOR CSV
         * VALUES WILL BE THE FIELD VALUES
        */

        for (var value in record) {
            // var record_value = record[value].getDisplayValue(); // to fetch displaying value on the form
            var record_value = record[value];
            if (record_value != 'sys_meta' || record_value != null) {
                csv_values += record_value + ',';
            }
        }

    }

    // Concatinating headers and values
    var csv_data = csv_headers + '\n' + csv_values;


    // generating the attachment
    var generateFile = new GlideSysAttachment();
    generateFile.write(record, fileName, 'text/csv', csv_data);


})(inputs, outputs);
  1. Set the Output Variables As Following:
LabelNameTypeMandatory
RecordRecordReference.IncidentFalse
File Namefile_nameStringFalse
  1. Test run the action on any incident record and see the file will be attached to that incident record.

Further Insights

In this example, I have configured it to an action. You can modify the layout and move the logic to script include, schedule jobs or business rules as per your requirement.

Please find attached the update set of this development

Generate Attachment From Incident

Did you find this article valuable?

Support Sandeep Rana by becoming a sponsor. Any amount is appreciated!