I have a list of VisualForce components called VFComponent, a list of attributes name VFAttributes and a junction object VFComponentToAttribute that links them in a many to many relationship.

I am trying to get a list of components, in the form of:

List<VFComponent__c> VList = [Query here FROM VFComponentToAttribute];

For the query I have tried getting Id, VFComponent__r.Id, VFComponent all to no avail. Everything I try seems to give me a type error.

Could someone be kind enough to explain how I could get a list,populated with child objects from the junction object in this way.

To clarify I am trying to pass a list of objects to some other code, but in general I would like to better understand what Salesforce actually stores in the junction object, I thought is was something like a key, but if I query the Id and try to use that I get type mismatches as well:

List<Id> idList = [Select childobject__r.id From JuncitonObject];

That gives me also gives an error as if the query result were objects and not IDs, which I suppose they are?

In short I am confused.

  • 1
    VFComponentToAttribute isn't the Parent. Instead, it's really a child of both the other 2 objects. It has a look-up to each of the parents. When you run a query, you're essentially searching for the intersection of those lookup records when you query VFComponentToAttribute. All that object really contains is links to the other objects.
    – crmprogdev
    Apr 18, 2016 at 16:31
  • Thanks, so how can I use that link to make a list of the other object, or IDs I can use to query a list of the other object? I know the information is there I just can't figure out the syntax.
    – ScottH7a9
    Apr 18, 2016 at 16:45

2 Answers 2


Okay, this was due to my own ignorance (well I knew that going in or I would not have posted here).

Anyhow, the problem is that any query on an object returns a list of that object. You then have to get the list of identifiers into a separate list using a loop.

So for instance the following works to run another class on the VFComponent object using a list of Ids for VFComponent from the VFComponentToAttribute object:

String compname = '%apex:pageBlock%';
List<Id> idList = new List<Id>();
List<VFComponentToAttributes__c> complist = [SELECT Id, Name, VFComponent__r.Id FROM VFComponentToAttributes__c Where VFComponent__r.Name Like :compname];                 
for(VFComponentToAttributes__c c: complist){  
    System.debug('c.name= ' + c.name + ', c.Id= ' + c.Id);

But if anyone else would like to suggest a more elegant solution I would be glad to hear it.

Also is there any way to find the true structure of the underlying database that Salesforce uses? I think this would be a lot less confusing if I knew what the code actually mapped to.

  • You want to get VFComponent__c, which is presented in VFComponentToAttributes__c List<VFComponent__c> complist = [SELECT id from VFComponent__c where name like '%apex:pageBlock%' and id in ( select VFComponent__c from VFComponentToAttributes__c)]; Apr 18, 2016 at 18:34

There are few standard junction objects in SF. Let's check Quote

It has accountid and OpportunityId, we can get opp name if we know account id

Get Opportunity Ids:

select OpportunityId from quote where accountid = '00155000005BXMYAA4'

Then get Opportunity Name:

select name from Opportunity where id in (select OpportunityId from quote where accountid = '00155000005BXMYAA4')

You could go to schema and check your objects, or you can go to 'Setup - Create - Object' and see for relashionships

Select account id for opportunity name:

select accountid from Quote where opportunityid in (select id from opportunity where name like '%BigData%') 
  • No, that does not work for my custom object. I can for instance do this: List<VFComponent__c> complist = [SELECT Id, Name FROM VFComponent__c] I cannot do any variation of this: List<VFComponent__c> complist = [SELECT VFComponent__r.Id FROM VFComponentToAttributes__c]; I cannot do it with any field that exsits on that object, I have tried every combination. The query is always treated as type List<VFComponentToAttribute> I always get a type error no matter what type of list I try to write it to.
    – ScottH7a9
    Apr 18, 2016 at 17:17
  • if you are selecting from VFComponentToAttributes, you will have list<VFComponentToAttributes> Apr 18, 2016 at 17:50
  • do you have VFComponent__c field on VFComponentToAttributes__c? try: List<VFComponent__c> complist = [SELECT from VFComponent__c where id in ( select VFComponent__c from VFComponentToAttributes__c)]; Apr 18, 2016 at 17:51

You must log in to answer this question.

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