You need to sign in to do that
Don't have an account?

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

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; } } } } }
updated the code
if your issue is resolved please mark it as solved
thanks
All Answers
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
please update the code like above code
if your issue is resolved please mark it as solved
thanks
It works only when thre is only one related record.
updated the code
if your issue is resolved please mark it as solved
thanks
Here is the code
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
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
Set<id> deliveryId = new Set<id>();
where Delivery__c IN: deliveryId ];
The trigger fires and creates only the parent but not the childs
Here is the Query Its correct that In Shoud be used with the iterable expression.
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)
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.