+ Start a Discussion
Michael3.BrownMichael3.Brown 

Trouble displaying Fields From Different Objects on my VF Page

Hello,

 

I have a custom "Pipeline Tracker" object with a lookup relationship to the "Account" object. I am trying to run a test where I display all my records, pulling 1 field from the "Pipeline Tracker" object and 1 field from the "Account" object.

 

I am trying to do this by putting everything into a list, which I hope to call on my VF page. I haven't gotten any errors in building my list.

    List <Account> pipelineRecords = new List <Account> ();
    
    public void buildList()
    {
        for (Account acct: [SELECT Sales_Pole__c,  
            (SELECT Category__c FROM Pipeline_Tracker_3__r)
        FROM Account])
        {
            pipelineRecords.add(acct);
        }
    }

    public List<Account> getpipelineRecords()
    {
        return pipelineRecords;
    }

 

On my VF page, I have a simple test where I simply want to call every record from my list and display the "Sales Pole" field from the "Account" object and the "Category" field from the "Pipeline Tracker" object. 

 

Here is the code I have on my VF page.

<apex:page controller="MultipleDBJoin">
<TABLE>
<TH>Category</TH>
<TH>Sales Pole</TH>
  <apex:repeat value="{!pipelineRecords}" var="a">       
       <TR><TD width="75"><apex:outputText value="{!a.Category__c}" /></TD>
       <TD width="100"><apex:outputField value="{!a.Sales_Pole__c}" /></TD></TR>
  </apex:repeat>
</TABLE>
</apex:page>

 

I am getting an error saying that Category C is not found on the Account object. I think the problem lies in my List Creation where I specified my List as the Account type. But, it wouldn't let me create a List unless I established it with an object type, and if I set the List as the object type: Pipeline_Tracker_3__c, I would still get an error with Sales Pole.

 

Does anyone know how I can work around this issue? Can I create an SObject instead and iterate through that in order to display each record?

 

Thanks

Mike

aballardaballard

Seems like you should be able to reference the category field as

  {!a.pipeline_tracker_3__r.category__c}  

Shashikant SharmaShashikant Sharma

You should use a wrapper class :

 

    List <Account> pipelineRecords = new List <Account> ();
    
    List<wrapperClass> wrapperList = new List <wrapperClass> ();
    public void buildList()
    {
       wrapperList = new List<wrapperClass>(); 
       for (Account acct: [SELECT Sales_Pole__c,  
            (SELECT Category__c FROM Pipeline_Tracker_3__r)
        FROM Account])
        {
            // Pipeline_Tracker_3__c assuming this is object api name
            Pipeline_Tracker_3__c p = new Pipeline_Tracker_3__c();
            if(acct.Pipeline_Tracker_3__r.size() > 0)
                   p = acct.Pipeline_Tracker_3__r.get(0);

            wrapperList.add(new wrapperClass( acct , p));
        }
    }

    public List<wrapperClass> getWrapperList()
    {
        return wrapperList;
    }
    
   public class wrapperClass {
   Account a {get;set;}
   Pipeline_Tracker_3 p {get;set;}
   public wrapperClass(Account a , Pipeline_Tracker_3__c p)
   {
       this.a = a;
       this.p = p;
   }
   }

 VFP

 

<apex:page controller="MultipleDBJoin">
<TABLE>
<TH>Category</TH>
<TH>Sales Pole</TH>
  <apex:repeat value="{!wrapperList}" var="item">       
       <TR><TD width="75"><apex:outputText value="{!item.p.Category__c}" /></TD>
       <TD width="100"><apex:outputField value="{!item.a.Sales_Pole__c}" /></TD></TR>
  </apex:repeat>
</TABLE>
</apex:page>

 I hope above example helps you in this you will only be able to show only single record of  Pipeline_Tracker_3__c  object. If you want to show all then pass list of Pipeline_Tracker_3__c  and use a repeat in VFP.