- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-05-2025 05:58 AM
I'm creating scripted rest api and I want to return a filed that is HTML in plain text
(function process(request, response) {
var searchText = request.body.data.text;
if (!searchText) {
response.setStatus(400);
response.setBody({ error: "Missing 'text' parameter" });
return;
}
var result = [];
var gr = new GlideRecord('kb_knowledge');
gr.addEncodedQuery('textLIKE' + searchText + '^workflow_state=published');
gr.query();
while (gr.next()) {
var htmlText = gr.getValue('text') || '';
// ✅ Strip HTML tags safely using regex
var plainText = htmlText.replace(/<[^>]*>/g, '');
result.push({
title: gr.getValue('short_description'),
article_body: plainText.trim()
});
}
response.setStatus(200);
response.setBody(result);
})(request, response);
still no luck
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-05-2025 06:16 AM
try to strip the tags
use this
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-05-2025 06:06 AM
Try this:
var plainText = gr.text.getHTMLValue(20).replace(/<\/?[^>]+(>|$)/g, "")
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-05-2025 06:10 AM
Try this @eyal abu hamad
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 process(request, response) {
var searchText = request.body.data.text;
if (!searchText) {
response.setStatus(400);
response.setBody({
error: "Missing 'text' parameter"
});
return;
}
var result = [];
var gr = new GlideRecord('kb_knowledge');
gr.addEncodedQuery('textLIKE' + searchText + '^workflow_state=published');
gr.query();
while (gr.next()) {
var htmlText = gr.getValue('text') || '';
// ✅ Strip HTML tags safely using regex
var plainText = decodeHTML(htmlText); //htmlText.replace(/<[^>]*>/g, '');
result.push({
title: gr.getValue('short_description'),
article_body: plainText.trim()
});
}
response.setStatus(200);
response.setBody(result);
})(request, response);
source
HOw to convert HTML to plain text in a UI Action - ServiceNow Community
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-05-2025 06:16 AM
try to strip the tags
use this
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-05-2025 08:26 PM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader