function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VivoVivo 

Error with related list

Hi,

 

Right now I have a Car__c custom object which has a many-to-many relationship with Feature__c, (the relationship is under an object named Car_Features__c).

 

In my code, on the visualforce side I have outputted {!Car__c.Features__r} which shows the correct related list under the car. (Shows the ids of the features in the car)

But when I do the same thing on the apex side and try to output it, the list is empty:

 

selectedCar is the Id parameter delivered from the visualforce side via commandlink.

 

CurrentCar = [SELECT Id,Name,Description__c FROM Car__c
WHERE Id = :selectedCar];

 

String msg = ' ';

 

List<Car_Features__c> CurrentFeats = CurrentCar.Features__r;

msg += CurrentFeats;

Return msg;

 

The msg should output the id's of the Car_Features__c in the related list, the same  way that the  {!Car__c.Features__r} call on the visualforce side does. 

However, the output shows msg to be empty, but printing out the Id of CurrentCar prints the correct car Id, so that is not the issue.

 

Any ideas?

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

CurrentCar.Features__r is essentially an array of Car_Features__c objects, but you have to populate that via the SOQL query - the platform won't fill out the object graph from the car record for you.

 

Thus you'll need something like

 

CurrentCar = [SELECT Id,Name,Description__c, (select id from Car_Features__r) FROM Car__c
WHERE Id = :selectedCar];