You need to sign in to do that
Don't have an account?
Manohar kumar
need to create parent and child in a loop
Hi Everyone,
i have a list of records which fields i am using to create, one parent record and one child record.i have one list for both parent and child.
i am inserting those list outside the loop. Now i have to set relationship between those. i cant do this inside loop because its not inserted yet.
Not able to figure out how do i do this.
for(rolinesDetails rold : rols){ Repair_Order_Line__c rol = new Repair_Order_Line__c(); rol.Quantity_to_Repair__c = rold.Quantity_Accepted; rol.Created_From_ROL__c = true; rol.Original_Inventory_Line__c = ?? // how do i put inv line id here rolls.add(rol); Inventory_Line__c il2 = new Inventory_Line__c(); il2.Acquisition_Cost__c = rold.roline.Estimated_Repair_Cost__c; il2.Serial_Number__c = rold.Serial_Number; invlist.add(il2); } insert invlist; insert rolls;its seems simple but cant figure out the solution. And i also need rolinesDetails fields value for creating both records.
RepairOrderLine is child and InventoryLine is parent. Thanks in advance.
Thnaks,
Manohar
Hi Manohar,
Does your parent object (InventoryLine) have any External Id field? If YES, you can use the External Id to create parent and child in a single statement. The example is shared in the developer guide
https://resources.docs.salesforce.com/sfdc/pdf/salesforce_apex_language_reference.pdf
Search for Creating Parent and Child Records in a Single Statement Using Foreign Keys and you will find the example.
If you do not have ExternalId's you can use another approach. Remember that List is always indexed in salesforce so we can use this to setup our logic. I have shared the logic for this in a previous post that you can review at https://developer.salesforce.com/forums/ForumsMain?id=9060G000000IBWqQAO
Thanks
Vivian
All Answers
Hi Manohar,
Does your parent object (InventoryLine) have any External Id field? If YES, you can use the External Id to create parent and child in a single statement. The example is shared in the developer guide
https://resources.docs.salesforce.com/sfdc/pdf/salesforce_apex_language_reference.pdf
Search for Creating Parent and Child Records in a Single Statement Using Foreign Keys and you will find the example.
If you do not have ExternalId's you can use another approach. Remember that List is always indexed in salesforce so we can use this to setup our logic. I have shared the logic for this in a previous post that you can review at https://developer.salesforce.com/forums/ForumsMain?id=9060G000000IBWqQAO
Thanks
Vivian
Do like below psedu code.
Thanks,
Lokesh
Any progress with the development?
Thanks
Vivian
Hi Vivian,
Sorry for the late response. It worked and thanks for you help.
Manohar