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
Basic idea about ServiceNow Scripting
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.
Navigate to ServiceNow Flow Designer.
-
Click On New Button and Select Action Option to Create A New Action
-
Add the Action Name and fill in the other fields accordingly.
- Create 2 Action Inputs
Label | Name | Type | Mandatory |
Incident Record | incident_record | Reference.Incident | True |
File Name | file_name | String | True |
Click on Add a new step to add a Script Action Step.
-
(Optional Step)
Rename it toGenerate Attachment.
Set the input variables as the following
- 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);
- Set the Output Variables As Following:
Label | Name | Type | Mandatory |
Record | Record | Reference.Incident | False |
File Name | file_name | String | False |
- 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.
Link and Resources
Please find attached the update set of this development