Removing HTML Tags

Jonathan102
Tera Guru

Hi all,

 

I Need help with a Business Rule script.  I am pulling data from fields on the idea table to fields on the demand table.  The issue is that I am pulling from a HTML field and inserting into a string field so I am getting <p> tags. 

 

I have tried the following script suggestion that I found in the community but it didn't remove the tags:

 

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

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


var gr = new GlideRecord("idea");
gr.addQuery("sys_id", current.idea.sys_id);
gr.query();
if (gr.next()) {

current.description = (gr.idea_description + "\n" + gr.u_idea_description_situation + "\n" + gr.u_idea_description_background + "\n" + gr.u_idea_description_assessment + "\n" + gr.u_idea_description_recommendation);
current.update();
}

})(current, previous);

 

 

Any suggestions?

 

Thanks,

Jonathan 

1 ACCEPTED SOLUTION

Thanks again Sai. 

 

This is the script that is now working for me, in case it helps anyone else.


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

 

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

 


    var gr = new GlideRecord("idea");
    gr.addQuery("sys_id", current.idea.sys_id);
    gr.query();
    if (gr.next()) {

 //html fields from idea table

        var fields = [];
        fields.push(decodeHTML(gr.getValue('idea_description')));
        fields.push(decodeHTML(gr.getValue('u_idea_description_situation')));
        fields.push(decodeHTML(gr.getValue('u_idea_description_background')));
        fields.push(decodeHTML(gr.getValue('u_idea_description_assessment')));
        fields.push(decodeHTML(gr.getValue('u_idea_description_recommendation')));

 
//all of the above fields getting pushed into the description field on the demand with space between them
        var description = fields.join("\n");

 

        current.description = description;
        current.update();
    }

 

})(current, previous);


Thanks,

Jonathan 

View solution in original post

4 REPLIES 4

Sai Shravan
Mega Sage

Hi @Jonathan102 ,

Here's an updated version of the decodeHTML function that uses DOM parsing to remove the HTML tags:

function decodeHTML(str) {
  var doc = new DOMParser().parseFromString(str, "text/html");
  return doc.body.textContent || "";
}

This function creates a new DOMParser object and uses it to parse the input string as HTML. It then retrieves the text content of the body element which automatically removes any HTML tags and returns only the text.

(function executeRule(current, previous /*null when async*/) {
  var gr = new GlideRecord("idea");
  gr.addQuery("sys_id", current.idea.sys_id);
  gr.query();
  if (gr.next()) {
    var description = [gr.idea_description,gr.u_idea_description_situation, gr.u_idea_description_background,      gr.u_idea_description_assessment,gr.u_idea_description_recommendation].join("\n");

    current.description = decodeHTML(description);
    current.update();
  }
})(current, previous);

 

Regards
Shravan

Please mark this as helpful and correct answer, if this helps you

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Thank you Sai.  The new script is not brining over any data from the html fields.  It is only brining over data from the idea_description field which is a string value.  

These are the html fields:

  • u_idea_description_situation
  • u_idea_description_background
  • u_idea_description_assessment
  • u_idea_description_recommendation

 

Thanks for you help!

Jonathan 

Thanks again Sai. 

 

This is the script that is now working for me, in case it helps anyone else.


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

 

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

 


    var gr = new GlideRecord("idea");
    gr.addQuery("sys_id", current.idea.sys_id);
    gr.query();
    if (gr.next()) {

 //html fields from idea table

        var fields = [];
        fields.push(decodeHTML(gr.getValue('idea_description')));
        fields.push(decodeHTML(gr.getValue('u_idea_description_situation')));
        fields.push(decodeHTML(gr.getValue('u_idea_description_background')));
        fields.push(decodeHTML(gr.getValue('u_idea_description_assessment')));
        fields.push(decodeHTML(gr.getValue('u_idea_description_recommendation')));

 
//all of the above fields getting pushed into the description field on the demand with space between them
        var description = fields.join("\n");

 

        current.description = description;
        current.update();
    }

 

})(current, previous);


Thanks,

Jonathan 

PedroRamos
Tera Contributor

I had to update mine to look like this:

function decodeHTML(str) {
        if (!str) return '';
        var a = str.replace(/<\/?[^>]+(>|$)/g, "");
        var b = a.replace(/&#(\d+);/g, function(match, dec) {
            return String.fromCharCode(dec);
        });
        var c = b
            .replace(/&rsquo;/g, '\u2019')
            .replace(/&lsquo;/g, '\u2018')
            .replace(/&ldquo;/g, '\u201C')
            .replace(/&rdquo;/g, '\u201D')
            .replace(/&hellip;/g, '\u2026')
            .replace(/&mdash;/g, '\u2014')
            .replace(/&ndash;/g, '\u2013')
            .replace(/&copy;/g, '\u00A9')
            .replace(/&nbsp;/g, ' ')
            .replace(/&quot;/g, '"')
            .replace(/&lt;/g, '<')
            .replace(/&gt;/g, '>')
            .replace(/&amp;/g, '&');
        return c;
    }