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
Ignacio de la Torre 3Ignacio de la Torre 3 

How to add child related object list to parent object with Visualforce Page ?

Hi all !

I have this two related object Cuadre__c and Pago__c being Cuadre the parent of Pago in a master detail relationship.

I have a requirement where the user needs to click a button from the Cuadre layout and a Visualforce page will launch showing all the related Pago__c to that Cuadre__c object.

Then if the user clicks and 'Add Line' button a new Pago__c record line appears so the user can create a new Pago__c record.

The user can the click a 'Save' button to save one or many created Pago__c records in the above "table" and insert those Pago__c records that should be related to the Cuadre__c object from where the page was launched.

This last part is not working .... when the user clicks the save button the Pago__c record is not created or related to the Cuadre__c record where the page was launched and I need help solving this.

This is my code:

VF page:
<apex:page standardController="Cuadre__c" extensions="insertParentChildCon" lightningStylesheets="true" showHeader="false" sidebar="false">
    
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockButtons location="top">
                <apex:commandButton action="{!Save}" value="Save"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Agregar pagos al cuadre">
                <apex:repeat value="{!pagoList}" var="pg" id="table">
                    <br></br>
                	<br></br>
                    <apex:inputField value="{!pg.Name}"/>
                    <apex:inputField value="{!pg.Tipo__c}"/>
                    <apex:inputField value="{!pg.Importe__c}"/>
                    <apex:inputField value="{!pg.FormaPago__c}"/>
                    <br></br>
                	<br></br>
            		<br></br>
                </apex:repeat> 
            </apex:pageBlockSection>
            <apex:commandButton value="Add Row" Action="{!AddRow}" immediate="true" id="button"/> 
            
        </apex:pageBlock>
    </apex:form>

</apex:page>

and this is the controller:
 
public class insertParentChildCon { 
    
    ApexPages.StandardController sc;
    Id cuId;   
    public List<Pago__c> pagoList { get; set; }
    
    public insertParentChildCon(ApexPages.StandardController sc){
        this.sc = sc;
        cuId = sc.getId();
        
        pagoList = [SELECT ID, Tipo__c, Name, FormaPago__c, Importe__c FROM Pago__c WHERE Cuadre__c =: cuId ];
    }
    
    
    public PageReference AddRow(){
        pagoList.add(new Pago__c(Cuadre__c = cuId));
        return null;
    } 
    
    public PageReference Save(List<Pago__c> pagoList){
        
        for(Pago__c pago : pagolist) {
            pago.Cuadre__c = cuId;
        }
        
        if(pagoList != null){ 
            upsert pagoList;
        }
        
        return null;
    } 
}

Please help me ;)
 
ShirishaShirisha (Salesforce Developers) 
Hi Ignacio,

Greetings!

Please refer the below thread for the sample code where you can get the list of the child records.

https://trailblazers.salesforce.com/answers?id=9063A000000iZgwQAE

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri