How to remove HTML tags from a field prior to export

GChanner
Tera Guru

Thought I would share the following use case.

 

Issue: When exporting data from html fields, the downloaded data contains HTML tags. We needed a solution that'll  remove the html tags prior to the exporting the data.

After much searching in the forums we found the following solution below that worked very well for us. Thanks  @Holly @WakeMed

You can modify it to work on any html fields.

 

Solution:

Step 1

Create a new string field on the desired form which contains the html field.

 

Step 2

Create a before business rule on the table. (In this example the task table because we are working with the description field.)

Note: This runs on the task table when description changes.

 

===== Business rule ====

(function executeRule(current, previous /*null when async*/) {
current.u_display_description = decodeHTML(current.description);
})(current, previous);

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.
});
}

 

We also modified it as shown below to work in the 'Calculated value' for other forms. This step does not require a BR.

 

decodeHTML(current.u_project_goal_statement.toString());

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.
});
}

 

Worked like a charm 😊

1 REPLY 1

-O-
Kilo Patron

How about

new GlideSPScriptable().stripHTML('HTML source')

for tag remover?