Stripping HTML from 1 field to another as part of a flow?

Ben28
Tera Contributor

I'm trying to create a flow, so when a Problem moves from RCA, a new PTASK is opened, and assigned to the Problem Owner to implement to corrective actions identified in the Fix Notes. I'm trying to pull fix_notes, (which is HTM enabled) from the Problem, and copy it to the PTASK description (which his plain text). I can get the fix_notes content to copy, but it comes over with all the html tags (<p>, < /br> etc.). I tried using a replace string transform function, within the flow, and using a regex to replace the string, but this results in a glide error when testing: com.glide.glideobject.GlideHTML@1e34d97. Is a transform the correct way to do this,, and if so, how, or is there a better way?

Ben28_0-1727214751952.png

 

5 REPLIES 5

Amit Verma
Kilo Patron
Kilo Patron

Hi @Ben28 

 

Refer below post and try the solution provided by Mak :

https://www.servicenow.com/community/developer-forum/remove-html-tags-when-copying-into-a-string-fie...

 

Thanks and Regards

Amit Verma


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

Amit Verma
Kilo Patron
Kilo Patron

@Ben28 

 

Tried with transformation function as well. Somehow, Replace String doesn't seems to work with html tags. Found an alternative and easy way to fix this. Please refer below steps :

 

1. Suppose I have a flow variable in which HTML is stored :

 

AmitVerma_0-1727237886584.png

 

2. To get rid of the HTML tags, we will make use of Scripting and regex. With replace function, we will replace the HTML tags with a null :

 

AmitVerma_1-1727238009860.png

 

var htmlString = fd_data.flow_var.accountnametocreate;
var plainText = htmlString.replace(/<[^>]+>/g,'');
return plainText;

 Output :

AmitVerma_2-1727238083364.png

 

 


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

Ben28
Tera Contributor

Hi, thanks for this, I got it somewhat working using "var htmlString = fd_data.trigger.current.fix_notes;" to pull the fix_notes details, and strip off the html. However, it is possible to replace the html line breaks with carriage returns? What I end up with currently, is 3 lines of HTML, all combined into 1. For example:

Corrective Action/Preventive Action Plan: Install more server memory

Change Required?: Yes/No

Tentative Implementation date: 

 

Becomes:

Corrective Action/Preventive Action Plan: Install more server memoryChange Required?: Yes/No Tentative Implementation date: 

Ben28
Tera Contributor

OK, think I got this working now. Had to add a new regex expression to replace <br> and <p> with /n first.

 

var ptaskinstructions = "This PTASK is to track CAPA Implementation.\n" +
"It can be reassigned to the team & person responsible for Implementation the actions identified in the RCA.\n" +
"Additional PTASKs can be created if required.\n";
var htmlString = fd_data.trigger.current.fix_notes;
const plainText = decodeHTML(htmlString);
//var plainText = htmlString.replace(/<[^>]+>/g,'');
combinedText = ptaskinstructions + plainText;
return combinedText;

function decodeHTML(str) {
var a = str.replace(/<br\s*\/?>|<p\s*\/?>/gi, '\n'); //Retain line breaks
var b = a.replace(/<\/?[^>]+(>|$)/g, ""); //Remove tags
var c = b.replace(/&amp;/g, '&'); //Retain any ampersands that are just ampersands
return c.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.
});
}