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
Cesar Ramirez Vasquez005391619375684564Cesar Ramirez Vasquez005391619375684564 

How can i create a loop to insert multiple invoice lines based on InvoiceID from the invoice object ? Insert multiple child objects from 1 parent is that possible ?

This is what i write

Cart= new list<Product__c>(); //This list is filled by the client and the products he want to add

try{
        for  ( Product__c C : Cart) {
       
        Line.Quantity__c = C.CantidadExistencia__c;
        //Line.ProductName__c = C.ProductName__c;
        Line.Invoice__c = x; //This field should be filled with the autogenerated number of the invoiceID this is a master relationship from line with Invoice
        Line.Price__c = C.Price__c;

               
        Linea.Product__c = C.Code__c;
    
        insert(Line);
      
        }


How can i make that work im pretty new to SF and apex i hope someone can help me.

Thanks
Best Answer chosen by Cesar Ramirez Vasquez005391619375684564
Swati GSwati G
Yes, you can insert multiple child for a parent.

List<Product_Line__c> lines = new list<Product_Line__c>() // create list of invoice line. assuming object api name as Product_Line__c
for(Product__c C : Cart) {

  for(Integer i =0; i < 5;i++){ // Creating loop here. it will create five lines with same details. You can change this according to your requirement
  
   Product_Line__c line = new Product_Line__c();
  
   Line.Quantity__c = C.CantidadExistencia__c;
   //Line.ProductName__c = C.ProductName__c;
   Line.Invoice__c = x; //This field should be filled with the autogenerated number of the invoiceID this is a master relationship from line with Invoice
   Line.Price__c = C.Price__c;
   Line.Product__c = C.Code__c;
  
   lines.add(Line); // adding line to list
  }

}

insert lines; // Perform insert after for loop. this is required to handle governor limits.

All Answers

Swati GSwati G
Yes, you can insert multiple child for a parent.

List<Product_Line__c> lines = new list<Product_Line__c>() // create list of invoice line. assuming object api name as Product_Line__c
for(Product__c C : Cart) {

  for(Integer i =0; i < 5;i++){ // Creating loop here. it will create five lines with same details. You can change this according to your requirement
  
   Product_Line__c line = new Product_Line__c();
  
   Line.Quantity__c = C.CantidadExistencia__c;
   //Line.ProductName__c = C.ProductName__c;
   Line.Invoice__c = x; //This field should be filled with the autogenerated number of the invoiceID this is a master relationship from line with Invoice
   Line.Price__c = C.Price__c;
   Line.Product__c = C.Code__c;
  
   lines.add(Line); // adding line to list
  }

}

insert lines; // Perform insert after for loop. this is required to handle governor limits.
This was selected as the best answer
Cesar Ramirez Vasquez005391619375684564Cesar Ramirez Vasquez005391619375684564
Hi thanks for your reply; and stil have somo doubts these assignments are correct (Line.Quantity__c = C.CantidadExistencia__c;) the list is of type Product but i want to match those values to Line Object values.
And how can i get the parent Id because this loop is inside a method that insert a invoice but the invoice Id is an auto number. Those lines have to match their parent 

Swati GSwati G
Hi Cesar,

When you insert any object salesforce populates the id in same instance. For example, if you execute the below statement then product.Id will give you the id of inserted product after insert statement.

insert product;


If you want to use the auto number file then you will have to do query on the inserted object.