function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anthony HuerterAnthony Huerter 

Visualforce Page block to appear when a field meets a certain criteria

I am trying to get a visualforce page to appear when a custom field meets a certain criteria.  Here is what I have now and this works but I need to only appear if:  Custom field is > 20 


<apex:page >
             <style> .bPageBlock { background-color: green !important; } </style>
                         <apex:form > <apex:pageblock title="testing"> <div>This is a green partner. </div>
                                   </apex:pageblock>                  
                          </apex:form>
</apex:page

I would also like the background color to change to yellow if custom field is between 11 and 19 and change to red if the custom field is <10 

This is my first time making a Visualfroce page so I am a little lost.  I have tried mulpile formulas but nothing seems to work.  Any help would be appreciated. 

Thanks
Best Answer chosen by Anthony Huerter
Dushyant SonwarDushyant Sonwar
Hi Anthony,
Use this
<style>
        .bPageBlock {
            background-color: {!If((object.yourcustomfield < 10 ),' red !important',If((object.yourcustomfield >= 11 && object.yourcustomfield <= 19),' yellow !important',' green !important'))};
        }
    </style>
<apex:pageblock title="testing" rendered="{!object.yourcustomfield!=null && object.yourcustomfield > 20}">
            <div>This is a green partner. </div>
        </apex:pageblock>
Or You can also use jquery and javascript to acheive your result as said by rahul

All Answers

Rahul Sangwan7341Rahul Sangwan7341
Hi you can do one thing, access the value of your field in javacript and assign it to a variable in javascript, and then based on value add dynamic style class to pageblock using jquery,

Please let me know if you are finding any difficulty.

Please choose this as Best Answer if it helps you to solve your problem. 
Dushyant SonwarDushyant Sonwar
Hi Anthony,
Use this
<style>
        .bPageBlock {
            background-color: {!If((object.yourcustomfield < 10 ),' red !important',If((object.yourcustomfield >= 11 && object.yourcustomfield <= 19),' yellow !important',' green !important'))};
        }
    </style>
<apex:pageblock title="testing" rendered="{!object.yourcustomfield!=null && object.yourcustomfield > 20}">
            <div>This is a green partner. </div>
        </apex:pageblock>
Or You can also use jquery and javascript to acheive your result as said by rahul
This was selected as the best answer
Anthony HuerterAnthony Huerter
So I made the change like Dushyant suggested.  

<apex:page > 
                            
       
<style>
        .bPageBlock {
            background-color: {!If((Account.New_Partner_Points__c < 10 ),' red !important',If((Account.New_Partner_Points__c >= 11 && Account.New_Partner_Points__c <= 19),' yellow !important',' green !important'))};
        }
    </style>
<apex:pageblock title="testing" rendered="{!Account.New_Partner_Points__c!=null && Account.New_Partner_Points__c > 20}">
            <div>This is a green partner. </div>
        </apex:pageblock>


</apex:page>

I am getting this erroe now. Error: Unknown property 'Account.New_Partner_Points__c' referenced in Backlgroundcolor2 
Rahul Sangwan7341Rahul Sangwan7341
Hi Anthony,

For accessing the field value in your VF page, you have to taken care of 2 things, that the field you are accessing to vf page of any object. That object should be public with getter and setter. And you can access the field in you page with this syntax {!Account.New_Partner_Points__c}.


Please choose this as Best Answer if it helps you to solve your problem. 
 
Anthony HuerterAnthony Huerter
How do you set up a  public "getter and setter"  I appreciate everyones help on this
Dushyant SonwarDushyant Sonwar
Anthony,
You Have Forgot to set
StandardController="Account"
in apex:page
Hope This Helps.
Rahul Sangwan7341Rahul Sangwan7341
Hi Anthony,

you have to add 
<apex:page StandardController="Account"> 
or if you are using custom controller than 

<apex:page Controller="ControllerName"> and in controller while declaring a variable do like this

public Account account{get;set;}

 
Please choose this as Best Answer if it helps you to solve your problem.