- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2018 12:41 AM
Hi All,
From the Knowledge table, I want to get the content from the field text. The content in the field is of type HTML.
I do not want the entire contents to be retrieved, I'm interested in only a part of it.
If I use the GlideElement getHTMLValue(maxChars) method, it returns even the HTML tags.
Like why is this happening. How can I retrieve only the text having 20 characters.
Here is my code
function getTextContents(query) {
var textContents = [];
var gr = new GlideRecord('kb_knowledge');
gr.addEncodedQuery(query);
gr.query();
while (gr.next()) {
textContents.push (gr.text.getHTMLValue(20));
}
return textContents;
}
Thanks in Advance
Balu
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2018 02:17 AM
Hi Balu,
Please use following.
gr.text.getHTMLValue(20).replace(/<\/?[^>]+(>|$)/g, "");
Thanks
harshad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2018 02:17 AM
Hi Balu,
Please use following.
gr.text.getHTMLValue(20).replace(/<\/?[^>]+(>|$)/g, "");
Thanks
harshad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2018 04:39 AM
Hi Harshad,
Isn't using a string replacement a bad idea in this case.
Like, if I have a lot of HTML tags at the start, say a HTML table or so, then the HTML tags itself might occupy 18 characters. So a string replacement will result in 2 characters only.
And also this is dynamic, like some text can contain a lot of HTML tags at the beginning, and others do not. So there cannot be a generalization.
I thought there was some GlideElement method to get only the text contents omitting the HTML value. This would result in correct value (unless, I get all the text, replace it and then get first 20 characters, which is an overhead here as this method is called frequently and the length of text field can be huge).
Thanks
Balu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2018 05:43 AM
yep, I totally agree with you.
I looked at some post regarding removing this tags but found nothing but this replace was used frequently.
I did not find any glide element method for this:(.
Thanks
Harshad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2018 08:48 AM
Hi Harshad,
I asked this question to the product team. Looks like the method what you have told before is the only approach.
Thanks for provide a solution. Like this would be useful for others as well, if they refer to this question.
Thanks
Balu