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
Manohar kumarManohar 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

  

Best Answer chosen by Manohar kumar
Vivian Charlie 1208Vivian Charlie 1208

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

Vivian Charlie 1208Vivian Charlie 1208

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

This was selected as the best answer
Lokesh KumarLokesh Kumar
Hi Manohar,

Do like below psedu code.
 
Account acc=new Account(Name='Blog Acc 8', Master_Id__c='Blog Acc 8');
Contact cont=new Contact(FirstName='Bob', LastName='Buzzard', Account=new Account(Master_Id__c='Blog Acc 8'));
 
insert new List<Sobject>{acc, cont};

Thanks,
Lokesh
Vivian Charlie 1208Vivian Charlie 1208
Hi Manohar,


Any progress with the development?

Thanks
Vivian
Manohar kumarManohar kumar

Hi Vivian,

Sorry for the late response. It worked and thanks for you help.

Manohar