Translate ASCII Code to its corresponding characters

Michael Galang
Tera Contributor

Hello experts, 

I need your help to Translate ASCII Code to its corresponding characters once the data is transform to its designated field. As you can see, the code is still reflected.

MichaelGalang_1-1751374679665.png
We have created a business rule to translate it but still not working. 

Business rule: 

Conditions: current.u_follow_up_communication != null && current.u_follow_up_communication !== ''

(function executeRule(current, previous /*null when async*/) {

var fieldName = 'u_follow_up_communication';

function htmlToUtf8PlainText(str) {
 
    str = str.replace(/&lt;/g, '<')
             .replace(/&gt;/g, '>')
             .replace(/&amp;/g, '&')
             .replace(/&quot;/g, '"')
             .replace(/&#39;/g, "'")
             .replace(/&#(\d+);/g, function(match, dec) {
                 return String.fromCharCode(dec);
             });

    // Remove HTML tags
    str = str.replace(/<\/?[^>]+(>|$)/g, "");

    // Clean up whitespace
    str = str.replace(/\s+/g, ' ').trim(); //

    // Encode the string as UTF-8
    return unescape(encodeURIComponent(str)); // Ensure UTF-8 encoding
}
var gr = new GlideRecord('sn_hr_core_case_relations');
gr.query();

while (gr.next()) {
   
    var currentValue = gr.getValue(fieldName);

    if (currentValue) {
        // Convert HTML to UTF-8 plain text
        var newValue = htmlToUtf8PlainText(currentValue);
        gr.setValue(fieldName, newValue);
        gr.update();
    }
}

})(current, previous);



I would appreciate any help regarding this matter.  

 

 

 

1 ACCEPTED SOLUTION

@Michael Galang 

then why not handle it in the field map or transform script itself

if you are using business rule then please use before update business rule on "sn_hr_core_case_relations" table

1) no need to use GlideRecord again

use this script

(function executeRule(current, previous /*null when async*/) {

    var fieldName = 'u_follow_up_communication';

    function htmlToPlainText(str) {
        if (!str) return '';
        // Decode common HTML entities
        str = str.replace(/&lt;/g, '<')
                 .replace(/&gt;/g, '>')
                 .replace(/&amp;/g, '&')
                 .replace(/&quot;/g, '"')
                 .replace(/&#39;/g, "'")
                 // Decimal ASCII entities
                 .replace(/&#(\d+);/g, function(match, dec) {
                     return String.fromCharCode(dec);
                 })
                 // Hexadecimal ASCII entities
                 .replace(/&#x([0-9A-Fa-f]+);/g, function(match, hex) {
                     return String.fromCharCode(parseInt(hex, 16));
                 });

        // Remove HTML tags
        str = str.replace(/<\/?[^>]+(>|$)/g, "");
        // Clean up whitespace
        str = str.replace(/\s+/g, ' ').trim();
        return str;
    }

    var currentValue = current.getValue(fieldName);
    if (currentValue) {
        var newValue = htmlToPlainText(currentValue);
        if (newValue !== currentValue) {
            current.setValue(fieldName, newValue);
        }
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Michael Galang 

how is the data getting captured in that field?

Are you using some transform map etc?

business rule is on which table?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

Thank you for your response. We are using transform map. The source of the data is from the Integrated software to Servicenow. Once the scheduled job run, it will pull the data and create and update its corresponding  servicenow record. 
The business rule is configured to sn_hr_core_case_relations. 





@Michael Galang 

then why not handle it in the field map or transform script itself

if you are using business rule then please use before update business rule on "sn_hr_core_case_relations" table

1) no need to use GlideRecord again

use this script

(function executeRule(current, previous /*null when async*/) {

    var fieldName = 'u_follow_up_communication';

    function htmlToPlainText(str) {
        if (!str) return '';
        // Decode common HTML entities
        str = str.replace(/&lt;/g, '<')
                 .replace(/&gt;/g, '>')
                 .replace(/&amp;/g, '&')
                 .replace(/&quot;/g, '"')
                 .replace(/&#39;/g, "'")
                 // Decimal ASCII entities
                 .replace(/&#(\d+);/g, function(match, dec) {
                     return String.fromCharCode(dec);
                 })
                 // Hexadecimal ASCII entities
                 .replace(/&#x([0-9A-Fa-f]+);/g, function(match, hex) {
                     return String.fromCharCode(parseInt(hex, 16));
                 });

        // Remove HTML tags
        str = str.replace(/<\/?[^>]+(>|$)/g, "");
        // Clean up whitespace
        str = str.replace(/\s+/g, ' ').trim();
        return str;
    }

    var currentValue = current.getValue(fieldName);
    if (currentValue) {
        var newValue = htmlToPlainText(currentValue);
        if (newValue !== currentValue) {
            current.setValue(fieldName, newValue);
        }
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur, 

We followed your advice to handle the conversion in the field mapping script of the Follow-Up Communication field, and it worked. Thanks alot.