+ Start a Discussion
Paul GintherPaul Ginther 

Show static message in VF page based on field value

Based on the value of a picklist field I need to render a static message in a small VF page at the top of the record in Classic UI.  I have created an Apex controller as well as the VF page, but I have not been able to make this work.  I am new to this level of development so any assistance you might be able to give would be greatly appreciated.  Basically, if a picklist field called Frequency contains one of three different values, I want to display a static text message.  Below is the code I have written so far:

Apex Class – “ContentMessageInlineCtrl”
public with sharing class ContentMessageInlineCtrl {
  public Content__c cov {get;set;}
 
  public ContentMessageInlineCtrl(ApexPages.StandardController controller) {
    if(!Test.isRunningTest()) {
      controller.addFields(new List<String>{'Frequency__c'});
    }
        cov = (Content__c) controller.getRecord();
    }
}
 
VisualForce Page = “ContentMessageInline”
<apex:page standardController="Content__c" extensions="ContentMessageInlineCtrl">
    <style type="text/css">
        .redAlert {
            font-weight: bold;
            color: #FF0000;
            font-size : 22px;
            text-decoration: none;
        }
       
    </style>
   
    <apex:form >
        <apex:outputPanel rendered="{if(!(Frequency__c == “Weekly”||Frequency__c == “Semi Monthly (Twice a month)”||
        Frequency__c == “Bi Weekly (Every Other week)”)true,false)}">          
            <center>
               <apex:outputText styleClass="redAlert" value="THIS IS WHERE THE STATIC MESSAGE WILL RESIDE"/> <br/>
            </center>
        </apex:outputPanel>
    </apex:form>
</apex:page>
 
Best Answer chosen by Paul Ginther
Rajneesh Ranjan 23Rajneesh Ranjan 23
Hi Paul,

I have made little changes in VF page and controller (how outputPanel renders and how controller can set dynamic values for you based on the picklist value) and it should work for you now.
 
<apex:page standardController="Content__c" extensions="ContentMessageInlineCtrl">
    <style type="text/css">
        .redAlert {
            font-weight: bold;
            color: #FF0000;
            font-size : 22px;
            text-decoration: none;
        }
       
    </style>
   
    <apex:form >
        <apex:outputPanel rendered="{!blnDislayMsg}" >          
            <center>
               <apex:outputText styleClass="redAlert" value="THIS IS WHERE THE STATIC MESSAGE WILL RESIDE"/><br/><BR/>
               <apex:outputText styleClass="redAlert" value="THIS IS {!strMsg} CONTENT!!"/><br/>
            </center>
        </apex:outputPanel>
    </apex:form>
    
</apex:page>
 
public with sharing class ContentMessageInlineCtrl {
  public Content__c cov {get;set;}
  public String strMsg {get;set;}
  public Boolean blnDislayMsg{get;set;}
 
  public ContentMessageInlineCtrl(ApexPages.StandardController controller) {
    if(!Test.isRunningTest()) {
      controller.addFields(new List<String>{'Name', 'Frequency__c'});
    }
        cov = (Content__c) controller.getRecord();
        
        if (cov.Frequency__c == 'Weekly' || cov.Frequency__c == 'Bi Weekly (Every Other week)' || cov.Frequency__c == 'Semi Monthly (Twice a month)') {
            blnDislayMsg = true;
            strMsg = cov.Frequency__c;
        }
    }
}

Thanks,
R

All Answers

Rajneesh Ranjan 23Rajneesh Ranjan 23
Hi Paul,

I have made little changes in VF page and controller (how outputPanel renders and how controller can set dynamic values for you based on the picklist value) and it should work for you now.
 
<apex:page standardController="Content__c" extensions="ContentMessageInlineCtrl">
    <style type="text/css">
        .redAlert {
            font-weight: bold;
            color: #FF0000;
            font-size : 22px;
            text-decoration: none;
        }
       
    </style>
   
    <apex:form >
        <apex:outputPanel rendered="{!blnDislayMsg}" >          
            <center>
               <apex:outputText styleClass="redAlert" value="THIS IS WHERE THE STATIC MESSAGE WILL RESIDE"/><br/><BR/>
               <apex:outputText styleClass="redAlert" value="THIS IS {!strMsg} CONTENT!!"/><br/>
            </center>
        </apex:outputPanel>
    </apex:form>
    
</apex:page>
 
public with sharing class ContentMessageInlineCtrl {
  public Content__c cov {get;set;}
  public String strMsg {get;set;}
  public Boolean blnDislayMsg{get;set;}
 
  public ContentMessageInlineCtrl(ApexPages.StandardController controller) {
    if(!Test.isRunningTest()) {
      controller.addFields(new List<String>{'Name', 'Frequency__c'});
    }
        cov = (Content__c) controller.getRecord();
        
        if (cov.Frequency__c == 'Weekly' || cov.Frequency__c == 'Bi Weekly (Every Other week)' || cov.Frequency__c == 'Semi Monthly (Twice a month)') {
            blnDislayMsg = true;
            strMsg = cov.Frequency__c;
        }
    }
}

Thanks,
R
This was selected as the best answer
Paul GintherPaul Ginther
Thank you Rajneesh!  That works perfectly.  Your assistance is greatly appreciated.