6

I am querying a rich text field that contains text information using a public rest service using this method http://blogs.developerforce.com/developer-relations/2012/02/quick-tip-public-restful-web-services-on-force-com-sites.html

The problem is that as you can see in the image the formatting added in Salesforce is returned in the response, how can I strip that out and just return the text ?

enter image description here

2 Answers 2

6

You normally have to do this at the client end, e.g. in your code, by parsing the HTML and then using the W3C DOM method getTextContent on the root node (for example).

However in reviewing the documentation on Rich Text fields, it seems that if you reference the field in a Formula the HTML markup is stripped out.

When a rich text area field is used in a formula, the HTML tags are stripped out before the formula is run. For example, when a rich text area field is used in a validation rule’s criteria, the HTML tags are removed before the evaluation.

You cannot directly reference a Rich Text field in a regular Formula Field, however you can reference it, in addition to validation rules, in a Workflow Field Update. In the example below I created a "Rich Text" field (Rich Text type) and "Rich Text Text" field (regular Text Area type). Then I create a Workflow and Workflow Field Update to update the Rich Text Text field each time the user changed the Rich Text field.

enter image description here

enter image description here

With something like this in place you can query for the text only version in your REST request. The following shows my Workflow definition if you need to understand this further.

enter image description here

enter image description here

1
  • Unfortunately, it appears that the Formula engine in Flow does not strip HTML. I tried to replicate this solution using Flow (as we can no longer create new Workflows), and the Long Text Area field ends up full of HTML. Would love to find a modern solution; it seems silly that the system can strip HTML quite easily, but there's now way to do it programmatically. Commented Nov 13, 2023 at 20:19
0

Another perhaps more cohesive option (assuming as in the link in your question you are exposing the data through your own Apex class) is to use a regular expression to remove the markup.

There are posts that indicate that regular expressions are not the best tool for this job and other posts that claim success. One in the latter category is this one How to get a rich text area content without HTML markups.

The HTML markup in rich test fields is inserted by Salesforce code and so is hopefully relatively consistent in its structure compared to arbitrary HTML pages.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .