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" />
</apex:page>
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)
system.debug(relation.getRelationshipName());
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()];
}