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
Girbson Bijou 8Girbson Bijou 8 

Copy Related Records to Other Object

Considering the attached Picture, i want to copy the A Delivery with all its related list to Container and Related List.  I mean,  The Value of Delivey will be a new record in the object Container and the Item Distributed of the object Delivery will become Arcticle Container for the object Container. 
The Code below saved well but it fires with the following error :
caused by: System.ListException: Before Insert or Upsert list must not have two identically equal elements. Line 51
trigger PostToAfficliateInventory on Delivery__c (after update) {

list <Container__c> con = new List <Container__c>();
list <Articles_Containers__c> contenairItems = new list <Articles_Containers__c>();

    for (Delivery__c d : Trigger.New){
         if (d.Is_Posted_To_Affiliate_Inventory__c){
             Container__c c = new  Container__c();
             c.Name = d.Automatic_Code__c;
             c.FFP_Centers__c = d.Affiliate_Center__c ;
             c.RecordTypeId = '0126A000000nMlU';
             c.Distribution_Center__c = d.AffiliateCenterId__c;
             c.Is_Owner_Shipper__c = 'No';
             c.Provenance__c = 'FFP PAP';
             c.Type__c = 'Food';
             c.Shipment_Status__c = 'A - AWAITING ARRIVAL';
             c.Is_Automatic_Creation__c = TRUE;
             
             con.add(c);  
             insert con;
          
             Articles_Containers__c ac = new Articles_Containers__c();
             list<Item_Distributed__c> itemList = new  list<Item_Distributed__c>();
             
             
             itemList = [SELECT id,Name, Product__r.Expiration_Date__c,Product__r.Unit_Cost__c,   
                                 Product__r.Lot_Number__c,  Product__r.Unit_Weight__c ,
                                 Product__r.UM__c, Product__r.Product__r.Id , Quantity__c 
                         FROM Item_Distributed__c  
                         WHERE Delivery__c =: d.Id];
          
             for (Item_Distributed__c OrderItems: itemList){

                 for(Integer i = 0; i<itemList.Size(); i++){
                     ac.FFP_Centers__c = d.Affiliate_Center__c ;
                    ac.RecordTypeId = '0126A0000019jRJ'; 
                     ac.Container__c =c.Id;
                     ac.Product__c = OrderItems.Product__r.Product__r.Id;
                     ac.Number__c= 0;
                     ac.Quantity__c = OrderItems.Quantity__c;
                     ac.Unit_Of_Measure_Paking__c= OrderItems.Product__r.UM__c;
                     ac.UM__c = OrderItems.Product__r.UM__c;
                     ac.Expiration_Date__c = OrderItems.Product__r.Expiration_Date__c;
                     ac.Lot_Number__c = OrderItems.Product__r.Lot_Number__c;
                     ac.Unit_Cost__c = OrderItems.Product__r.Unit_Cost__c;
                     ac.Unit_Weight__c =OrderItems.Product__r.Unit_Weight__c;
                     
                     contenairItems.add(ac);
                     Insert contenairItems;
                }
             }          
         }
    } 
}

User-added image
Best Answer chosen by Girbson Bijou 8
Deepak GerianiDeepak Geriani
Okay its not working because you are not refershing the object memory 
for (Item_Distributed__c OrderItems: itemList){
                   ac = new Articles_containers__c();
                    ac.FFP_Centers__c = d.Affiliate_Center__c ;
                    ac.RecordTypeId = '0126A0000019jRJ'; 
                     ac.Container__c =c.Id;
                     ac.Product__c = OrderItems.Product__r.Product__r.Id;
                     ac.Number__c= 0;
                     ac.Quantity__c = OrderItems.Quantity__c;
                     ac.Unit_Of_Measure_Paking__c= OrderItems.Product__r.UM__c;
                     ac.UM__c = OrderItems.Product__r.UM__c;
                     ac.Expiration_Date__c = OrderItems.Product__r.Expiration_Date__c;
                     ac.Lot_Number__c = OrderItems.Product__r.Lot_Number__c;
                     ac.Unit_Cost__c = OrderItems.Product__r.Unit_Cost__c;
                     ac.Unit_Weight__c =OrderItems.Product__r.Unit_Weight__c;
                     
                     contenairItems.add(ac);
                     
                }
Insert contenairItems;


updated the code 

 

if your issue is resolved please mark it as solved 
thanks

All Answers

Deepak GerianiDeepak Geriani

Hello Girbson Bijou,

You are runing the for loop  each item for itemList you just need to remove the inside for loop and run again 

for (Item_Distributed__c OrderItems: itemList){
                    ac.FFP_Centers__c = d.Affiliate_Center__c ;
                    ac.RecordTypeId = '0126A0000019jRJ'; 
                     ac.Container__c =c.Id;
                     ac.Product__c = OrderItems.Product__r.Product__r.Id;
                     ac.Number__c= 0;
                     ac.Quantity__c = OrderItems.Quantity__c;
                     ac.Unit_Of_Measure_Paking__c= OrderItems.Product__r.UM__c;
                     ac.UM__c = OrderItems.Product__r.UM__c;
                     ac.Expiration_Date__c = OrderItems.Product__r.Expiration_Date__c;
                     ac.Lot_Number__c = OrderItems.Product__r.Lot_Number__c;
                     ac.Unit_Cost__c = OrderItems.Product__r.Unit_Cost__c;
                     ac.Unit_Weight__c =OrderItems.Product__r.Unit_Weight__c;
                     
                     contenairItems.add(ac);
                     Insert contenairItems;
                }


please update the code like above code 

if your issue is resolved please mark it as solved 
thanks

Girbson Bijou 8Girbson Bijou 8
Hi Deepak, 
It works only when thre is only one related record. 
Deepak GerianiDeepak Geriani
Okay its not working because you are not refershing the object memory 
for (Item_Distributed__c OrderItems: itemList){
                   ac = new Articles_containers__c();
                    ac.FFP_Centers__c = d.Affiliate_Center__c ;
                    ac.RecordTypeId = '0126A0000019jRJ'; 
                     ac.Container__c =c.Id;
                     ac.Product__c = OrderItems.Product__r.Product__r.Id;
                     ac.Number__c= 0;
                     ac.Quantity__c = OrderItems.Quantity__c;
                     ac.Unit_Of_Measure_Paking__c= OrderItems.Product__r.UM__c;
                     ac.UM__c = OrderItems.Product__r.UM__c;
                     ac.Expiration_Date__c = OrderItems.Product__r.Expiration_Date__c;
                     ac.Lot_Number__c = OrderItems.Product__r.Lot_Number__c;
                     ac.Unit_Cost__c = OrderItems.Product__r.Unit_Cost__c;
                     ac.Unit_Weight__c =OrderItems.Product__r.Unit_Weight__c;
                     
                     contenairItems.add(ac);
                     
                }
Insert contenairItems;


updated the code 

 

if your issue is resolved please mark it as solved 
thanks

This was selected as the best answer
Girbson Bijou 8Girbson Bijou 8
It give the same issue. 
Here is the code
trigger PostToAfficliateInventory on Delivery__c (after update) {

list <Container__c> con = new List <Container__c>();
list <Articles_Containers__c> contenairItems = new list <Articles_Containers__c>();

    for (Delivery__c d : Trigger.New){
         if (d.Is_Posted_To_Affiliate_Inventory__c){
             Container__c c = new  Container__c();
             c.Name = d.Automatic_Code__c;
             c.FFP_Centers__c = d.Affiliate_Center__c ;
             c.RecordTypeId = '0126A000000nMlU';
             c.Distribution_Center__c = d.AffiliateCenterId__c;
             c.Is_Owner_Shipper__c = 'No';
             c.Provenance__c = 'FFP PAP';
             c.Type__c = 'Food';
             c.Shipment_Status__c = 'A - AWAITING ARRIVAL';
             c.Is_Automatic_Creation__c = TRUE;
             
             con.add(c);  
             insert con;
          
             Articles_Containers__c ac = new Articles_Containers__c();
             list<Item_Distributed__c> itemList = new  list<Item_Distributed__c>();
             
             
             itemList = [SELECT id,Name, Product__r.Expiration_Date__c,Product__r.Unit_Cost__c,   
                                 Product__r.Lot_Number__c,  Product__r.Unit_Weight__c ,
                                 Product__r.UM__c, Product__r.Product__r.Id , Quantity__c 
                         FROM Item_Distributed__c  
                         WHERE Delivery__c =: d.Id];
          
             for (Item_Distributed__c OrderItems: itemList){

                 //for(Integer i = 0; i<itemList.Size(); i++){
                     ac.FFP_Centers__c = d.Affiliate_Center__c ;
                     ac.RecordTypeId = '0126A0000019jRJ'; 
                     ac.Container__c =c.Id;
                     ac.Product__c = OrderItems.Product__r.Product__r.Id;
                     ac.Number__c= 0;
                     ac.Quantity__c = OrderItems.Quantity__c;
                     ac.Unit_Of_Measure_Paking__c= OrderItems.Product__r.UM__c;
                     ac.UM__c = OrderItems.Product__r.UM__c;
                     ac.Expiration_Date__c = OrderItems.Product__r.Expiration_Date__c;
                     ac.Lot_Number__c = OrderItems.Product__r.Lot_Number__c;
                     ac.Unit_Cost__c = OrderItems.Product__r.Unit_Cost__c;
                     ac.Unit_Weight__c =OrderItems.Product__r.Unit_Weight__c;
                     
                     contenairItems.add(ac);
                     
                    
                //}
                
             }
             Insert contenairItems;
         }
        
    } 
}

 
Deepak GerianiDeepak Geriani

Hello,

here is changes i had made to your code please just copy and paste and let me know if you are facing any issue and if it solved then mark it as solved 

trigger PostToAfficliateInventory on Delivery__c (after update) {

list <Container__c> con = new List <Container__c>();
list <Articles_Containers__c> contenairItems = new list <Articles_Containers__c>();
list<Item_Distributed__c> itemList = new  list<Item_Distributed__c>();
Container__c c;
    for (Delivery__c d : Trigger.New){

         if (d.Is_Posted_To_Affiliate_Inventory__c){
             c = new  Container__c();
             c.Name = d.Automatic_Code__c;
             c.FFP_Centers__c = d.Affiliate_Center__c ;
             c.RecordTypeId = '0126A000000nMlU';
             c.Distribution_Center__c = d.AffiliateCenterId__c;
             c.Is_Owner_Shipper__c = 'No';
             c.Provenance__c = 'FFP PAP';
             c.Type__c = 'Food';
             c.Shipment_Status__c = 'A - AWAITING ARRIVAL';
             c.Is_Automatic_Creation__c = TRUE;
             
             insert c;

             Articles_Containers__c ac = new Articles_Containers__c();
             itemList = new  list<Item_Distributed__c>();
               itemList = [SELECT id,Name, Product__r.Expiration_Date__c,Product__r.Unit_Cost__c,   
                                 Product__r.Lot_Number__c,  Product__r.Unit_Weight__c ,
                                 Product__r.UM__c, Product__r.Product__r.Id , Quantity__c 
                         FROM Item_Distributed__c  
                         WHERE Delivery__c  IN=: d.id];

             for (Item_Distributed__c OrderItems: itemList){
                     ac.FFP_Centers__c = d.Affiliate_Center__c ;
                     ac.RecordTypeId = '0126A0000019jRJ'; 
                     ac.Container__c =c.Id;
                     ac.Product__c = OrderItems.Product__r.Product__r.Id;
                     ac.Number__c= 0;
                     ac.Quantity__c = OrderItems.Quantity__c;
                     ac.Unit_Of_Measure_Paking__c= OrderItems.Product__r.UM__c;
                     ac.UM__c = OrderItems.Product__r.UM__c;
                     ac.Expiration_Date__c = OrderItems.Product__r.Expiration_Date__c;
                     ac.Lot_Number__c = OrderItems.Product__r.Lot_Number__c;
                     ac.Unit_Cost__c = OrderItems.Product__r.Unit_Cost__c;
                     ac.Unit_Weight__c =OrderItems.Product__r.Unit_Weight__c;
                     
                     contenairItems.add(ac);
                
             }
             Insert contenairItems;
         }
        
    } 
}
Girbson Bijou 8Girbson Bijou 8
Hi Deepak, 
When i paste you code , i get the following errors in the line 29 and the code can not be saved.
Unexpected token '='.
Unexpected token ':'.
Extra ';', at ']'.
Expression cannot be a statement.
Expression cannot be assigned // Line 25

in the line 29, when i replace   WHERE Delivery__c IN=: d.id]  by WHERE Delivery__c =: d.id];  the code is saved but the trigger the fire with the same error 

 
Deepak GerianiDeepak Geriani
Just update the Query as below code or you can copy and paste it.
itemList = [SELECT id,Name, Product__r.Expiration_Date__c,Product__r.Unit_Cost__c,   
                                 Product__r.Lot_Number__c,  Product__r.Unit_Weight__c ,
                                 Product__r.UM__c, Product__r.Product__r.Id , Quantity__c 
                         FROM Item_Distributed__c  where Delivery__c IN: d.id ];
if it solved then mark it as solved 
Girbson Bijou 8Girbson Bijou 8
I get this error when the code is saving:  "IN operator must be used with an iterable expression". I create a variable deliveryId   and use it as following
 Set<id> deliveryId = new Set<id>();
 where Delivery__c IN: deliveryId ];

The trigger fires and creates only the parent but not the childs
 
Deepak GerianiDeepak Geriani

Here is the Query Its correct that In Shoud be used with the iterable expression.

itemList = [SELECT id,Name, Product__r.Expiration_Date__c,Product__r.Unit_Cost__c,   
                                 Product__r.Lot_Number__c,  Product__r.Unit_Weight__c ,
                                 Product__r.UM__c, Product__r.Product__r.Id , Quantity__c 
                         FROM Item_Distributed__c  where Delivery__c  =: d.id ];


As we are using single id when query the record, but you should query it outside the for loop and create map of itemList as Apex Code Best Practice.

https://developer.salesforce.com/index.php?title=Apex_Code_Best_Practices&oldid=26951 (https://developer.salesforce.com/index.php?title=Apex_Code_Best_Practices&oldid=26951)

Girbson Bijou 8Girbson Bijou 8
Hi Deepak,
You got the best answer.  we should only add  Articles_containers ac = new Articles_Container__c() inside the For loop as below. 
Thank you for your help.
for (Item_Distributed__c OrderItems: itemList){

                     Articles_containers ac = new Articles_Container__c();
                     ac.FFP_Centers__c = d.Affiliate_Center__c ;
                     ac.RecordTypeId = '0126A0000019jRJ'; 
                     ac.Container__c =c.Id;
                     ac.Product__c = OrderItems.Product__r.Product__r.Id;
                     ac.Number__c= 0;
                     ac.Quantity__c = OrderItems.Quantity__c;
                     ac.Unit_Of_Measure_Paking__c= OrderItems.Product__r.UM__c;
                     ac.UM__c = OrderItems.Product__r.UM__c;
                     ac.Expiration_Date__c = OrderItems.Product__r.Expiration_Date__c;
                     ac.Lot_Number__c = OrderItems.Product__r.Lot_Number__c;
                     ac.Unit_Cost__c = OrderItems.Product__r.Unit_Cost__c;
                     ac.Unit_Weight__c =OrderItems.Product__r.Unit_Weight__c;
                     
                     contenairItems.add(ac);
                
             }