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
Yi TanYi Tan 

Display cross object query result on VF page

Hi All

I am a SF administrator and very new to Apex code and VF. I have some questions for my first apex class and VF. Hope you can help me! 

The objective: on case object, I would like to use Account Name value on case as foreign key to query our custom object Account Technical jurisdiciton (ATJ). The relationship between account and ATJ is one to many. There is no direct relationship between case and Account Technical Jurisdiction object. The VF page should be display some fields value from ATJ when user is viewing a case record. 
But when I put my code in, it says " content cannot be displayed: attempt to de-reference a null object", can any one take a look at the codes below and guide me through?  Thank you! 
Apex Code: public with sharing class IGTCaseConJurExtension {
    public Case cs {get;set;}
    public List < IGT_AccountTechnicalJurisdiction__c> Records {get; set;}
    Public IGTCaseConJurExtension (){    Records = [SELECT Account__c,Id,IGT_Technical_Jurisdiction__c,Jurisdiction_Name__c FROM IGT_AccountTechnicalJurisdiction__c WHERE Account__c =: cs.accountId];
    }
}

VF page: 
<apex:page controller="IGTCaseConJurExtension">
<apex:pageBlock title="Account Technical Jurisdiction">
<apex:pageBlockTable value="{!Records}" var="Record"> 
            <apex:column > 
                <apex:facet name="header">Account Name</apex:facet> 
                <apex:outputText value="{!Record.Account__c}"/> 
            </apex:column> 
            <apex:column > 
                <apex:facet name="header">IGT Technical Jurisdiction</apex:facet> 
                <apex:outputText value="{!Record.IGT_Technical_Jurisdiction__c}"/> 
            </apex:column> 
            <apex:column > 
                <apex:facet name="header">Jurisdiction Name</apex:facet> 
                <apex:outputText value="{!Record.Jurisdiction_Name__c}"/> 
            </apex:column> 
        </apex:pageBlockTable> 
</apex:pageBlock>
</apex:page>
SlashApex (Luis Luciani)SlashApex (Luis Luciani)
Hi Yi,

If you would like to display it on the Case detail page then you need to make sure the you are using a standardController with and Extension.
Here is the code:

Apex Code:
 
public with sharing class IGTCaseConJurExtension 
{
    public Case cs {get;set;}
    public List < IGT_AccountTechnicalJurisdiction__c> Records {get; set;}   
    
    public IGTCaseConJurExtension(ApexPages.StandardController cont)
    {
        cs = (Case)cont.getRecord();
        Records = [SELECT Account__c,Id,IGT_Technical_Jurisdiction__c,Jurisdiction_Name__c FROM IGT_AccountTechnicalJurisdiction__c WHERE Account__c =: cs.AccountId];
    }
}

VF Page:
 
<apex:page standardcontroller="Case" extensions="IGTCaseConJurExtension">
<apex:pageBlock title="Account Technical Jurisdiction">
<apex:pageBlockTable value="{!Records}" var="Record"> 
            <apex:column > 
                <apex:facet name="header">Account Name</apex:facet> 
                <apex:outputText value="{!Record.Account__c}"/> 
            </apex:column> 
            <apex:column > 
                <apex:facet name="header">IGT Technical Jurisdiction</apex:facet> 
                <apex:outputText value="{!Record.IGT_Technical_Jurisdiction__c}"/> 
            </apex:column> 
            <apex:column > 
                <apex:facet name="header">Jurisdiction Name</apex:facet> 
                <apex:outputText value="{!Record.Jurisdiction_Name__c}"/> 
            </apex:column> 
        </apex:pageBlockTable> 
</apex:pageBlock>
</apex:page>

You will have to add the VF page to the Case page layout in order for it to show for users viewing the case.

Good luck!
Yi TanYi Tan
Hi Luis

I really appreciate for your help! 
I copied the code into my org, but it poped an error " [Error] Error: Invalid field Account__c for SObject Case"
Since ATJ has no relationship with Case, those three fields ( account__c, IGT_Technical_Jurisdiction__c, Jurisdiction_Name__c) are not on case. How can I fix this? thank you! 
SlashApex (Luis Luciani)SlashApex (Luis Luciani)
Hi Yi,

The is querying the ATJ object, not the case. When the case page loads, the code gets the AcocuntId from the case, and then uses that account id to get the related ATJ objects. Isn't that what you need?

Are you sure you copied my code completely? Did you make any changes to the VF PAge? I don't see why it would fail with that error, since we are not referencing the Account__c field on the case, we are doing so on the ATJ objects.
Yi TanYi Tan
Hi Luis

I double checked it. They are exactly the same. :( I don't know why it was failed. I attached the screenshots here, hope they are big enough for you to see them.
User-added image
User-added image
Yi TanYi Tan
I fixed this problem. with standard controller constructor, you can only display case related fields. If need to display fields from object which is not related to case, a custom method need to be created. Thank you everyone for your help. 

here is the controller extension:
public with sharing class IGTCaseConJurExtension {

public Case cs; 

public List <IGT_AccountTechnicalJurisdiction__c> atjList {get;set;} 

    public IGTCaseConJurExtension(ApexPages.StandardController cont) {
   this.cs = (case) cont.getRecord(); 
    system.debug ( 'case Id' + cs.Id);
   cs = [SELECT AccountId, CaseNumber from Case where Case.Id =:cs.Id LIMIT 1];
    
    system.debug ('case name' + cs.CaseNumber);
    system.debug ('case account' +cs.accountId);
     }
     
    public List<IGT_AccountTechnicalJurisdiction__c> getatjLists() {
     // use account Id to query a list of account technical jurisdictions
     atjList = [SELECT Name,Account__c, IGT_Technical_Jurisdiction__c,Jurisdiction_Name__c from IGT_AccountTechnicalJurisdiction__c where Account__c =: cs.accountId];
     return atjList;
    
         }
       
    }

Here is the VF page:
<apex:page standardcontroller="Case" extensions="IGTCaseConJurExtension">
<apex:pageBlock title="Account Technical Jurisdiction">
  <apex:pageBlockTable var="ls" value="{!atjLists}">
   <apex:column value="{!ls.Account__c}"/>
   <apex:column value="{!ls.Name}"/>
   <apex:column value="{!ls.IGT_Technical_Jurisdiction__c}"/>
   <apex:column value="{!ls.Jurisdiction_Name__c}"/>
  </apex:pageBlockTable>

</apex:pageBlock>
</apex:page>