I have a picklist field.

When the field value is x, the color should be yellow. And when its value is y its color should be orange.

How to change the color of the output text based on picklist values in Salesforce? How do I implement this?


You can use the CASE formula on the output text's style attribute, like the following:

          style="color: {!CASE(Account.AccountSource, 'Web', '#f0f', '#000')}">

This will turn the text pink when "Web" is selected as the Account Source.

Please remember to add a actionSupport tag to your picklist element so that the page can rerender when the option is changed, like this:

<apex:inputField id="field1" value="{!Account.AccountSource}">
    <apex:actionsupport event="onchange" rerender="mainform"/>
  • Why are you putting #000 there? it is appearing in black color and color is not changed.
    – SFDC
    Aug 17 '17 at 12:19
  • The first argument of the case formula is the field to be evaluated, followed by the value to be compared and the output to that specific value. The last argument (#000) is the default when the value does not match any of the previously specified values. Aug 17 '17 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.