Remove Rich Text Formatting in Flows
There are times in which you want to copy text from a Rich Text Area field to a Long Text Area field. When you try to implement this in your flow you’ll find that the Long Text Area field will not just have the text, it’ll also contain the HTML tags.
Use Case:
When a Case is created with Status = New, a Follow Up Task should be created with the following field values:
1- Task: OwnerId = Case: CreatedById
2- Task: Subject = Follow Up
3- Task: WhoId = Case: ContactId
4- Task: Comments = Case: Description RichText
Please note that I created a Description RichText field on the Case object to test our case:
The flow looks something like this:
It’s a Record Triggered flow that runs After Create on the Case Object.
Decision Element:
The Decision Element is there to check if the Case Status = New. If the Case Status is New then we can create the Task record.
Create Records:
When you test this flow by creating a Case, you’ll find that all the fields were populated correctly except for the Comments field. That’s because we are trying to copy text from a Rich Text field to a Long Text field, the result is that the HTML tags like <p> and <b> are appearing as well.
Solution:
Sometimes your flow needs to do more than the default elements allow, so with a little help from Apex I was able to remove the HTML tags in the Comments field. In this flow I’m calling an Apex Action to remove the rich text formatting.
What Changed?
1- I created an apex class that has an Invocable Method. This method will be called by the flow Apex Action to remove the HTML tags from the Description RichText field.
2- I created a Resource in my flow that will hold the text returned from the Apex Method.
Resource Type: Variable | API Name: plainText | Data Type: Text
3- I added the Apex Action to my flow just before the Create Records element. The Apex Action will call the Invocable Method and pass the Description RichText field as input and produce the output value in the plainText variable I created above.
4- I updated the Create Records element to assign the plainText variable that’s been returned from the Apex Action to the Comments field on the Task object.
Run the Flow
Now when you try to create a Case again to fire this flow, the HTML tags are removed successfully.
Happy Coding! :)