function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Matt UrbanowskiMatt Urbanowski 

Getting formatted HTML text in Apex

Is there any way to get just the "OuterHTML" text of a richtext field through Apex (NOT Visualforce)? I've checked all the String functions but none of them seem to do this.
e.g. the actual field content is: <body> This is the contents. </body>

I would like a trigger to return the value "This is the contents."

Is this possible?
Thanks,
Matt
Best Answer chosen by Matt Urbanowski
Dushyant SonwarDushyant Sonwar
Hi Matt,

Are you saying that you want a plain text from the html? If that's you wanted , then you can try stripHtmlTags(htmlInput) string function of apex.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm
Hope this helps.

All Answers

Dushyant SonwarDushyant Sonwar
Hi Matt,

Are you saying that you want a plain text from the html? If that's you wanted , then you can try stripHtmlTags(htmlInput) string function of apex.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm
Hope this helps.
This was selected as the best answer
Matt UrbanowskiMatt Urbanowski
Thanks! Exactly what I was looking for. Can't believe I missed that function.
Aradhika Chawla 73Aradhika Chawla 73
But stripHtmlTags(htmlInput) is removing the newline for me. How to resolve that ? And also links are no more links as well ?