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 Delivery will be a new record in the object Container and the Item Distributed of the object Delivery will become Article 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.

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){
         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];
        if(itemList.Size()>0) {
            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;
                 insert con;
                 Articles_Containers__c ac = new Articles_Containers__c();
                 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;
                 Insert contenairItems;  

enter image description here

  • Does this need to be done in a trigger? It might be best to implement this as a quick action and have it be performed on button click instead of via trigger on record save. What is the user journey that would kick off this process? Jun 26, 2020 at 20:33
  • you have a few issues here: 1) the query for Item_Distributed is within a for loop - you need to bulkify this code; 2) Multiple Item_Distributed records are pointing at the same Container; 3) you are doing inserts of containers inside of for loop. Bulkify this. I suggest you read the apex doc on bulkification
    – cropredy
    Jun 27, 2020 at 18:11
  • Thank you both for reply. @MitchSpano, i like your idea. I know how to create quick action but i do not already implement a case like this. Can you describe how this process will be done. Will i need A Apex class. Please let me know the steps. Jun 29, 2020 at 12:18

1 Answer 1


I think the best way to accomplish something like this would be to perform this action on quick action button click rather than in a trigger. Here are some of the steps that would be necessary to implement a solution like this:

  1. Create an Apex class with a method that performs the copying and insertion of these records. This class would be called something like DeliveryToContainerController and it would have a method that looks something like this: @AuraEnabled public static Id copyDeliveryToContainer(Id deliveryId) {...} where you would query for Delivery and related Distributed items, map fields to Container and Article Container fields, insert Container and Article Container records, and return Container Id.
  2. Create an Aura Lightning Component called DeliveryToContainerQuickAction. This component would implement the force:appHostable, flexipage:availableForAllPageTypes, force:lightningQuickAction and the force:hasRecordId interfaces and uses the DeliveryToContainerController defined in step 1. You would then use the recordId attribute as the input to the method we defined earlier and call that method on init of the component. Finally, you would use the lightning:navigation component to navigate to the newly created Container record using the Id returned by the method.
  3. Create a Quick Action on the Delivery sObject and select the DeliveryToContainerQuickAction component that was created in step 2 as the component. Add that to the delivery page layout and voila!

There are a couple of additional things to consider with this approach:

  1. Hard coding field mappings might not be the best, so perhaps in the future you would investigate the use of Custom Metadata to define the field mappings between the sObjects.
  2. This button would always be visible on the Delivery page layout, so there may need to be some validation performed and perhaps an exception thrown if the Delivery is not ready to be copied to a Container yet.

I hope this helps you out!


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .