- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2023 05:25 AM
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(/&/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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2023 08:50 AM
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(/&/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2023 06:35 AM
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
Shravan
Please mark this as helpful and correct answer, if this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2023 07:02 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2023 08:50 AM
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(/&/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2025 12:35 PM
I had to update mine to look like this: