HOw to convert HTML to plain text in a UI Action
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2018 04:02 PM
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't use XXX because it makes things worse!"
Any ideas how to remove the HTML special character formatting in a UI Action?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2018 05:13 PM
Check the below link if it helps
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2018 08:21 AM
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 ' from the word don$#39;t so it displays as don't.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2018 09:55 AM
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(/&/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.
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2020 09:34 AM
I was looking for this exact sort of thing and it works perfectly! Thanks so much for posting this.