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
JoeyDJoeyD 

Single page Parent with multiple children - My sample code

Hi all,

 

Below is some code that I got to work with the help of everyone here on the forums as well as elsewhere on the internet, so I thought I would post it for everyone to view in case it might help someone else down the road who is looking for something similar.

 

What it does is allow you to enter a new record of a custom parent object (which, itself, has a master-detail to the Opportunity object) while also entering multiple records for the child object that will save under the parent.

 

When the "New" button is clicked from the related list of an Opportunity record, the Opportunity field is automatically populated.


Note: I have only used this class and VF page in the sandbox, as a learning experiment. Everything works as I would expect it to, however, I am very new to programming in general, so I would suggest creating your own test class, and testing this stuff out in your sandbox or developer edition first, if you want to use it.

 

Also, I don't expect this stuff to be perfect (or, for that matter, not broken), so I'm very open to suggestions on how to make this stuff cleaner or more efficient. 

 

I'll try to post some screen shots as well.

 

The VF page:

 

<apex:page StandardController="Custom_Parent__c" extensions="parentChild" id="thePage">
    <style>
        .container
            {
                margin-left:10%;
                border:1px;
            }
        .even
            {
                background-color:#eeeed1;
            }
        .odd
            {
            }
    </style>
    <apex:sectionHeader title="Parent and Child"/>
        <apex:Message />
    <apex:form >
        <apex:pageBlock id="pb" title="Parent Detail">
            <apex:pageBlockButtons >
                <apex:commandbutton value="Save" action="{!Save}"/>
                <apex:commandbutton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="2" showHeader="false">
                <apex:outputField value="{!parent.Opportunity__c}" id="getOppid"/>
                <apex:inputField value="{!parent.Field1__c}"/>
                <apex:inputField value="{!parent.Field2}"/>
                <apex:inputField value="{!parent.Field2__c}"/>
                <apex:inputField value="{!parent.Field3__c}"/>
                &nbsp;
                <apex:inputField value="{!parent.Field4__c}"/>
                &nbsp;
                <apex:inputField value="{!parent.Field5__c}"/>
                <br/>
                <br/>
                <br/>      
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Child" columns="1" collapsible="false"> 
                <apex:pageBlock id="pb1">
                    <apex:pageMessages />
                    <apex:outputPanel layout="block" styleClass="container"> 
                    <apex:pageBlockTable value="{!lstInner}" var="c" id="therepeat" style="width:460px;padding:4px;" rowClasses="even,odd">
                        <apex:column headerValue="Column 1 Header" style="border:1px solid #f3f3eb;">
                            <apex:inputField value="{!c.child.Col1__c}" style="width:3em;"/>
                        </apex:column>
                        <apex:column headerValue="Column 2 Header" style="border:1px solid #f3f3eb;">
                            <apex:inputField value="{!c.child.Col2__c}"/>
                        </apex:column>
                        <apex:column headerValue="Column 3 Header" style="border:1px solid #f3f3eb;">
                            <apex:inputField value="{!c.child.Col3__c}" style="width:2em;"/>
                        </apex:column>
                        <apex:column style="border:1px solid #f3f3eb;">
                            <apex:commandLink value="Remove" action="{!Del}" rerender="pb1">
                                <apex:param name="rowToBeDeleted" value="{!c.recCount}" assignTo="{!selectedRowIndex}"></apex:param>
                            </apex:commandLink>
                        </apex:column>
                        
                    </apex:pageBlockTable>
                    <apex:commandLink value="Add Row" action="{!Add}" rerender="pb1"/>
                </apex:outputPanel>
                </apex:pageBlock>
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="1" title="New Section Title" collapsible="false">
                <apex:inputField value="{!parent.Field6__c}" style="width:400px;"/>
            </apex:pageBlockSection>
        </apex:pageblock>
    </apex:form>
</apex:page>

 

 

The Apex class:

 

 

public class parentChild {

    public List<Custom_Child__c> children  = new List<Custom_Child__c>();
    public List<innerClass> lstInner {get;set;}
    public String selectedRowIndex {get;set;}  
    public Integer count = 1;
    Custom_Parent__c parent;
    
    public Custom_Parent__c getparent(){
        if(parent == null) parent = new Custom_Parent__c();
        return parent;
    }
    
    public String getOppid(){
        String q = ApexPages.currentPage().getParameters().get('OppId');
        return q;
    }

    public PageReference Save(){
        try{
            for(Integer j = 0;j<lstInner.size();j++){
                children.add(lstInner[j].child);
            } 
            insert parent;
            for (Custom_Child__c sku : children){
                sku.Custom_Parent__c = parent.id;
            }
            insert children;
        }
        catch(DmlException ex){
            ApexPages.addMessages(ex);
        } 
        PageReference pr = new PageReference('/'+parent.id);
        pr.setRedirect(True);
        return pr;
    }
        
    public void Add(){   
        count = count+1;
        addMore();      
    }

    public void addMore(){
        innerClass objInnerClass = new innerClass(count);
        lstInner.add(objInnerClass);    
        system.debug('lstInner---->'+lstInner);            
    }
    
    public void Del(){
        system.debug('selected row index---->'+selectedRowIndex);
        lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
        count = count - 1;  
    }

    public parentChild(ApexPages.StandardController ctlr){
        this.parent = (Custom_Parent__c)ctlr.getRecord();
        lstInner = new List<innerClass>();
        addMore();
        selectedRowIndex = '0';   
    }

    public class innerClass {       
        
        public String recCount {get;set;}
        public Custom_Child__c child {get;set;}
        
        public innerClass(Integer intCount){
            recCount = String.valueOf(intCount);        
            child = new Custom_Child__c();
        }  
    }
}

 

 

I've tried to replace all of my actualy object and field names with generic ones, so I apologized if I missed something.

Anyhow, let me know what you think! And hopefully this helps someone. :)

 

 

JoeyDJoeyD