Unlock the Power of Color in Your Reports and Page Layouts

Other than using Rich Text fields, there are rigid limits in regards to how fields appear on a page layout or report.
In the Account report below, you can see a text field that appears in different colors depending on the industry of the account. This article shows one way to obtain that functionality.

ReportColoredText

Below is the formula used to display the colored text:

IMAGE( “/apex/TextToImage?color=”
+
IF( CONTAINS( ‘Energy|Construction|Transportation|Shipping|Utilities|Manufacturing|Agriculture’, TEXT( Industry ) ), ‘red’, ‘green’ )
+ “&text=”
+ SUBSTITUTE( Name, “&”, “%26amp;” ) + ” (”
+ IF( CONTAINS( ‘Energy|Construction|Transportation|Shipping|Utilities|Manufacturing|Agriculture’, TEXT( Industry ) ), ‘Basic’, ‘Service’ ) + ” Industry)”, “Test” )

Notice how it starts with an IMAGE() function with a concatenation of:

  1. a link to a VisualForce page
  2. a parameter “color” followed by…
  3. …an IF() function that determines whether the text should appear in red or green
  4. a parameter “text” followed by…
  5. …a SUBSTITUTE() function that replaces any “&” character in the Account Name with an equivalent escape code
  6. another IF() function that determines whether to append the words “(Basic Industry)” or “(Service Industry)” depending on the picklist value of the Account Industry

That is right, this formula transforms a text field into an image using a VisualForce page! How is that possible?

Here is the page markup for TextToImage:

<apex:page sidebar="false" showHeader="false"
 applyHtmlTag="false" applyBodyTag="false" 
 standardStylesheets="false" 
 ContentType="image/svg+xml" >
    <apex:variable var="textWidth" 
         value="{! LEN( $CurrentPage.parameters.Text ) * 10 }"/>
    <apex:variable var="textColor" 
         value="{!IF( $CurrentPage.parameters.Color = null, 
                  'red', $CurrentPage.parameters.Color )}" />

    <svg id="layer_1" 
        xmlns="http://www.w3.org/2000/svg" 
        xmlns:xlink="http://www.w3.org/1999/xlink" 
        height="20" width="{! textWidth }">
      <text x="0" y="15" font-family="Verdana" 
          font-weight="bold" fill="{! textColor }">
          {! $CurrentPage.parameters.Text }
      </text>
    </svg>
</apex:page>

In this page there are 3 unusual things:

  1. no controller, no header, no sidebar, no style sheet
  2. the ContentType is set to image/svg+xml
  3. the SVG tag containing a TEXT tag

The SVG image format – Scalable Vector Graphics – is relatively less known standard but it is very versatile and compatible with virtually all browsers. It allows us to define an image using plain XML.

In order to generate an image, this VisualForce page uses the parameters Text and Color. Color can be any CSS color code such as #FF00FF, #00A0C0, etc or a CSS color name such as red, green, darkblue, etc.
An URL like below would make the text “This is a test.” appear in cyan:

/apex/TextToImage?color=cyan&text=This+is+a+test.
Screen Shot 2016-05-17 at 12.59.35 AM

This way of manipulating text with SVG opens the door to a few ideas and allows interesting features that will be explored in the next articles… (hint:  check out the bar codes in the first image!)


Posted

in

by

Tags:

Comments

4 responses to “Unlock the Power of Color in Your Reports and Page Layouts”

  1. […] ← Custom formats in reports and page layouts […]

  2. […] explained in the first article of this series, this is a VisualForce page that uses the Scalable Vector Graphics format to specify an image […]

  3. Reddy avatar
    Reddy

    while creating Formula field… syntax error showing.
    Please give us step by step procedure to create this.
    Thank you.
    Reddy.

    1. Reddy avatar
      Reddy

      Hi Fernando,

      Sorry. I resolved the syntax error and its working fine.

      Thank you,
      Reddy.