You need to sign in to do that
Don't have an account?
Trigger to create child records
Hi,
I have the following custom objects:
1. Purchase Order(master) and Purchase_order_lines(detail)
2.Purchase_Order_Receipt(master) and Purchase_Order_ Receipt_lines(detail).
Purchase_Order_Receipt has a lookup relationship with Purchase Order(not able to create a master detail :"You cannot convert this field to a master-detail relationship because this would exceed the maximum depth of a master-detail relationship")
Whenever a new Purchase Order Receipt is created,a trigger should create Purchase_order_receipt_lines and populate them with the purchase order lines.
I have tried writing the following trigger:
trigger rec on Purchase_Order_Receipt__c (after insert)
{
Purchase_Order_Receipt__c to=trigger.new[0];
Purchase_Order__c p=[select id from Purchase_Order__c where id=:to.Purchase_Order__c];
list<Purchase_Order_Line__c> pl=[select id,Quantity__c,Unit_Cost__c,Product__c,purchase_order__c from Purchase_Order_Line__c where Purchase_Order__c=:p.id ];
for(Purchase_Order_Line__c pli:pl)
{
Purchase_Order_Receipt_Lines__c rl=new Purchase_Order_Receipt_Lines__c();
rl.Product__c=pli.Product__c;
rl.Purchase_Order__c=pli.purchase_order__c;
insert rl;
}
}
But this is erroring out :
"Apex trigger rec caused an unexpected exception, contact your administrator: rec: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Purchase Order Receipt]: [Purchase Order Receipt]: Trigger.rec: line 25, column 1".
Please help.
Hi,
I guess, in the Purchase_Order_Receipt_Lines__c, Name is the mandatory field and since the name field is missing in the below code, you might have got this error.
Thanks
All Answers
Hi,
I guess, in the Purchase_Order_Receipt_Lines__c, Name is the mandatory field and since the name field is missing in the below code, you might have got this error.
Thanks
Thanks Sureka! This resolved the problem