You need to sign in to do that
Don't have an account?
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
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
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
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.
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
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.