How to Convert CSV file any format to UTF-8 in Business Rule?

Srinivas Goud K
Tera Contributor

Hi All, 

I have a requirement like if the user attached csv file (any format) for catalog item in service portal I need to convert into UTF-8 format. Here, basically after attaching CSV file I'm fetching the content and splitting and storing some content in a string variable (hash_attachment) using Business Rule here

without convert into UTF-8 the content is storing in string variable like this : �T�0�F�m�B�A�E�A�H�A�A�A�A�A�o�A�S�Q�d�h�S�g�A�A�C�g�C�d�B�2�F�K�m�q�L�Q�L�e�Q�C�C�Q�U..

 

Business Rule(Async) :

(function executeRule(current, previous /*null when async*/ ) {
    // Add your code here
    var hash = "";
    // var source = "dbarker";
    var attach = new GlideRecord("sys_attachment");
    // attach.addQuery("table_name", "sc_req_item");
    attach.addQuery("table_sys_id", current.sys_id); // current.sys_id gives sys_id of RITM
    attach.query();
    if (attach.next()) {
        // gs.log("Has attachment", source);
        var bytesContent = new GlideSysAttachment().getBytes(attach);
        var strData = Packages.java.lang.String(bytesContent); // got rid of packages before
        try {
            hash = strData.split("\n")[1].split(",")[2];
            hash = hash.substring(0, hash.length() - 1);
        } catch (e) {
            // gs.log("Caught error", source);
        }
    }
    current.variables.hash_attachment = hash;
    current.setWorkflow(false);
    current.update();
})(current, previous);

Is it possible to convert into UTF-8 any modification required in the above business rule or any other approach need to follow kindly help me.
Thanks
6 REPLIES 6

Hi Sunil,
Thank you for the reply I tried this one as well but didn't work.

Gal Katz
Kilo Sage

Hi  Srinivas,
Did you find a solution to this requirement?
I'm facing the same difficulty.
Thanks!