+ Start a Discussion
Charni WigginsCharni Wiggins 

Render visualforce page if condition is true

Hi all,

Is there anyway to not show a visualforce page on a record if a certain field (picklist) has a specific value? Not much exprience with VF and have come across 'Rendered' but this controls elements of the VF page as I understand, not the whole page? Much appreciate any help. 
Best Answer chosen by Charni Wiggins
Narender Singh(Nads)Narender Singh(Nads)
Hey Charni, 
Yes you can conditionally render an VF page as well.
Say, you have an account type record and you want to render your VF based on that account's rating field.
Then your VF page will look something like this:
<apex:page standardController="Account" rendered="{!if(account.rating=='Hot',true,false)}" >
   
    <!-- Your VF page Content -->
    
</apex:page>
This page will get rendered on your page layout when that account record's rating is hot.

Let me know if it helps.
Thanks.

All Answers

Narender Singh(Nads)Narender Singh(Nads)
Hey Charni, 
Yes you can conditionally render an VF page as well.
Say, you have an account type record and you want to render your VF based on that account's rating field.
Then your VF page will look something like this:
<apex:page standardController="Account" rendered="{!if(account.rating=='Hot',true,false)}" >
   
    <!-- Your VF page Content -->
    
</apex:page>
This page will get rendered on your page layout when that account record's rating is hot.

Let me know if it helps.
Thanks.
This was selected as the best answer
Charni WigginsCharni Wiggins
Narender Singh(Nads) thank you!! Didn't realise it would be as easy as that, awesome! :) 
Charni WigginsCharni Wiggins
Hi Narender,

I just tried to implement this but seem to be getting an error, it's quite strange because I am not using NOT() but the error reads:
Incorrect parameter type for function 'not()'. Expected Boolean, received Text

This is my apex:page line
<apex:page rendered="{!IF(CONTAINS(TEXT(!Contact.TR1__Candidate_Status__c), "Perm"),TRUE,FALSE)}" lightningStylesheets="true" showHeader="false" standardStylesheets="false" sidebar="false" standardController="Contact" docType="html-5.0" applyBodyTag="false" applyHtmlTag="false">
The field in the condition is a picklist, and I need it to work with contains as it should render for mulitple values... I tried ISPICKVAL at first but keep getting this error... 

Many thanks
Narender Singh(Nads)Narender Singh(Nads)
Hi Charni,
Try this:
rendered="{!IF(CONTAINS(TEXT(Contact.TR1__Candidate_Status__c), "Perm"),TRUE,FALSE)}"

The reason you are getting this error because you are using '!' twice.'!' Once used you can fetch any property from your conrtroller class in '{}' these braces.
For example:

{!c.name !c.id} Will give an error:
Correct version of this would be: {!c.name c.id}
Suppose in a value attribute seperated by a '-' then,
you can't do: value="{!c.name- !c.id}" (Will give an error)
correct way is: value="{!c.name}-{!c.id}"

I hope you understood what am trying to explain.
Let me know if you need any further clarifiction. will try my best to explain.
Thanks!
Charni WigginsCharni Wiggins
Hi Narender,

I see, it makes sense! Thank you for the explanation! :)

I used your formula above and now I am getting this error:
Incorrect parameter type for function 'TEXT()'. Expected Number, Date, DateTime, Picklist, received Text

 
Narender Singh(Nads)Narender Singh(Nads)
Hey Charni,

I see why you are getting that error.
Remove the TEXT() function and directly use contains function.
Like this:
rendered="{!IF(CONTAINS(Contact.TR1__Candidate_Status__c, "Perm"),TRUE,FALSE)}"

Thnaks!
Charni WigginsCharni Wiggins
Narender - you are aewsome, thank you so much! The error has gone! Can I just ask why I didn't need the TEXT()?
Narender Singh(Nads)Narender Singh(Nads)

Hey Charni,
The error says it all:
Incorrect parameter type for function 'TEXT()'. Expected Number, Date, DateTime, Picklist, received Text

Contact.TR1__Candidate_Status__c is straight away giving us a text string(How we know this? Because the error message says 'received Text').
And the construct of CONTAINS() function is: CONTAINS(text,text_to_compare).

Hence no need to use TEXT() function. :)