How to get raw text from HTML field?

tombialkowski
Giga Expert

Hi all,

We have a catalog item with a description (Example - "Description of Cat Item"), but this is an HTML field and the escription field of the actual catalog tasks are plain text. From within a workflow I am trying to retrieve the plain text version of the Catalog Item description field, and assign it to the description field in the Catalog Task that's created.

Here's what I've got so far:

        task.description = current.cat_item.description.getDisplayValue();

It works in the sense that it populates the Catalog Task description, but the HTML tags are included as well (Example - <p>Description of Cat Item</p>)

I know some old non-ServiceNow tricks where you can use a DIV tag to get to the textContent, but I'm not sure how to go about this in ServiceNow. Any suggestions?

1 ACCEPTED SOLUTION

tombialkowski
Giga Expert

Disregard - I went the route of using a regexp to trim out the tags.


View solution in original post

5 REPLIES 5

tombialkowski
Giga Expert

Disregard - I went the route of using a regexp to trim out the tags.


Hi Tom,



Would you be willing to share the script you used?



Antone


Morning Antone,



- I set the description fields of the individual catalog items to reflect what I want them to say.


- I then use the following script from within the workflow to 1) replace <br /> with true CRLF's (linebreaks), 2) replace all remaining tags with an empty space and 3) assign the newly formatted text to the catalog task's description field.




// Retrieve the 'Description' HTML from the Catalog Item


var text = current.cat_item.description.getDisplayValue();



// Replace all <br /> tags with a CRLF


var regX1 = /<br\s\/>/ig;


var text2 = text.replace(regX1, String.fromCharCode(13));



// Replace all remainging HTML tags with ""


var regX2 = /(<([^>]+)>)/ig;


var finalText = text2.replace(regX2, "");



// Set the Catalog Task's 'Description' field to the raw text of the Catalog Items 'Description' field


task.description = finalText;




I'll likely turn this into a script include and make it a function call at some point, but this get's me by for now. Hope it helps:)


Thank you!



Tone