+ Start a Discussion
charlie_vcharlie_v 

Get ID of current parent

Does anyone have a code sample of a controller that pulls in SOQL of a child object, where the child object's records are related to the ID of the parent object currently displayed on the page?

 

This is what I have, but it's not working:

 

public class HeadlineWithCommentsController { Headline__c[] headline = [SELECT Id, Name, Item__c, (SELECT Id, Name, Comment__c, Comment_Brief__c, Comment_By__c, Email__c, Post_to_Site__c FROM Headline_Comments__r WHERE Post_to_Site__c = True) FROM Headline__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')]; }

 

 

 

ShikibuShikibu
Using a constructor, something like this:

 

 

public class HeadlineWithCommentsController { Headline__c theHeadline; // Constructor public HeadlineWithCommentsController(ApexPages.StandardController stdController) { theHeadline = (Headline__c)stdController.getRecord(); theHeadline = [ SELECT Id, Name, Item__c, (SELECT Id, Name, Comment__c, Comment_Brief__c, Comment_By__c, Email__c, Post_to_Site__c FROM Headline_Comments__r WHERE Post_to_Site__c = True) FROM Headline__c WHERE Id = :theHeadline.Id][0]; } }

 

 

 

charlie_vcharlie_v

I'm getting an error when I try to use this in my VF page:

 

Error: Unknown constructor 'HeadlineWithCommentsController.HeadlineWithCommentsController()'

 

I used your code above:

 

public class HeadlineWithCommentsController { Headline__c theHeadline; public HeadlineWithCommentsController(ApexPages.StandardController stdController) { theHeadline = (Headline__c)stdController.getRecord(); theHeadline = [ SELECT Id, Name, Item__c, (SELECT Id, Name, Comment__c, Comment_Brief__c, Comment_By__c, Email__c, Post_to_Site__c FROM Headline_Comments__r WHERE Post_to_Site__c = True ORDER BY Name) FROM Headline__c WHERE Id = :theHeadline.Id][0]; } }