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
frasuyetfrasuyet 

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>
 

 

 

 

 

 

 

AvromAvrom

I think the simplest thing to do would be to use a controller extension, e.g.:

 

 

<apex:page StandardController="Case" extensions="ImportantContacts" showHeader="false" sidebar="false">

 

 

ImportantContacts is an Apex class:

 

 

public class with sharing ImportantContacts {
    public List<Contact> contacts {get; set;}
    
    public ImportantContacts(ApexPages.StandardController stdController) {
        Account parent = (Account) stdController.getRecord();
        ID parentId = parent.id;
        contacts = [select attributesYouWantToDisplay from Contact
            where account.id = :parentId and
            role = 'x'];
    }
}

 

 

Then, you can use contacts in any of a number of iteration components. The simplest, probably, is apex:repeat :

 

 

<apex:repeat value="{!contacts}" var="currContact" >
   <apex:outputField value="{!currContact.someFieldYouQueried}" />
</apex:repeat>

 

 

(There are various table and list components, if you want to do something fancier--see the developer's guide for details.)

 

 

frasuyetfrasuyet

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.


 

public with sharing class EEM_OnboardingContacts {
 public List<Contact> contacts {get; set;}
    
    public EEM_OnboardingContacts(ApexPages.StandardController stdController) {
        Account parent = (Account) stdController.getRecord();
        ID parentId = parent.id;
        contacts = [select AccountId,firstname 
        from Contact
        where AccountId = :parentId and role__c = 'advertising'];
    }
}

 

 

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.

 

public with sharing class EEM_OnboardingContacts {
 public List<Contact> contacts {get; set;}
    
    public EEM_OnboardingContacts(ApexPages.StandardController stdController) {
        Case parent = (Case) stdController.getRecord();
        ID parentId = parent.AccountId;
        contacts = [select AccountId,firstname from Contact
            where AccountId = :parentId and
            role__c = 'advertising'];
    }
}

 

Any additional thoughts/wisdom to share? Thanks!

 

AvromAvrom

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:

 

 

<apex:outputText rendered="{!case.accountid = ''}" value="" />

 

 

Then your second bit of code should work.