Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

HOw to convert HTML to plain text in a UI Action

gjz1
Giga Expert

I have a requirement to update the contents of a field from one table to the field of another table if a specific action occurs.  The sending table's field is HTML, the receiving table's field is string.

I need to convert the HTML field to plain text so it renders correctly in the receiving table.  I've searched the community and find a lot of information (that works) to convert the tags but nothing to convert the special characters.

My UI Action:

var rhtml = current.html_field.getDisplayValue();
var txtDesc = rhtml.replace(/<\/?[^>]+(>|$)/g, "");

var gr = new GlideRecord("my_table");
gr.get(current.my_table);
gr.setValue("string_field", txtDesc);
my_table.update();

I entered "So, don't use XXX because it makes things worse!" in the HTML field, after running the above UI Action the data in the text field is "So, don&#39;t use XXX because it makes things worse!"

Any ideas how to remove the HTML special character formatting in a UI Action?

 

5 REPLIES 5

SanjivMeher
Mega Patron
Mega Patron

Check the below link if it helps

 

https://community.servicenow.com/community?id=community_question&sys_id=1c92c721dbd8dbc01dcaf3231f96...


Please mark this response as correct or helpful if it assisted you with your question.

gjz1
Giga Expert

I did see that post already but it doesn't cover what I need.  I'm trying to remove the HTML symbols.  For example, I need to remove &#39; from the word don$#39;t so it displays as don't.

gjz1
Giga Expert

I have found a solution for myself, I created the following function that seems to convert everything to text - at least it meets my needs.  It would be nice if ServiceNow would add a built-in function to convert from HTML to text.

function decodeHTML(str) {
    var a = str.replace(/<\/?[^>]+(>|$)/g, ""); //Remove tags
    var b = a.replace(/&amp;/g, '&'); //Retain any ampersands that are just ampersands
    return b.replace(/&#(\d+);/g, function(match, dec) {
        return String.fromCharCode(dec); //Returns the special character from the decimal code representation and returns the entire decoded string.
    });
}

 

David Lundy
Tera Contributor

I was looking for this exact sort of thing and it works perfectly!  Thanks so much for posting this.