+ Start a Discussion
venkatasubhashkvenkatasubhashk 

Create Multiple Parents and Children and Map Parent id to Child

Is there a way i can create a Multiple Parents and childs at a time each corresponding to right parent..

Hi,

 

I want to create a custom visual force page to Create a Multiple Parent and Related Children, where on save i have to mach the parent id to corresponding children and save them too.

 

I Have code as Follows where on save all children are Mapped to First Parent Record....

 

 

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{
     
            insert Opps;
                      
             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();
       
 }
   
        }

 

kiranmutturukiranmutturu

if you have an identifier that can combine the parent to the respective child records then its not a big deal to work on this...

venkatasubhashkvenkatasubhashk

can you give me an example ...

kiranmutturukiranmutturu

u have a parent say x is there and there is an identifier like p1 to identify the parent and there are some childs to them like c1,c2.c3... for all these childs there must be a identifier as same as parent like p1 on the child object also.like this for p2 and c4,c5,c6..... and so on..

 

 

my idea is like ultimately i have to identify the repestive parent along with respective child records by using a comman identifier.....with out this we can't paired up the childs to the respective parent .. Hope you got it now

venkatasubhashkvenkatasubhashk

can we get this on save of parent and children ,or this is only after save in to Database

kiranmutturukiranmutturu

before saving the records u know what are all the parents and their respective records right? then u have to prepare the identifier in a way to find the right set of combinations using javascript or in the controller....its depends on the idea that u can ...

venkatasubhashkvenkatasubhashk

ok I will Try....if you can show me an example or change my code it would be helpful to continue..... 

kiranmutturukiranmutturu

sorry i dont have any sample code.. i am just throwing my thoughts

r1985r1985

Is the number of line items and opportunity in both lists are same so that if they are 10 opportunities and 10 line items each will have one to one relation?

venkatasubhashkvenkatasubhashk

No 1 Opp may have 3  lines , but all other Opp will have 3 lines