+ Start a Discussion
bryan.revelant1.36932878080717bryan.revelant1.36932878080717 

Visual Force Page Example of three tables

I understand that this is a pretty general Question however I have been trying understand how to build Visual Force pages from scratch. I am running into issues when trying to display the data model/objects when creating a Visual Force Page.

 

I can have one object display data. However when I add one or more tables to the VF i get errors. 

 

Would it be possible for anyone to forward me an example of a VF, Controller and class of how they display three objects on a VF. Basic would be perfect 

 

Best regards, 

sandeep@Salesforcesandeep@Salesforce

As this is your first query so I am providing you some code understanding because it is very necessary for you to get some wide knowledge of VF and controller side.

 

As per your query if i have threee obj A, B & C and need to show Data of these three objects on VF Page in single table then follow below code

Class :

 

Public class threeObjDataTable{

 

Public threeObjDataTable()

{

for(A__C a : [select id from A__c])

{

for(B__C b : [select id from B__c])

{

  for(C__c c: [select id from C__c])

   {

         WrapList.add(new WrapClass(a, b, c))

   }

 

}

 

}

 

 

}

 

 

Public List<wrapClas> WrapList{get;set;}

 

Public Class wrapClas{

public A__C a{get;set;}

public B__C b{get;set;}

public C__C c{get;set;}

 

Public wrapClas(A__c  tempA ; B__c temp B__c , C__c tempC)

{

     a = tempA ; 

     b = tempB; 

     c = tempC; 

}

 

}

 

}

 

 

Page : 

 

<apex:Page>

<Apex:form>

<apex:pageBlock>

    <apex:PageBlockTable value="{!WrapList}"  var="item" >

       <apex:column value="{item.a.ID">

       <apex:column value="{item.b.ID">

       <apex:column value="{item.c.ID">

   </Apex:PageBlockTable>

</apex:pageBlock>

</Apex:form>

</apex:page>

 

 

bryan.revelant1.36932878080717bryan.revelant1.36932878080717

It appears there is a sytax error with the controller.

 

 WrapList.add(new WrapClass(a, b, c))

 

Error: Compile Error: expecting a semi-colon, found '}' at line 23 column 3

 

Tried to place a semi colon

 

 

WrapList.add(new WrapClass(a, b, c));

 

Then it gives me an error of

 

Public wrapClas(A__c  tempA ; B__c temp B__c , C__c tempC)

 

Error: Compile Error: expecting a right parentheses, found ';' at line 57 column 28

sandeep@Salesforcesandeep@Salesforce

I have just given you an idea how to write a code..you need to rewrite this code as per your requirement . like you need to replace all objects with your APIs also .

bryan.revelant1.36932878080717bryan.revelant1.36932878080717
I figured as much... For fun and learning practice. I did however created 3 objects A B C. Obviously the VF I will build will have actual names with actual fields. I do also understand the principal of the SOQL.

The creating and sytax is my downfall. I came from a Microsoft platform.

With your example code however if I have 3 objects A B C with the standard columns the code should work correct?
Sri549Sri549
Public class threeObjDataTable{
 Public threeObjDataTable()
    {
    for(Account a : [select id from Account ])
     {
        for(Contact b : [select id from Contact])
          {
                for(Opportunity c : [select id from Opportunity ])
                {
                   WrapList.add(new WrapClas(a, b, c));
                }
          }
     }
    }
    Public List<wrapClas> WrapList{get;set;}
        Public Class wrapClas{
            public Account tempA{get;set;}
            public Contact tempB{get;set;}
            public Opportunity tempC{get;set;}
                Public wrapClas(Account  A , Contact  B, Opportunity  C)
                {
                A=a; 
                B=b; 
                C=c; 
                } 
                             }
                               }

 

Sri549Sri549

Try with this

you will not any error

Public class threeObjDataTable{
 Public threeObjDataTable()
    {
    for(Account a : [select id from Account ])
     {
        for(Contact b : [select id from Contact])
          {
                for(Opportunity c : [select id from Opportunity ])
                {
                   WrapList.add(new WrapClas(a, b, c));
                }
          }
     }
    }
    Public List<wrapClas> WrapList{get;set;}
        Public Class wrapClas{
            public Account tempA{get;set;}
            public Contact tempB{get;set;}
            public Opportunity tempC{get;set;}
                Public wrapClas(Account  A , Contact  B, Opportunity  C)
                {
                A=a;
                B=b;
                C=c;
                }
                             }
                               }

 

Thanks&Regards

Srinivas

bryan.revelant1.36932878080717bryan.revelant1.36932878080717

Last dumb basic level question.

 

Displaying the data on the VF.

 

So if I wanted to display the data on object A and then have a input  object B. Such as a question for A and an answer for B how would that be written. I see that there are various ways to display the data in apex.

 

Optional Question:

 

I have noticed when I do create the aformetioned, when I hit the save button the input data records still show. I am expecting that when the users hit the save button the input data will clear.

 

 

 

<apex:page controller="threeObjDataTable" showheader="false" title=""> <Apex:form>

<apex:pageBlock>

    <apex:PageBlockTable value="{!WrapList}"  var="item" >

       <apex:column value="{item.a.ID">

       <apex:column value="{item.b.ID">

       <apex:column value="{item.c.ID">

   </Apex:PageBlockTable>

</apex:pageBlock>

</Apex:form>

</apex:page>

bryan.revelant1.36932878080717bryan.revelant1.36932878080717

Such as the below will give an error of:

 

System.NullPointerException: Attempt to de-reference a null object 

 

Class.threeObjDataTable.<init>: line 10, column 1

 

<apex:page controller="threeObjDataTable" showheader="false" title=""> <Apex:form >

<apex:pageBlock >

    <apex:PageBlockTable value="{!WrapList}"  var="item" >

       <apex:outputText value="{!Name"></apex:outputText>

         </Apex:PageBlockTable>

</apex:pageBlock>

</Apex:form>

</apex:page>

Sri549Sri549

Hi bryan

 

 <apex:outputText value="{!Name}"></apex:outputText>  this is the correct syntax

in page block table vlaue and var are mandatory attributes

so if you want to display of name of any object in table

<apex:column value="{!item.a.Name}" headerValue="Account Name"/>   

 Thanks

Srinivas

bryan.revelant1.36697709739127bryan.revelant1.36697709739127

When i changed the code to the below I get the following error.

 

Error: Unknown property 'threeObjDataTable.wrapClas.a'

 

 

 

 

<apex:page controller="threeObjDataTable" showheader="false" title=""> <Apex:form >

<apex:pageBlock >

    <apex:PageBlockTable value="{!WrapList}"  var="item" >

      <apex:column value="{!item.a.Name}" headerValue="Account Name"/>

         </Apex:PageBlockTable>

</apex:pageBlock>

</Apex:form>

</apex:page>

 

 

Public class threeObjDataTable{
 Public threeObjDataTable()
    {
    for(Account a : [select id from Account ])
     {
        for(Contact b : [select id from Contact])
          {
                for(Opportunity c : [select id from Opportunity ])
                {
                   WrapList.add(new WrapClas(a, b, c));
                }
          }
     }
    }
    Public List<wrapClas> WrapList{get;set;}
        Public Class wrapClas{
            public Account tempA{get;set;}
            public Contact tempB{get;set;}
            public Opportunity tempC{get;set;}
                Public wrapClas(Account  A , Contact  B, Opportunity  C)
                {
                A=a;
                B=b;
                C=c;
                }
                             }
                               }


 

dbalke123dbalke123

I also would like to pull data from three different objects into one table in a visualforce page. I copied and pasted the code sample above and configured it to our needs (I want to draw data from Accounts, Products, and a custom object called: Invoice History).

 

This is what I have below:

Error: Compile Error: Loop variable must be an SObject or list of Account at line 4 and column 17

 

Public class threeObjDataTable{
 Public threeObjDataTable()
    {
    for(Account a : [select id from Account])
     {
        for(Invoice_History__c b : [select id from Invoice_History__c])
          {
                for(Product2 c : [select id from Product2 ])
                {
                   WrapList.add(new WrapClas(a, b, c));
                }
          }
     }
    }
    Public List<wrapClas> WrapList{get;set;}
        Public Class wrapClas{
            public Account tempA{get;set;}
            public Invoice_History__c tempB{get;set;}
            public Product2 tempC{get;set;}
                Public wrapClas(Account  A , Invoice_History__c  B, Product2  C)
                {
                A=a;
                B=b;
                C=c;
                }
                             }
                               }

 

 

Even if I complete this apex class, I need to draw certain fields from these three objects into a data table. Will I be able to do this when I create a visualforce page using this apex class? I am a beginner developer and need help on this. Thanks!

bryan.revelant1.37773959363043bryan.revelant1.37773959363043

I dont think that you code will work based on your loops. Could you confirm the objects and the code that you are using.

 

Are you just looking to populate a visual force page with 3 diffrent objects. Is there a relationship between the objects that you want to display?

bryan.revelant1.37773959363043bryan.revelant1.37773959363043

Actully it would be more like something like this

 

 

public

withsharingclass AccountOppCon

{

   

public Map<Id, Contact> ValidContacts = new Map<Id, Contact>();

   

public List<Contact> TheRecordsIShow {get; set;}

   

public List<Opportunity> OppRecords {get; set;}

   

public List<Contact> contacts {get; set;} 

   

public List<Custom_Object__c> TheCObject {get; set;}

Public String AccountID = System.currentPagereference().getParameters().

get('id');

   

   

   

public AccountOppCon(ApexPages.StandardController controller)

    {

       

for(Contact con : [Select Id From Contact WHERE AccountID=:AccountID])

       

       

set<Id> TheKeys = ValidContacts.keyset();

     

      TheRecordsIShow =  [

Select Id, Name, AccountID, Title From Contact WHERE AccountID=:AccountID];

      OppRecords = [

Select Name, stageName, Amount From Opportunity where AccountID=:AccountID];

      TheCObject = [

Select Name from Custom_Object__c where Account__c=:AccountID]; 

    }

public

PageReference done() {

        PageReference nextPage =

new PageReference('/' + AccountID);

       

return nextPage;

}

public

PageReference AddContact()

{

 

 

    PageReference redirect =

new PageReference('/apex/InsertAccount');

 

   

// pass the selected asset ID to the new page

    redirect.getParameters().put(

'id',AccountID);

    redirect.setRedirect(

true);

 

   

return redirect;

}

}

dbalke123dbalke123

Ok so if I copy and paste your code and customize it to the specific objects that I need (instead of contacts and opportunities) this should work for the apex class to create a matrix table of the data. Ultimately, I need the table to look something along this format

 

http://salesforcevision.blogspot.com/2012/07/salesforcecom-visualforce-reports.html (specifically I need the dates at the top row and both columns underneath those dates for each month...if that makes sense)

 

For the visual force page...I don't think the previous example in the discussion would work either.

bryan.revelant1.37773959363043bryan.revelant1.37773959363043

O, your talking about reports rather than visual force pages correct?

dbalke123dbalke123
Well I want to create a customized matrix report, and I don't think I can
do that without having to rewrite the table in code since I want to add
more columns than are given in the salesforce standard application. So
ultimately, yes I want to create a report, but I just assumed I would have
to create a visualforce page as well to display the report that was written
in code. Does that make sense?

Dawn