Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 12:32 AM - edited 12-12-2023 12:34 AM
Hi @laszloballa , BR full script is as follows:
See var originalContentsInFile = Packages.java.lang.String(bytesInFile);
(function executeRule(current, previous /*null when async*/ ) {
var comment = current.variables.u_comment.toString(); // get the value of Comment from catalog item
var ritmNumber = current.number; // get the value of RITM number
// get the attachment record from the current RITM
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'sc_req_item');
attachmentGR.addQuery('table_sys_id', current.sys_id);
attachmentGR.addQuery('file_name', 'CONTAINS', '.csv');
attachmentGR.query();
if (attachmentGR.next()) {
var gsa = new GlideSysAttachment();
var tableName = attachmentGR.getValue('table_name');
var sysId = attachmentGR.getValue('table_sys_id');
var bytesInFile = gsa.getBytes(tableName, sysId);
var originalContentsInFile = Packages.java.lang.String(bytesInFile);
originalContentsInFile = String(originalContentsInFile);
var rows = originalContentsInFile.split('\n');
var columnA = [];
for (var i = 1; i < rows.length; i++) { // start from index 1 to skip the header row
var columns = rows[i].split(',');
columnA.push(columns[0]);
}
var concatenatedDeviceID = columnA.join(',');
// gs.print("Data in Column A concatenatedDeviceID : " + concatenatedDeviceID);
var ci = new GlideRecord('cmdb_ci_hardware');
ci.addQuery('serial_number', 'IN', concatenatedDeviceID.split(","));
ci.query();
// var count = ci.getRowCount();
// gs.info('Found ' + count + ' hardware records with serial numbers ' + concatenatedDeviceID);
while (ci.next()) {
var workNotes = ci.getValue('u_work_notes'); // get the existing work_notes value
var newWorkNotes = "RITM number: " + ritmNumber;
if (comment) {
newWorkNotes += "\nComment: " + comment; // add the comment to the work_notes value if provided
}
// gs.print("work_notes : " + newWorkNotes);
// set newNotes as the journal entry for u_work_notes
var field = ci.u_work_notes;
field.setJournalEntry(newWorkNotes);
ci.update(); // save the changes
}
// gs.print("Records Updated in cmdb_ci_hardware.");
} //else {
// gs.print("No matching records found in cmdb_ci_hardware.");
// }
})(current, previous);