1

I am looking for a way to conditionally render HTML based on the value of a field on an opportunity. I have taken multiple stabs at it but can't figure out where I'm going wrong. First I tried to work everything through one visualforce page using an IF statement, but then decided it may work better if I utilized components as well as a page.

Here's what I have, any guidance would be appreciated.

Page:

<apex:page standardController="Opportunity"

action="
  {!
  IF(!opportunity.Contact_Attempts__c > 3, 
       (<c:ViableOppComponent/>),
       (<c:DeadOppComponent/>)
    )
  }">
</apex:page>

Components: - both use same code, just different message

<apex:component >  

<style>
h1 {text-align:center;color:green;font-size: 20px;width:100%;margin:0 auto}
#container {width:100%;margin:0 auto;text-align:center;}
</style>

<div id="container">
    <br />
    <h1> This prospect is still viable. Continue outreach. </h1>
 </div>


</apex:component>
2

Add your components inside apex:outputPanel and use rendered attribute to display and hide

<apex:page standardController="Opportunity" >

    <apex:form>
        <apex:outputPanel rendered="{!(opportunity.Contact_Attempts__c > 3)}" >
            <c:ViableOppComponent/>
        </apex:outputPanel>
        <apex:outputPanel rendered="{!(opportunity.Contact_Attempts__c <= 3)}" >
            <c:DeadOppComponent/>
        </apex:outputPanel>
    </apex:form>

</apex:page>

As @sfdcfox suggested. Thanks for your suggestion.

<apex:page standardController="Opportunity" >

   <apex:form>
        <c:ViableOppComponent rendered="{!(opportunity.Contact_Attempts__c > 3)}"/>

        <c:DeadOppComponent rendered="{!(opportunity.Contact_Attempts__c <= 3)}"/>
   </apex:form>

</apex:page>
  • 2
    Actually, all components, even custom components, come with a built-in "rendered" property, so you don't necessarily need to wrap it in another element... – sfdcfox Apr 7 '16 at 16:07
  • @sfdcfox in component also we can add rendered property ? – Ratan Paul Apr 7 '16 at 16:09
  • 2
    Yes, we can write your example as: <c:ViableOppComponent rendered="{!(opportunity.Contact_Attempts__c > 3)}"/><c:DeadOppComponent rendered="{!(opportunity.Contact_Attempts__c <= 3)}"/> – sfdcfox Apr 7 '16 at 16:12
  • @sfdcfox yes exactly this is.. I wasn't aware of that, thanks for info.. – Ratan Paul Apr 7 '16 at 16:13

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.