Need assistance with below best practice error

Community Alums
Not applicable

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

Steps to resolve issue: It is recommended to run the Package Call Removal Tool, and replace all Java package calls with the Glide alternative.
Required to run the Package Call Removal Tool, and replace all Java package calls with the Glide alternative.
 
Business Rule script
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);

 

 
Please assist

 

 

1 ACCEPTED SOLUTION

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

View solution in original post

9 REPLIES 9

Hi @Community Alums 

you can use the following conversion:

 

var originalContentsInFile = gs.base64Decode(GlideStringUtil.base64Encode(bytesInFile));

 

 Maik

Community Alums
Not applicable

Hi @Maik Skoddow , 

Thanks for your reply, Let me check

Community Alums
Not applicable

Hi @Maik Skoddow , I checked but it is not working, Unable to fetch records from attachment (.xlsx)

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

Community Alums
Not applicable

Hi @Maik Skoddow , Sorry for the confusion, but GlideExcelParser API working fine for me, Thanks!