0

Here is what I am trying to do.

I have a visualforce page, I have information from the account object populating in a table.

I'd also like to populate information from my custom object to that visualforce page. How do I do that? I would be okay using another table, I'm not picky on how that information shows up just as long as it works.

My code is simple, I'm a beginner.

<apex:page standardController="Account" recordSetVar="amanda">
    <apex:pageBlock >
      <apex:pageBlockTable value="{!amanda}" var="acc">
       <apex:column value="{!acc.Name}"/>
      </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

The API name of the custom object would be Milestone__c, I would like to bring over name, Where_are_we__c, and Name__c

5

1 Answer 1

0

You'll need to use Apex or the API to get this data. Visualforce itself can't do what you're trying to do without some help.

<apex:page controller="MyPageController">
  <apex:pageBlock>
    <apex:pageBlockTable value="{!accounts}" var="accountRecord">
      <apex:column value="{!accountRecord.Name}" headerValue="Name" />
      <apex:column> 
        <apex:dataTable value="{!accountRecord.Milestones__r}" var="milestone">
          <apex:column value="{!milestone.Name}" headerValue="Name" />
          <apex:column value="{!milestone.Where_are_we__c}" headerValue="Where Are We?" />
          <apex:column value="{!milestone.Name__c}" headerValue="Custom Name" />
        </apex:dataTable>
      </apex:column>
    </apex:pageBlockTable>
  </apex:pageBlock>
</apex:page>

And for the Apex code:

public class MyPageController {
  public Account[] getAccounts() {
    return [
      SELECT Name, (
        SELECT Name, Where_Are_We__c, Name__c 
        FROM Milestones__r
      )
      FROM Account
    ];
  }
}

Note that there's a limit of 50,000 records unless you use the "readOnly" property on apex:page. You can also add database filters to selectively choose which records you want to show.

4
  • So you made an extension right? the top code goes onto the visual force and the bottom goes to the extension class? Commented Aug 8, 2023 at 17:03
  • @ArtimusFowl No, this is more straight-forward. Instead of a standard controller, we use a custom controller (controller instead of standardController). The first block is the Visualforce, the second is the Apex code. This code is meant to be illustrative, not prescriptive, though. I've left out things like error handling and security to keep it simple.
    – sfdcfox
    Commented Aug 8, 2023 at 17:37
  • so where does the apex code go? sorry, i'm trying to understand. i was told i need to make an extension and come up with a SOQL query. I do have a relationship between account and my custom object milestone__c Commented Aug 8, 2023 at 17:53
  • @ArtimusFowl you don't need an extension. This code should copy as-is with no modification. The Apex Code goes into an Apex Class, the Visualforce goes into a Visualforce Page. There is no extension here, as we are not using a standard controller on this example.
    – sfdcfox
    Commented Aug 8, 2023 at 23:16

You must log in to answer this question.

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