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


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


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.