Remove Rich Text Formatting Using Flow

Remove Rich Text Formatting Using Flow

Last Updated on April 25, 2022 by Rakesh Gupta

Big Idea or Enduring Question:

  • How do you automatically remove HTML markup from the rich text area field and return plain text?

Objectives:

After reading this blog, you’ll be able to: 

  • Understand how to use stripHtmlTags sting method
  • Understand how to check Text Area (Rich) ISNULL or ISBLANK
  • Call an Invocable apex class from the flow
  • Stripe out HTML tags from a rich text field
  • and much more

Business Use case

Arda Turan is working as a System Administrator at Gurukul on Cloud (GoC). Arda is in the process of implementing opportunity management at GoC. He has a requirement to store values from Rich Text (Area) Deal Analysis in plain text to Long Text (area) Deal Analysis Text field. 

Automation Champion Approach (I-do):

Earlier this week I needed to remove some HTML tags from a Rich Text (Area) field, the target string was already saved with HTML tags, and one of the requirements specifies that we need to render it as a raw text and store it in another field. 

To strip out all the HTML tags from a string there is a specific string method i.e. stripHtmlTags. This function is used to remove HTML markup and returns plain text. As of the current release, it is not possible to directly call String Class methods from the salesforce flow, that is why we are going to implement an invocable Apex class.

To solve this requirement, we will use the After-save Record-Triggered Flow. Check out this article to understand why we are using after-save record-triggered flow for this scenario. 

Before discussing the solution, let me show you a diagram of a Process Flow at a high level. Please spend a few minutes going through the following Flow diagram and understand it.

Let’s begin building this automation process.

Guided Practice (We-do):

There are 4 steps to solve Arda’s business requirement using After-Save Record-Triggered Flow. We must:

  1. Create a custom Text Area (Rich) field on the Opportunity object
  2. Create a custom Text Area (Long) field on the Opportunity object
  3. Create Apex class & Test class
  4. Salesforce Flow Steps:
    1. Define Flow properties for record-triggered flow
    2. Create a formula to determine the length of the Rich Text(Area) field
    3. Add a decision element to check if the length is greater than 0
    4. Add action – call an Apex class to remove HTML markup
    5. Add an update records element to update the opportunity

Step 1: Create a Custom Field on the Opportunity Object to Store Deal Analysis 

First of all, we will create a Text Area (Rich) field to store deal analysis, as mentioned below:

  1. Click Setup.
  2. In the Object Manager, type Opportunity.
  3. Select Fields & Relationships, then click New.
  4. Select Text Area (Rich) as Data Type, then click Next.
  5. Enter Field Label and click the tab key, the Field Name will populate. 
    1. Enter the details:
      1. Length 32,768.
      2. # Visible Lines 10.
      3. As a best practice, always input a description and Help Text. 
      4. Click on the Next button.
  6. Set the Field-level Security for the profiles 
  7. Add this field to Page Layout.
  8. Click Save.

Step 2: Create a Custom Field on the Opportunity Object to Store Deal Analysis in Plain Text

The next step is to create a Text Area (Long) field to store deal analysis in plain text format, as mentioned below:

  1. Click Setup.
  2. In the Object Manager, type Opportunity.
  3. Select Fields & Relationships, then click New.
  4. Select Text Area (Long) as Data Type, then click Next.
  5. Enter Field Label and click the tab key, the Field Name will populate. 
    1. Enter the details:
      1. Length 32,768.
      2. # Visible Lines 3.
      3. As a best practice, always input a description and Help Text. 
      4. Click on the Next button.
  6. Set the Field-level Security for the profiles makes sure that the field is read-only.   
  7. Add this field to Page Layout.
  8. Click Save.

Step 3: Create an Apex class and Test class   

  1. Click Setup.
  2. In the Quick Find box, type Apex Classes.
  3. Clicks on the New button.
  4. Copy code from GitHub and paste it into your Apex Class. 
  5. Click Save.

Repeat the above steps and click the Test class. You can get the code from my GitHub repo. 

Step 4.1: Define Flow Properties

  1. Click Setup.
  2. In the Quick Find box, type Flows.
  3. Select Flows then click on the New Flow.
  4. Select the Record-Triggered Flow option, click on Create and configure the flow as follows:
    1. Object: Opportunity
    2. Trigger Opportunity Flow When: A record is created or updated
    3. Set Entry Criteria
      1. Condition Requirements: None
    4. Optimize the Flow For Action and Related Records
  5. Click Done.

Step 4.2: Formula to Calculate the Text Area (Rich) Length 

Rich text fields are a little wonky. ISNULL and ISBLANK don’t work with this field type. To check NULL on this data type field we have to use LEN to calculate the number of characters.

  1. Under Toolbox, select Manager, then click New Resource to calculate Deal Analysis field length. 
  2. Input the following information:
    1. Resource Type: Formula
    2. API Name: forN_DealAnalysisLength
    3. Data Type: Number
    4. Decimal Places: 0
    5. Formula: LEN({!$Record.rakeshistomMVP__Deal_Analysis__c})
  3. Click Done.

Step 4.3: Using Decision Element to Check if the Formula Length

Now we will use the Decision element to check if the deal analysis field is greater than 0 characters or not. 

  1. On Flow Designer, click on the +icon and select the Decision element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Under Outcome Details, enter the Label the API Name will auto-populate.
  4. Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
    1. Row 1:
      1. Resource: {!forN_DealAnalysisLength}
      2. Operator: Greater Than
      3. Value: 0
  5. When to Execute Outcome: If the condition requirements are met
  6. Click Done.

Step 4.4: Add an Action to Call Apex class to Remove HTML Markup

  1. On Flow Designer, below the Yes node, click on the +icon and select the Action element.
  2. Search and select the RemoveHtmlMarkup apex class from the dropdown menu
  3. Enter a name in the Label field; the API Name will auto-populate.
  4. Set Input Values:
    1. Field: richText
    2. Value: {!$Record.rakeshistomMVP__Deal_Analysis__c}
  5. Select Manually assign variables
  6. Store Output Values:
    1. Field: output
    2. Value: {!$Record.rakeshistomMVP__Deal_Analysis_Text__c}
  7. Click Done.

Step 4.5: Add Action – Update Records

 The next step is to update the opportunity record, for this, we will use the Update Records element.

  1. On Flow Designer, click on the +icon and select the Update Records element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. For How to Find Records to Update and Set Their Values select Use the IDs and all field values from a record or record collection
  4. Select Record(s) to Update 
    1. Record or Record Collection: {!$Record}
  5. Click Done.

In the end, Arda’s Flow will look like the following screenshot:

Once everything looks good, perform the steps below: 

  1. Click Save.
  2. Enter Flow Label the API Name will auto-populate.
  3. Click Show Advanced.
  4. API Version for Running the Flow: 55
  5. Interview Label: Remove HTMl Markup {!$Flow.CurrentDateTime}
  6. Click Save

Almost there! Once everything looks good, click the Activate button.  

Proof of Concept

Now onwards, when a business user updates the Deal Analysis Text Area (Rich) field, the Record-triggered flow will automatically store the plain text in the Deal Analysis Text field. 

Formative Assessment:

I want to hear from you!

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

6 thoughts on “Remove Rich Text Formatting Using Flow

  1. Rakesh, thanks for the informative post. I am unable to deploy because of insufficient test code coverage. I don’t see where the stripHtmlTags method is being invoked in the test class, but maybe I am missing something.

  2. I cannot for the life of me get the Apex to show up as an Apex Action option in my flow. No matter what I try (changed the flow API version, made the Apex global, added input and output variables, recomplied my Apex classes), it won’t appear as an option for me to seel

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.