You need to sign in to do that
Don't have an account?
venkatasubhashk
Create Multiple Parents and Children and Map Parent id to Child
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....
Is there a way i can create a Multiple Parents and childs at a time each corresponding to right parent..
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]; OpportunitylineItem[] oli = [select id,Quantity,UnitPrice,Opportunityid,Quote_Original_List_Price__c from OpportunityLineItem where Opportunityid=:o.id] ; Pricebookentry p = [select id,name from Pricebookentry where PricebookEntry.IsActive = true limit 1 ]; 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 = [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]; // LitOrd.Billing_Contact__c = o.Billing_Contact__c; Opps.add(LitOrd); for(j=0;j<o.Opp_Line_Items_Count__c;j++){ // for(OpportunitylineItem oli1:oli){ OpportunitylineItem LitOrdch = new OpportunitylineItem(); //Oppli =[select id,Quantity,UnitPrice,Opportunityid,Quote_Original_List_Price__c from OpportunityLineItem where Opportunityid=:LitOrd.id limit 1]; // LitOrdch.Quote_Original_List_Price__c = oli1.Quote_Original_List_Price__c ; //LitOrdch.opportunityid = LitOrd.id; // LitOrdch.opportunity=LItord.id; LitOrdch.PricebookEntryId=p.id; Oppli.add(LitOrdch); // } } } } public void addrow() { Opportunity LitOrd = new Opportunity(); //Opportunity LitOrd1 = new Opportunity(); Opps.add(LitOrd); //Opps.add(LitOrd1); } public void removerow() { Integer i = Opps.size(); Opps.remove(i-1); } public PageReference save() { try{ insert Opps; for(Opportunity Opps1 :Opps){ for(opportunityLineItem oppli1 :Oppli){ Oppli1.OpportunityId = Opps1.id; } //insert Oppli; } 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(); } }
Without reading the post in complete detail, you're trying to achieve a parent - child relationship.
If the parent records dont already exist, i.e. they dont already have a salesforce id, then you could set an internal id on them, which would help you relate the parent to child records
eg. on Opportunity create a field called Internal_Id__c, which is hidden and not displayed
Map<String, Opportunity> opps = new Map<String, Opportunity>{};
// a map of opportunity ids to a list of child objects
Map<String, List<OpportunityLineItem>> oppLI = new Map<String, List<OpportunityLineItem>>{};
so when you create a new parent
Opportunity parent = new Opportunity(Internal_Id__c = '001');
opps.add(parent.Internal_Id__c, parent);
new chilren
oppLI.put(parent.Internal_Id__c, new List<OpportunityLineItem>);
oppLi.get(parent.Internal_Id__c).add(new OpportunityLineItem());
first persist the parent list
so Database.insert(opps.Values());
and then grab the references and set on children
List<OpportunityLineItem> children = new List<OpportunityLineItem>{};
for(String oppId : oppLI.keySet()){
Id parentId = opps.get(oppId).Id; // grab the salesforce id, now that parent has been inserted
for(OpportunityLineItem oli : oppLI.get(parentId)){
oli.OpportunityId = parentId; //set parent reference
children.add(oli); //aggregate children for insert
}
}
//Now that parent references have been set, insert children
Database.insert(children);
Hi
I created the Field Internal id but un fortunately getting some Errors on saving the class
i have problems in inserting the child to right parent
please suggest me which part of the following code needs to be changed