Conditionally Change Color of Text Field Values in Salesforce

Conditionally Change Color of Text Field Values in Salesforce

Big Idea or Enduring Question:

  • How do you display text field values in different colors based on the opportunity stage? 


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

  • Understand when to use before-save flow
  • Use the formula to display texts in different colors
  • Use the SUBSTITUTE function to remove HTML tags from the rich text area
  • Use assignment element to update the rich text area field 
  • and much more

Business Use case

Olivia Rodrigo is working as a System Administrator at Gurukul on Cloud (GoC). Olivia has received a requirement from her sales director to display the Deal Analysis field text in green color if the opportunity is open or won; otherwise, it shows text in red. 

Automation Champion Approach (I-do):

There are a few possible solutions for the above business scenario. However, our purpose, in this blog, is to learn about Flow and its features. Therefore, we will use Before-Save Record-triggered Flow to solve the above business requirement.

Using before-save allows us to accomplish the goals much faster because each record does not get saved to the database again. Avoiding that extra save procedure means skipping another round of Assignment rules, Auto-response rules, and other customizations that take time to execute.

Use a Before-save Flow on the following use cases:

  1. Update fields on new or changed records
  2. Trigger a Validation rule (this is a custom solution not available out of the box)

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 3 steps to solve Olivia’s business requirement using Before-Save Record-Triggered Flow. We must:

  1. Create a custom field on the Opportunity object
  2. Salesforce Flow
    1. Define flow properties for record-triggered flow
    2. Create a formula to change the font color of the text field value 
    3. Add an Assignment element to the opportunity field

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

Remember the most recent release Salesforce doesn’t support conditional formatting of field values even for the formula field. Only rich text fields support formatting. Let’s go ahead and create a rich text area field to store the deal analysis.

  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.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 the Flow When: A record is created or updated
    3. Set Entry Criteria
      1. Condition Requirements: None
    4. Optimize the Flow For Fast Field Updates
  5. Click Done.

Step 2.2: Formula to Change Font Color of the Text Field Values 

  1. Under Toolbox, select Manager, then click New Resource to construct the opportunity name using concatenate formula. 
  2. Input the following information:
    1. Resource Type: Formula
    2. API Name: forT_DealAnalysisColorText
    3. Data Type: Text
    4. Formula:
      1. IF(TEXT({!$Record.StageName}) ='Closed Lost', ('<html><body><div style="color: red;">' & SUBSTITUTE(SUBSTITUTE(SUBSTITUTE({!$Record.rakeshistomMVP__Deal_Analysis__c}, '<div style="color: green;">', ''), '<div style="color: red;">', ''), '</div>', '') & '</div></body></html>'),
        IF(TEXT({!$Record.StageName}) ='Closed Won', ('<html><body><div style="color: green;">' & SUBSTITUTE(SUBSTITUTE(SUBSTITUTE({!$Record.rakeshistomMVP__Deal_Analysis__c}, '<div style="color: green;">', ''), '<div style="color: red;">', ''), '</div>', '')& '</div></body></html>'),
        IF({!$Record.IsClosed}=False, ('<html><body><div style="color: green;">' & SUBSTITUTE(SUBSTITUTE(SUBSTITUTE({!$Record.rakeshistomMVP__Deal_Analysis__c}, '<div style="color: green;">', ''), '<div style="color: red;">', ''), '</div>', '')& '</div></body></html>'),
  3. Click Done.

SUBSTITUTE() formula used to substitute new text for old text in a text string.

Step 2.3. Using Assignment Element to Update the Opportunity FIeld

The next step is to update the opportunity Deal Analysis field with formatted values. We will use the Assignment element, as we are using before-save flow. 

  1. On Flow Designer, click on the +icon and select the Assignment element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Set Variable Value
    1. Row 1:
      1. Field: {!$Record.rakeshistomMVP__Deal_Analysis__c}
      2. Operator: Equals
      3. Value: {!forT_DealAnalysisColorText}
  4. Click Done.

In the end, Olivia’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: Colorful Text {!$Flow.CurrentDateTime}
  6. Click Save

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

Proof of Concept

Now onwards, if a user updates the opportunity stage, then the Before-save record-triggered flow will automatically update the Deal Analysis field text value. 

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!

Preferred Timing(required)

3 thoughts on “Conditionally Change Color of Text Field Values in Salesforce

  1. Hi Rakesh. Personally I steer away from assigning text box sizes to the full 32,768 characters. 1) I’ve yet to see many real live use cases where the all characters are genuinely required, 2) when uploading the record into excel (for analysis or mass update) I find Excel often crashes (due to hitting or exceeding a cell size limit) . Therefore I prefer to use 30,000 characters an absolute maximum top limit.
    Apart from that, as ever, as useful and well explain article. Thank you.

Leave a Reply

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