- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 11:48 PM - edited 12-12-2023 12:37 AM
Hi All,
I am getting below best practice error. I used "Packages.java.lang.String(bytesInFile)" in BR script.
Best Practice Warning: brni_BPE10482 Checks for direct calls to Java packages
Required to run the Package Call Removal Tool, and replace all Java package calls with the Glide alternative.
(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);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 01:55 AM
Hi @Community Alums
in your first code you wanted to load a CSV file which is text based and that was the reason for my solution. Now you want to load an Excel file which is a native and binary format. Here my approach will not work. You really should be clear in your issue description and requirement!
So please have a take a look at the GlideExcelParser API which provides a way to extract data from Excel files.
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 12:50 AM - edited 12-12-2023 12:55 AM
Hi @Community Alums
you can use the following conversion:
var originalContentsInFile = gs.base64Decode(GlideStringUtil.base64Encode(bytesInFile));
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 12:52 AM
Hi @Maik Skoddow ,
Thanks for your reply, Let me check
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 01:16 AM - edited 12-12-2023 01:17 AM
Hi @Maik Skoddow , I checked but it is not working, Unable to fetch records from attachment (.xlsx)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 01:55 AM
Hi @Community Alums
in your first code you wanted to load a CSV file which is text based and that was the reason for my solution. Now you want to load an Excel file which is a native and binary format. Here my approach will not work. You really should be clear in your issue description and requirement!
So please have a take a look at the GlideExcelParser API which provides a way to extract data from Excel files.
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 02:01 AM
Hi @Maik Skoddow , Sorry for the confusion, but e GlideExcelParser API working fine for me, Thanks!