Visualforce display a related list from a lookup field on a custom object

apexclasscustom-objectrelated-listvisualforce

I am quite new to salesforce/coding and got myself stuck on a stumbling block when developing object for our internal use.

I have and object called Inventory_Skid_c this object has a field called Skid Location that is a lookupfield to another object called WH_Bin.
So my issue is the following I want to display another object that is related to WH_Bin as a related list in this page this is object is named Stock_Levels__c

Under the WH_Bin I can see as a related list all the stock levels records and I want this information pertaining only this Bin to be show on the Inventory Skid object.

I tried to google and solve using only visual force but it seems impossible so I tried to do on the sandbox using a controller and visualforce but im pretty much stuck

Here is the code I have for the controller:

public class WarehouseBinChildController{
  list<WH_Warehouse_Bins__c> cons = [
    Select Id,
    Name,
    (SELECT Id FROM Stock_Levels__c)
    FROM WH_Warehouse_Bins__c

  ];
}

for the visual force I'm completely lost how to write the apex for it.
Stock level reference image

Below is the schema, sorry it took me some time to figure out how to get this view:
enter image description here

EDIT
Error I am getting with the code:
enter image description here

Best Answer

You can do this without any Apex controller just by exploiting the standardController for Inventory_Skid__c

<apex:page standardController="Inventory_Skid__c">

  <apex:relatedList 
     list="Stock_Levels__r"
     subject="{!Inventory_Skid__c.Skid_Location__c}"  
   />
</apex:page>

where

  • list is the API name of the related list from parent to child
  • subject evaluates to an ID field that is the parent of the related list

References