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?
Attribution to: user5161
Possible Suggestion/Solution #1
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
Attribution to: Eric
Possible Suggestion/Solution #2
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 objectAs
collection has a reference called ObjectBs__r
which is a collection of ObjectB__c
.
Attribution to: Keith C
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/32119