0

I have this two custom objects called Cuadre__c and Pago__c, Pago__c being a child o cuadre (master-detail).

I have this requirement where I need to launch a Visualforce page from the Cuadre__c layout and load the information from all the related Pago__c's !!

Also, if the user clicks an 'add new line button', a new line of Pago__c should appear for the user to complete some fields from the Pago__c object.

And there should also be a Save button to save the new Pago__c the user might have generated if she or he clicked the 'add new line' button. This Pago__c should save to the realted Cuadre__c from where the Visualforce was called.

Ok, this is my logic so far:

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" >
                    <apex:inputField value="{!pg.Tipo__c}"/>
                    <apex:inputField value="{!pg.Importe__c}"/>
                    <apex:inputField value="{!pg.FormaPago__c}"/>
                </apex:repeat> 
            </apex:pageBlockSection>
            <apex:commandButton value="Add Row" Action="{!AddRow}" immediate="true" id="button"/> 
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:

public class insertParentChildCon { 
    
    ApexPages.StandardController sc;
    public List<Cuadre__c> cuadreList {get;set;}    
    Cuadre__c cuadre {get;set;}   
    public List<Pago__c> pagoList {get; set;}
    
    public insertParentChildCon(ApexPages.StandardController sc) {
        this.sc = sc;
        //cuadre.Id = apexpages.currentpage().getparameters().get('id');
        cuadre = new Cuadre__c();
        pagoList = new List<Pago__c>();
        AddRow();                                
    }
    
    
    public Cuadre__c getCuadre() {
        return cuadre;
    }
    
    public PageReference AddRow() {
        pagoList.add(new Pago__c());
        return null;
    } 
    
    public PageReference Save() {
        upsert cuadre;
        
        List<Pago__c> pagos = new List<Pago__c>();
        if(cuadre != null) {
            for(Pago__c pago : pagolist) {
                pago.Cuadre__c = cuadre.Id;
                pagos.add(pago);
            }
        }
        if(pagos != null) { 
            upsert pagoList;
        }
        return null;
    } 
}

This is generating this UI:

One row:

enter image description here

Multiple rows:

enter image description here

Problem is that the code is not bringing the related Pago__c childs from the Cuadre__c object where I'm clicking the button to launch the Visualforce and I can't figure out how to do it.

I've tried this line in the controller's contructor

//cuadre.Id = apexpages.currentpage().getparameters().get('id');

public insertParentChildCon(ApexPages.StandardController sc) {
        this.sc = sc;
        //cuadre.Id = apexpages.currentpage().getparameters().get('id');
        cuadre = new Cuadre__c();
        pagoList = new List<Pago__c>();
        AddRow();                                
    }

but not working ... how can I get the childs from the Cuadre__c object in my VF so I can edit the fields, save them, upsert them, etc etc ??

Thanks a lot for reading and helping

1 Answer 1

1

You are not getting any child records because you need to query them first. Query records in controller (add all missing fields) and you are good to go.

public insertParentChildCon(ApexPages.StandardController sc) {
        this.sc = sc;
        //cuadre.Id = apexpages.currentpage().getparameters().get('id');
        cuadre = new Cuadre__c();
        pagoList = new List<Pago__c>();
        Id cuId = sc.getId();
        pagoList = [SELECT ID FROM Pago__c WHERE Cuadre__c =: cuId ];
        AddRow();                                
    }

You must log in to answer this question.

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