I am trying to display data in a VF table that is stored in a custom object named medication. The medication object is a related to a Case. When I go to a case detail page I want the use to be able to click a custom button that launches the VF page. The table should contain the contents of the custom medication object. I have made several unsuccessful attempts to create the VF page and the custom controller.

Here is my attempt at the controller:

public with sharing class DrugTracker {
    public Medication__c med{get;set;}

    public DrugTracker(){

        med = [select Test__c from Medication__c where Test__c = 'Test1'];
  • Hi @CodeYankee, welcome to SFSE. Please take a moment to visit the help center and read How to Ask. You will need to edit your question with more detail if you wish to get useful answers. Please clarify the nature of the relationship between these objects. Which one is the parent? Also please share any markup you have attempted.
    – Adrian Larson
    Feb 20, 2017 at 0:48
  • The custom object is Medication and contents only two fields - The first is a lookup to the Case object and the second is a lookup to the Product object
    – CodeYankee
    Feb 20, 2017 at 0:50

1 Answer 1


If you just want to see the related child records, you don't even need Apex.

<apex:page standardController="Case">
    <apex:relatedList list="Medications__r" />

Taking a guess at API Names, it is common to name the child related list as plural, and any custom relationship will end in __r. Note that you can get the verbatim name using this script in Execute Anonymous:

for (ChildRelationship relation : SObjectType.Case.getChildRelationships())
    if (relation.getChildSObject() == Medication__c.sObjectType)

It is critical that you include this standardController attribute, if you want to adopt a standard approach to adding the page as a button on the Case object. Once you have a page implementing this controller, you can surface it by defining a Custom Button which uses it or embedding it into the Page Layout.

If you do want to write some Apex logic, the code you write will be an extension, not a controller. You should pause and take a moment to read Difference between controller and extensions. Read it in its entirety.

Anyway, if you want to write an extension, you need to accept the given controller. In this case, if you only want to access child records, you can just call getId.

public List<Medication__c> medications { get; private set; }

// constructor
public MyExtension(ApexPages.StandardController controller)
    medications = [SELECT ... FROM Medication__c WHERE Case__c = :controller.getId()];

You must log in to answer this question.

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