+ Start a Discussion
azar khasimazar khasim 

Trying to create bulk records in Child Object from Parent Object using Visualforce Page !

Hello Developers/Everyone,

Trying to create bulk records in Child Object from Parent Object using Visualforce Page .

Objects Names :
Opportunity  (Opportunity)
Product Schedule (Project_Schedule__c)
Schedule (Schedule__c)

Relation Between Them :
Master Detail
Opportunity to Schedule(MasterDetail)
Schedule to Product Schedule(Master Detail)

Actual Action to perform:

On Opportunity Record Page(Detail Page)
Create a custom Button. 
Custom Button contain Visualforce Page.
In that Visualforce Page we have fields of Schedule Object which is child of Opportunity.
User update Mandatory fields of Schedule Object, on that Visualforce Page and click on save at that time.
We need to create 10 or 12 or 20 records with the same data in all records.(Number of records will be dynamic)

Now Example : 
10 records have to get created on Schedule Object.
then at that time 
in Sub child Object(Product Schedule) their also 5 or 7 or 10 Records need to get create dynamically.

Final point is 
From Opportunity 
we need to create a record in child object as well as in super child(Child Child Object) Object.
These are to be bulk records and are dynamic in insertion.

***Please provide me a solution.
If any doubts please revert back.., 
I need this implementation and any suggestion will really helps me a lot.

Thanks and Regards,
Azar Khasim.
Marzorati ClaudioMarzorati Claudio
When "User update Mandatory fields of Schedule Object, on that Visualforce Page and click on save", use APEX to write backend logic to implement your business process.
I imagine that you could have some rules to create 5, 7 or 20 child records.
Ensure this rules maybe with the help of a custom metadata.

I don't know your business logic, but when you can do anything from standard, the backend is the solution.
azar khasimazar khasim
hello marzorati,

Thanks for the point you told.
Actually I have done 60% work.
Object A and Object B has Master Detail Relation.
Object B and Object C has Master Detail Relation

From Opportunity(Object A)
Create Bulk records in object B is done.

while records created on Object B at that time we need to create child records in Object C.
This functionality I have stucked.

Below are my codes.
help me from this point.

VF Page:
<apex:page StandardController="Opportunity" extensions="ScheduleDynamic" showHeader="false" sidebar="false" lightningStylesheets="{!$User.UIThemeDisplayed == 'Theme4d'}" >
  <!--     <img src="{!($Resource.RohmLogo)}" width="100%" height="100%" 
style="position: fixed;background-repeat:repeat;Right: 0cm; top:0cm; z-index:0"/>   -->
    <apex:form >
        <apex:pageBlock title="Schedule Bulk Record Creation">
            <apex:outputPanel >
                <apex:pageBlockSection collapsible="false" columns="1" > 
                    <apex:outputField style="color:black" label="Opportunity Name" value="{!s2.Opportunity__c}"/>   
                    <apex:inputField style="color:black" label="Scheduled Date" value="{!s2.Scheduled_Date__c}"/>
                    <apex:inputField style="color:black" label="Forecasted Set Quantity" value="{!s2.Forecasted_Set_Quantity__c}"/>
                    <apex:inputText label="NoofRecords" value="{!cr}" id="NOR"/>   
        <apex:pageBlock title="Bulk Product Schedule Create">
            <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
                <apex:column headerValue="Ident">
                    <apex:outputText value="{!wrapper.ident}"/>
                <apex:column headerValue="Schedule Number">
                    <apex:outputField value="{!wrapper.ps.Schedule_Number__c}"/>
                <apex:column headerValue="ROHM Part Number">
                    <apex:inputField value="{!wrapper.ps.ROHM_Part_Number__c}"/>
                <apex:column headerValue="Qty Set">
                    <apex:inputField value="{!wrapper.ps.Qty_Set__c}" />
                <apex:column headerValue="Action">
                    <apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable">
                        <apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/> 
            <apex:commandButton value="Add Row" action="{!addRows}" rerender="wtable">
                <apex:param name="addCount" value="1" assignTo="{!addCount}"/> 
            <apex:commandButton value="Add 2 Rows" action="{!addRows}" rerender="wtable">
                <apex:param name="addCount" value="2" assignTo="{!addCount}"/> 
            <apex:commandButton value="Add 5 Rows" action="{!addRows}" rerender="wtable">
                <apex:param name="addCount" value="5" assignTo="{!addCount}"/> 
                <apex:commandButton value="Save" action="{!save}"/>

My Vf Page View

Apex Controller:
public class ScheduleDynamic {
    public static integer cr{get;set;} // No of records to create in Schedule 2 Object
    public Schedule_2__c s2{get;set;} // Opportunity Child(Master-Detail Relation)
    public opportunity opp{get;set;} // Parent Object of Schedule Object.
    public Product_Schedule_2__c ps2{get;set;} // Schedule 2 Child(Master-Detail Relation)
    public List<pswrapper> wrappers {get; set;} // Product Schedule 2 Object Wrapper in List.
    public pswrapper[] wrapcons {get; set;} // Constructor for pswrapper.
    public static Integer toDelIdent {get; set;} // Delete Button in Row
    public static Integer addCount {get; set;} // Add Multiple Rows
    private Integer nextIdent=1; // Individual Auto Numbers 
    public string a{get;set;} // To capture ID of Opportunity Record(Dynamic)
    public string b{get;set;} // To capture ID of Schedule 2 Record(Dynamic)
    public OpportunityLineItem oppli {get;set;} // Lookup to Opportunity (OpportunityLineItem is Standard Salesforce Object)
    public ScheduleDynamic(apexpages.StandardController sc){
        s2 = new Schedule_2__c();
        s2.Opportunity__c = a;
        wrappers=new List<pswrapper>();
        for (Integer idx=0; idx<1; idx++) {
            wrappers.add(new pswrapper(nextIdent++));
    public void delWrapper() {
        Integer toDelPos=-1;
        for (Integer idx=0; idx<wrappers.size(); idx++) {
            if (wrappers[idx].ident==toDelIdent) {
        if (-1!=toDelPos) {
    public void addRows() {
        for (Integer idx=0; idx<addCount; idx++) {
            wrappers.add(new pswrapper(nextIdent++));
    public pagereference save(){
            // Schedule 2 Single Record 
            insert s2;

            // Bulk Records creation for Schedule 2 
            Integer Count;
            list<Schedule_2__c> BulkRecs2 = new list<Schedule_2__c>();
            Count = cr;
                for(integer i=1; i<= Count-1; i++){
                    Schedule_2__c s2RecordsBulk = new Schedule_2__c();
                    s2RecordsBulk.Opportunity__c = s2.Opportunity__c; 
                    s2RecordsBulk.Scheduled_Date__c = s2.Scheduled_Date__c.addmonths(i);
                    s2RecordsBulk.Forecasted_Set_Quantity__c = s2.Forecasted_Set_Quantity__c;
                insert BulkRecs2;
                system.debug('Output Rec ----'+BulkRecs2);
      /*      // Product Schedule 2 Records Save
          List<Product_Schedule_2__c> pss=new List<Product_Schedule_2__c>();
            wrapcons = new List<pswrapper>();
          //  ps.Schedule_Number__c = s2.Id;
         //   insert wrapcons;   
            for (pswrapper wrap : wrappers) {
            insert pss;  */
            // for single record creation product scheduled
         /*   Product_Schedule_2__c prdSchd = new Product_Schedule_2__c();
            prdSchd.Schedule_Number__c = s2.Id;
            insert prdSchd;  */
            //for bulk rec creation for product scheduled
            list<Product_Schedule_2__c> prdSchdList = new list<Product_Schedule_2__c>();
            for(Schedule_2__c schdObj: BulkRecs2){
                system.debug('$$Schedule newly created records :'+BulkRecs2);
                Product_Schedule_2__c prdSchd2 = new Product_Schedule_2__c();
                for(integer i=1; i<=addCount; i++){
                    prdSchd2.Schedule_Number__c = schdObj.Id;                  
                system.debug('## prdSchdList is'+prdSchdList);
            insert prdSchdList;
        }catch (Exception e) {
            ApexPages.addMessages (e);
        return new PageReference('/' + Schema.getGlobalDescribe().get('Schedule_2__c').getDescribe().getKeyPrefix() + '/o');
    public class pswrapper {
        public Product_Schedule_2__c ps {get;set;}
        public Integer ident {get;set;}
        public pswrapper(Integer inIdent) {
            ps=new Product_Schedule_2__c(Qty_Set__c=+ ident);

Thanks and Regards,
Azar Khasim.