You need to sign in to do that
Don't have an account?
Cross Object Query to Apex:Output Field
Looking for general guidance with some visualforce development. I’d like to build a vf widget that will conditionally display contacts associated with an account within a case.
For example: If an account has 10 contacts only display the 5 contacts that have a contact.role__c of ''x'' within the case.
I’ve been playing with my apex:outputField hello world sample below and can bring in the account data - that's the easy part. Next steps is to get the contacts that are of interest which is the part that I am missing.
What’s the approach that would work to query the contacts (through the related account) and display within the case? A simple sample would be very helpful to get me going.
Thanks.
<apex:page StandardController="Case" showHeader="false" sidebar="false"> <style> .pbsection td{ background-color:#FFFFFF; color: #5a5a5a; padding-top: 0px; padding-bottom: 0px; } .apexp .individualPalette .accountBlock .bPageBlock .pbBody .pbSubsection .detailList .labelCol { padding-top: 2px; padding-bottom: 2px; } .apexp .individualPalette .caseBlock .bPageBlock .pbBody .pbSubsection .detailList .dataCol { padding-top: 2px; padding-bottom: 2px; } .apexp .individualPalette .caseBlock .bPageBlock .pbBody .pbSubheader { background-color:#FFFFFF; color: #5a5a5a; font-size: 14px; height: 0px; } .apexp .individualPalette .caseBlock .bPageBlock { background-color:#FFFFFF; border-top: 0px; border-bottom: 0px; } </style> <apex:form styleClass="pbsection"> <apex:pageBlock mode="view" > <apex:pageBlockSection columns="2" > <apex:outputField value="{!case.CaseNumber}"/> <apex:outputField value="{!case.account.name}"/> <apex:outputField value="{!case.account.rating}"/> <apex:outputField value="{!case.ClosedDate}"/> <apex:outputField value="{!case.CreatedDate}"/> </apex:pageBlockSection> <apex:pageBlockSection columns="1" showHeader="true"> <apex:outputField value="{!case.description}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
I think the simplest thing to do would be to use a controller extension, e.g.:
ImportantContacts is an Apex class:
Then, you can use contacts in any of a number of iteration components. The simplest, probably, is apex:repeat :
(There are various table and list components, if you want to do something fancier--see the developer's guide for details.)
Using your model, I created the hello word class below which compiles. However, the error message of:
"Content cannot be displayed: Invalid conversion from runtime type SOBJECT:Case to SOBJECT:Account" displays within the case layout visualforce component.
Based on the error message, it seems like it thinks it needs to render the result set in an account and not case. E.g. It doesn't know the account is related to a case.
I modified the class to the below but am getting a different error:
"Content cannot be displayed: SObject row was retrieved via SOQL without querying the requested field: Case.AccountId" displays within the case layout visualforce component.
Any additional thoughts/wisdom to share? Thanks!
Sorry, I'd missed that the standard controller was for case rather than account. The standard controller won't query the account ID from the case unless it's used somewhere on the page. There are a couple of ways to do this, but one (probably the easiest) is to do something that uses the account ID on the page in a trivial way, like the following:
Then your second bit of code should work.