[SalesForce] How to retrieve just the text without formatting from a rich text field using JSON

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

Best Answer

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

Related Topic