+ Start a Discussion
Michael3.BrownMichael3.Brown 

Can You Use Conditions in VF to Display Certain Fields?

I have a custom object in SalesForce called Pipeline_Tracker__c. One of the fields on this object is "Owner." On my VisualForce report, I have a table set up with <apex:repeat> tags to display all the records in my Pipeline Tracker object.  When it comes to the owner field, I want to be display it as long as there is a value. However, if no owner is listed, I want to instead display the Account owner from the related account object..

 

Is there a way to do something like this in VisualForce? Basically, "if Owner__c" is null, display Account__r.Owner"

 

Thanks,

Mike

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You can indeed do this.  There's a few ways, here's one that should do it.  Both fields are present, but only one is rendered:

 

<apex:column headerValue="Owner">
   <apex:outputField value="{!rec.Owner__c}" rendered="{!NOT(ISBLANK(rec.Owner__c))}"/>
   <apex:outputField value="{!rec.Account.Owner__c}" rendered="{!ISBLANK(rec.Owner__c)}"/>
</apex:column>

 

All Answers

bob_buzzardbob_buzzard

You can indeed do this.  There's a few ways, here's one that should do it.  Both fields are present, but only one is rendered:

 

<apex:column headerValue="Owner">
   <apex:outputField value="{!rec.Owner__c}" rendered="{!NOT(ISBLANK(rec.Owner__c))}"/>
   <apex:outputField value="{!rec.Account.Owner__c}" rendered="{!ISBLANK(rec.Owner__c)}"/>
</apex:column>

 

This was selected as the best answer
Shashikant SharmaShashikant Sharma

You can write it like this

 

<apex:repeat value="pipelinelist" var="obj">
<apex:column>
  <apex:outPutLabel value="{!IF(ISNULL(obj.OwnerId) ,  obj.Account__r.OwnerId , obj.OwnerId)}" rendered="></apex:outPutLabel>  
    </apex:outPutLabel>  
</apex:column>
</apex:repeat>

 

 

Or

<apex:repeat value="pipelinelist" var="obj">
<apex:column>
  <apex:outPutLabel value="{!obj.OwnerId}" rendered="{!NOT(ISNULL(obj.OwnerId))}" ></apex:outPutLabel>  
    <apex:outPutLabel value="{!obj.Account__r.Owner}" rendered="{!ISNULL(obj.OwnerId)}" ></apex:outPutLabel>  
</apex:column>
</apex:repeat>

 in this only one will be displayed

Michael3.BrownMichael3.Brown

Awesome. Thanks, Bob! I was unaware that's what the rendered attribute did. That definitely made things simple and solved my problem.

Shashikant SharmaShashikant Sharma

Cross post 

Michael3.BrownMichael3.Brown

Thanks Shashikant for replying as well. I had no idea you could use IF statements within VisualForce. I will definitely be able to try and apply that to other scenarios.

 

Thanks!

Mike

bob_buzzardbob_buzzard

I just assume now that you, me and Ankit will reply to every post :)

Shashikant SharmaShashikant Sharma

Yes seems we should schedule our timings now , otherwise many cross post are in waiting :)

Ankit AroraAnkit Arora

Haha, good one Bob. Will join you guys after 2-3 hours.

Need to go some where (arghhh).

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page