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
venkatasubhashkvenkatasubhashk 

Map Parents to Children while creating Parents and Childs

Hi,

I created a vf page to insert Opportunities and Opportunityline items.

i am unable to Map the Right parent to right child.

my code is

 

Apex class

public class TVA_Billing_Items {

    public List<Opportunity> Opps  {get; set;}
    public List<OpportunitylineItem> Oppli  {get; set;}
   
    string id;
    decimal i =0; 
    decimal j = 0;
    
    public TVA_Billing_Items(ApexPages.StandardController myController) 
    {
    this.id = ApexPages.currentPage().getParameters().get('id');
     Opportunity o =[select id,name,Account.name,stagename,CloseDate,amount,probability,Header_Opportunity__c,Accountid,Opp_Line_Items_Count__c,Billing_Contact__c,No_Of_Months_for_Billing__c from Opportunity where id=:id];
     
     i = o.No_Of_Months_for_Billing__c;
     Opps = new List<Opportunity>();
     Oppli =new List<OpportunitylineItem>();
     
     for(i=0;i<o.No_Of_Months_for_Billing__c;i++){
     Opportunity LitOrd = new Opportunity();
      Opps.add(LitOrd);
      
       for(j=0;j<o.Opp_Line_Items_Count__c;j++){
          
             OpportunitylineItem LitOrdch = new OpportunitylineItem();
           
             Oppli.add(LitOrdch); 
          
        
        } 
       
       }
       }   
     

            
    public PageReference save() 
    {
    
     try{
     //Here I am Inserting the Opportunities first 
            insert Opps;
                    // for each Opp i am trying to insert OPplines but a//all lines are saved to first Opp  
             for(Opportunity Opps1 :Opps){
                       
             for(opportunityLineItem oppli1 :Oppli){
             Oppli1.OpportunityId = Opps1.id;
            
            }
            
            }
            
            insert Oppli;
        }    

      catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Oops An Error Occured!'));
      return null;
    } 
     
         return(new ApexPages.StandardController(Opps[0])).view();
       
 }
   
        }

VF page

 

<apex:page standardController="Opportunity" extensions="TVA_Billing_Itemssubhash"  >
<apex:form >
    
    <apex:pageMessages />
    <apex:pageBlock title="Add Billing Items" >
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}" rerender="error" />
            </apex:pageBlockButtons>
            
            
            
            <apex:pageBlockTable value="{!Opps}" var="a" id="table" >
            
               
                
                 <apex:column headerValue="Header Opportunity">
                   
                    <apex:inputField value="{!a.Header_Opportunity__c}" required="true"/>
                    
                    </apex:column> 
                     
                      <apex:column headerValue="Billing Opp Name">
                   
                    <apex:inputField value="{!a.name}" required="true"/>
                    
                    </apex:column>  
                      <apex:column headerValue="Stage">
                   
                    <apex:inputField value="{!a.stagename}" required="true"/>
                    
                    </apex:column> 
                     <apex:column headerValue="Account">
                   
                    <apex:inputField value="{!a.Accountid}" required="true"/>
                    
                    </apex:column> 
                     <apex:column headerValue="Close Date">
                   
                    <apex:inputField value="{!a.CloseDate}" required="true"/>
                    
                    </apex:column> 
                     <apex:column headerValue="Probability">
                   
                    <apex:inputField value="{!a.Probability}" required="true"/>
                    
                    </apex:column>  
                   
                    <apex:column headerValue="Amount">
                      <apex:inputField value="{!a.amount}" required="true"/>
                     
                    </apex:column>  
                     
                    
                     
                  <apex:column headerValue="Quick Save">
                     <apex:commandLink value="Quick save" action="{!qsave}"/>
                     
                    </apex:column>  
                     
               
                  </apex:pageBlockTable>
                  
                  <apex:pageBlockTable value="{!Oppli}" var="b" id="table2" >
            
               
                
              <apex:column headerValue="Unit Price">
                   
                    <apex:inputField value="{!b.UnitPrice}" required="true"/>
                    
                    </apex:column>  
                    
                     <apex:column headerValue="Quantity">
                   
                    <apex:inputField value="{!b.Quantity}" required="true"/>
                    
                    </apex:column>  
                    <apex:column headerValue="Opportunity">
                   
                    <apex:inputField value="{!b.Opportunityid}" required="true"/>
                    
                    </apex:column>  
                    
                 
                 
                  </apex:pageBlockTable>
               
           
    <apex:pageblockButtons location="bottom">
        <div style="text-align:right;margin-right:30px;font-weight:bold;">
        <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error" immediate="true" />
&nbsp;|&nbsp;&nbsp;
            <apex:commandLink value="Remove Row" action="{!removeRow}" rerender="table,error" immediate="true" />                         
        </div>
    </apex:pageblockButtons>  
    </apex:pageBlock>
    </apex:form>
</apex:page>

 any ideas to Map parent id to children