function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alex MakkAlex Makk 

Getting related records: Account -> Custom Object

Hi all! I'm really new to apex code, trying to figure out how to get related records from Account. Any help is appreciated!

VF page:
<apex:page Controller="UseofRepeatOnEpudsController">
<apex:form >
    <apex:pageBlock title="Accounts with assoicated Contacts">
        <apex:repeat value="{!epudsList }" var="epd">
            <apex:pageBlockSection title="{!epd.name} - {!epd.Account1__r.Name}">
                <apex:pageBlockTable value="{!epudsList}" var="epd2">
                    <apex:column value="{!epd2.Absorbent_drum_1__c}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:repeat>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class UseofRepeatOnEpudsController {
    public List<Logistic__c> epudsList{get;set;}
    public List<Account> AccountList{get;set;}
    public Id accId{get;set;}
    
    public UseofRepeatOnEpudsController() {
        accId = ApexPages.CurrentPage().getparameters().get('Id');
        epudsList = new List<Logistic__c>();
        epudsList = [SELECT id, Name ,  Account1__R.Name, Absorbent_drum_1__c From Logistic__c where Account1__c =:accId] ;
        accountList = [SELECT id, Name From Account];
    }

​Result:
I can't select VF page when setting up a button. My guess is that I need to use Standard controller="Account" and extention (My controller). I'm not sure how to set it correctly. Getting errors.

Also, epudsList is showing related custom object names which is fine, but PageBlockTable will show records not per custom object reocord - but all custom object records together.

So basically I'd like to press button on account and get related records separated. Accoont - > List of custom object records -> (Values related to one record)

Now its showing like: Account -> List of custom object records -> (Values related to multiple records) which is wrong.

Thanks for helping me out!
sandeep@Salesforcesandeep@Salesforce
Here is my response : 
Page: 
<apex:page standardController="Account" extensions="UseofRepeatOnEpudsController">
<apex:form >
    <apex:pageBlock title="Accounts with assoicated Contacts">        
            <apex:pageBlockSection title="{!AccountName}">
                <apex:pageBlockTable value="{!epudsList}" var="epd2">
                       <apex:column value="{!epd2.Name}"/>
                        <apex:column value="{!epd2.Absorbent_drum_1__c}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>        
    </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class UseofRepeatOnEpudsController {
    public List<Logistic__c> epudsList{get; set;}
    public String AccountName{get; set}
    public UseofRepeatOnEpudsController( ApexPages.StandardController cont) {
        accId = ApexPages.CurrentPage().getparameters().get('Id');
        List<Logistic__c> epudsList = [Select id, Name ,  Account1__r.Name, Absorbent_drum_1__c From Logistic__c where Account1__c =:accId]:
       if(epudsList .size()> 0) 
            AccountName = epudsList[0].Account1__r.Name;

    }
}


If this helped  you to resolve your problem then please mark this as best answer so that it can be useful for others. . 

Thanks
Sandeep Singhal​
Alex MakkAlex Makk
Thanks for reply, but controller has an error.     Error: Compile Error: unexpected token: '}' at line 3 column 38
Alex MakkAlex Makk
Here is how I got this to work:
 
public class UseofRepeatOnEpudsController {
    public List<Logistic__c> epudsList{get;set;}
    public List<Account> AccountList{get;set;}
    public String AccountName{get; set;}
    public Id accId{get;set;}
    
    public UseofRepeatOnEpudsController( ApexPages.StandardController Account) {
        accId = ApexPages.CurrentPage().getparameters().get('Id');
        epudsList = new List<Logistic__c>();
        epudsList = [SELECT id, Name ,  Account1__R.Name, Absorbent_drum_1__c From Logistic__c where Account1__c =:accId] ;
        if(epudsList .size()> 0) 
            AccountName = epudsList[0].Account1__r.Name;
    }
}
But as you see on screenshot, 111 is on every table and many empty rows (empty rows are empty records related to account). 111 should be related to only one related record. 

User-added image
Nithesh NNithesh N
Hi ryan,

Change the Line 3 of the Controller Code  

From
public String AccountName{get; set}
To
public String AccountName{get; set;}

Hope it Helps....

Best,
Nithesh
Alex MakkAlex Makk
Hi Nithesh, pls refer to my previous answer. That part has been corected. Thanks for helping me out anyways!
Nithesh NNithesh N

Hi Ryan, Is there any reason you are using "Repeat" tag in your visualforce?
I believe thats the reason 111 is appearing on every table. Basically, You are dispalying the same list of records repeatedly.

In a Nut shell, You are using same List (epudsList) in Both Repeat tag and Pageblock table tag. 

 

Alex MakkAlex Makk
Hi Nithesh, I was using repeat hoping to get all related records to Account. Not sure how do I set up separate list for record info.
Nithesh NNithesh N

Okay, lets follow this approach. 

Lets Say your Account Object has three related objects ( Object A, Object B, Object C).

So, Your Account ( ABC Inc.) will have related records from these three objects. From what i have understood. You want to display the records in following way...

ABC inc...

Object A

Related Record 1
Related Record 2
Related Record 3

 

Object B

Related Record 1
Related Record 2
Related Record 3

 

Object C

Related Record 1
Related Record 2
Related Record 3

 

So, In Controller we should create three lists for each related Object. Like in the draft psuedo code below...

public class UseofRepeatOnEpudsController {
    public List<ObjectA__c> AList{get;set;}
    public List<ObjectB__c> BList{get;set;}
    public List<ObjectC__c> CList{get;set;}
    public String AccountName{get; set;}
    public Id accId{get;set;}
    
    public UseofRepeatOnEpudsController( ApexPages.StandardController Account) {
        accId = ApexPages.CurrentPage().getparameters().get('Id');
         AList = new List<ObjectA__c>();
         BList = new List<ObjectB__c>();
         CList = new List<ObjectC__c>();
        
       AList = [SELECT id, Name  From ObjectA__c where Account1__c =:accId] ;
  
       BList = [SELECT id, Name   From ObjectB__c where Account1__c =:accId] ;

       CList = [SELECT id, Name   From ObjectC__c where Account1__c =:accId] ;     
        
    
    }
}
 

and The Visualforce format should look like
 

<apex:page standardController="Account" extensions="UseofRepeatOnEpudsController">
<apex:form >
    <apex:pageBlock title="Related records of Account ">        
            <apex:pageBlockSection title="Object A List">
                <apex:pageBlockTable value="{!AList}" var="epd1">
                       <apex:column value="{!epd1.Name}"/>
                        <apex:column value="{!epd1.Id}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>  
            
            <apex:pageBlockSection title="Object B List">
                <apex:pageBlockTable value="{!BList}" var="epd2">
                       <apex:column value="{!epd2.Name}"/>
                        <apex:column value="{!epd2.Id}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>  

            <apex:pageBlockSection title="Object C List">
                <apex:pageBlockTable value="{!CList}" var="epd3">
                       <apex:column value="{!epd3.Name}"/>
                        <apex:column value="{!epd3.Id}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>  
      
    </apex:pageBlock>
    </apex:form>
</apex:page>
 

P.S. This is just a format, You need to make changes accordingly. 

Alex MakkAlex Makk
Hi Nithesh! Thanks for reply! The case is that account has just one related object. But multiple records (On related object). Every table (like on screenshot) should only show values for just one record. Now every table is showing values from every record because I'm using same list. I don't know how to properly specify another list..
Alex RanaAlex Rana
is this code help me to store my tracking record like for this website : https://itrackcourier.com/ (https://itrackcourier.com)