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

Amit Pandey
Kilo Sage

Hi @Srinivas Goud K 

 

Interesting scenario-

 

Can you try this once-

 

(function executeRule(current, previous /*null when async*/ ) {
    var hash = "";
    var attach = new GlideRecord("sys_attachment");
    attach.addQuery("table_sys_id", current.sys_id); 
    attach.query();
    if (attach.next()) {
        var bytesContent = new GlideSysAttachment().getBytes(attach);
        var decoder = new TextDecoder("utf-8");
        var strData = decoder.decode(bytesContent);
        try {
            hash = strData.split("\n")[1].split(",")[2];
            hash = hash.substring(0, hash.length() - 1);
        } catch (e) {
            // Handle error
        }
    }
    current.variables.hash_attachment = hash;
    current.setWorkflow(false);
    current.update();
})(current, previous);

 

https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder/TextDecoder

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB1118085

 

Regards,

Amit

Hi Amit, 
Thanks for the response I tried your modified code but didn't work it is storing as empty value in the variable

Hi @Srinivas Goud K 

 

Can you try this in background script and share the output so that I can debug it?

 

Regards,

Amit

Maddysunil
Kilo Sage

@Srinivas Goud K 

Give a try to below code:

 

 

(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 = new GlideStringUtil().getString(bytesContent, "UTF-8"); // Convert bytes to string using UTF-8 encoding
        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);

 

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks