Options
- 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.
1 ACCEPTED SOLUTION
Options
- 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
(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 tmp = new GlideElement();
tmp.setDisplayValue(htmlText);
var plainText = tmp.getDisplayValue().replace(/<[^>]*>/g, '');
result.push({
title: gr.getValue('short_description'),
article_body: plainText
});
}
response.setStatus(200);
response.setBody(result);
})(request, response);
If my response helped please mark it correct and close the thread so that it benefits future readers.
5 REPLIES 5
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2025 07:31 AM
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.