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
Josip KrajnovićJosip Krajnović 

Writing a Custom Controller extension to get related records and iterate over list/index and use with apex:repeat

I have 3 custom objects with a Master-Detail Relationship and Lookup Relationship.
CustomA__c (related CustomB__c) <-> CustomB__c <-> CustomC_c (related CustomB_cc)

I´ve built a Visualforce page with HTML table to replicate a PDF document and a Custom Controller Extension, so far so good.
But I´m just a beginner with apex coding.

The problem is that I need to replicate the document as it is, when there are no related records for CustomA__c or less then 5, it should still show the full table (the empty rows). Max. rows/related records on the document is 5, no second page needed.

Currently I´m trying to accomplisch that by using apex:variable and apex:repeat as I´ve seen some examples, but perhaps there is also another solution. For the Visualforce page I already wrote the code for the rows with data and another apeax:repeat for the empty rows.

But I´m really strugling with the controller, i know i need to iterate over the list, the code that i already wrote is also put together out of examples as i just don´t understand it yet good enough.

Any help would be appreciated!  Thanks, Josip
 
public with sharing class CustomAController {    
    
    public CustomA__c customa{get; set;}
    public CustomA__c ca{get; set;}
    
    public CustomAController (ApexPages.StandardController controller) {
        ca = (CustomA__c )controller.getRecord();
        customa= [SELECT Id, Name  FROM CustomA__c WHERE Id = :ApexPages.currentPage().getParameters().get('Id')];
    }
    
        public List<CustomB__c > getrelatedCustomB() {
        List <CustomB__c > cbList = New List<CustomB__c >(5);
        
        for(CustomA__c acc:[SELECT Id, Name, (SELECT Id, Name, ... , CustomCfield__r.Name FROM CustomBs__r ORDER BY Name LIMIT 5) FROM CustomA__c WHERE Id = :customa.Id]){
            for(CustomB__c cb:acc.CustomBs__r)
                cbList.add(cb);
        }
        
        return cbList;
        }
        
}

 
Deepali KulshresthaDeepali Kulshrestha
Hi Josip,

Please refer to the following link as they may be helpful in solving your problem:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_repeat.htm
https://salesforce.stackexchange.com/questions/540/apex-repeat-using-set-collection
https://developer.salesforce.com/forums/?id=906F000000096uwIAA
https://blog.jeffdouglas.com/2010/04/02/visualforce-row-counter-for-iteration-components/

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha