…thoughts on ServiceNow and digital transformation

Post

ServiceNow Script for Producing a CSV File of Attachments with Incident Number


var content = 'RecordName,FileName,AttachmentSysId,TableName,RecordSysId,HardwareCategory';

var attGR = new GlideRecord('sys_attachment');
attGR.addQuery('table_name', 'IN', 'alm_hardware,incident');

attGR.query();
while (attGR.next()) {

var tableName = attGR.getValue('table_name');

var parentGR = new GlideRecord(tableName);
parentGR.get(attGR.getValue('table_sys_id'));

var recordName = '';
var hardwareCategory = '';
if (tableName == 'alm_hardware') {

hardwareCategory = parentGR.getDisplayValue('model_category');
recordName = 'AST-' + hardwareCategory + '-' + parentGR.getValue('serial_number');
} else {
recordName = parentGR.getDisplayValue();
}


content += '\n';
content += recordName;
content += ',' + attGR.file_name.getDisplayValue();
content += ',' + attGR.getUniqueValue();
content += ',' + attGR.getValue('table_name');
content += ',' + attGR.getValue('table_sys_id');
content += ',' + hardwareCategory;
}


var attachment = new GlideSysAttachment();

//attach the file to a problem record
var rec = new GlideRecord('problem');
rec.get('<<put the sys_id of a problem record here>>');
var fileName = 'bia_attachments.csv';
var contentType = 'text/csv';


var agr = attachment.write(rec, fileName, contentType, content);