0

I have two objects - object A with fields "Name, Number and Quantity", And object B with fields "Amount, Order_c and Units_c". There is a lookup relationship between the two objects with field name Inventory__c.

My question is: can we bring those two object fields into a single page block in a visual force page? If so, how to write the SOQL query for getting all records?

1
  • Yes. This is why we are using controllers most of the time with a VF page. In which object your Inventory__c field sits on?
    – highfive
    Apr 8, 2014 at 5:01

2 Answers 2

1

You can make use of Relationship Queries which feel a bit strange if you ave a background in SQL.

In this case (and assuming Inventory__c is on ObjectB__c and the default names for the relationship ObjectBs has been used):

public ObjectA__c[] objectAs {
    get {
        if (objectAs == null) {
            objectAs = [
                    select Name, Number, Quantity,
                            (select Amount, Order__c, Units__c from ObjectBs__r)
                    from ObjectA__c
                    where ...
                    ];
        }
        return objectAs;
    }
    private set;
}

In your page block, each instance of the objectAscollection has a reference called ObjectBs__r which is a collection of ObjectB__c.

0

Look into wrapper classes.

public class vfpage{

   public objWrapper records(){
      get{
      //populate wrapper with data
      objWrapper o = New ObjWrapper(OBJADATA, OBJBDATA);
      return o;
      }

      private set;
   }

   Public class objWrapper{

     public objA {get;set;}
     public objB {get;set;}

     public objWrapper(ObjA a, ObjB b){
       objA = a;
       objB = b;
     }



  }

}

Then use {!records.XXXXX} as your variable.

Modification to this will need to be made but this will give you a start

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .