I have one object named "Order_batch__c" .In that i have records in such a way
I want to separate the data into two records one is parent object "order" and child object "order item"
- order consists of fields : customer po,account name, customer id
- order item consists of fields : nicomatic pin,quantity ordered
Trigger:
trigger orderitem on order_batch__c (after insert , after update) {
order__c[] orders = new order__c[] {};
order__c[] uporders=new order__c[]{};
set<string> ids=new set<string>();
set<string> ids1=new set<string>();
for (order_batch__c ob : Trigger.new){
if(Trigger.isInsert){
orders.add(new order__c(Customer_Po__c = ob.Or_customer_PO_Number__c));
ids.add(ob.Or_customer_PO_Number__c);
}
if(Trigger.isupdate)
{
ids.add(ob.Or_customer_PO_Number__c);
}
}
insert orders;
list<order__c> ord= new list<order__c>([select id,Customer_Po__c from order__c where Customer_Po__c in:ids ]);
for (order__c ob : ord){
uporders.add(new order__c(id=ob.id,Customer_Po__c = ob.Customer_Po__c));
}
update uporders;
Order_Item__c[] upitems = new Order_Item__c[] {};
Order_Item__c[] items = new Order_Item__c[] {};
Integer index = 0;
for (order_batch__c ob : Trigger.new)
{
if(Trigger.isInsert)
{
items.add(new Order_Item__c(
Name = ob.Or_Nicomatic_p_n__c,
Client_PN__c = ob.Or_clientpn__c,
Quantity__c = ob.Quantity_Ordered__c,
Request_Date__c = ob.Requested_Date__c,
Discount__c = ob.Discount__c,
Price__c = ob.Unit_price__c,
Order__c = orders[index].Id
));
index++;
}
if(Trigger.isupdate)
{
ids1.add(ob.Or_Nicomatic_p_n__c);
}
}
insert items;
List<Order_Item__c> orderlineitem=new list<order_item__c>([select id ,name,Order_customer_po_number__c from order_item__c where Order_customer_po_number__c In:ids and name in:ids1]);
for (order_item__c ob : orderlineitem){
upitems.add(new order_item__c(id=ob.id,name = ob.name));
}
update upitems;
}
My requirement is to separate records in parent and child records .when ever there is an update in order batch records it has to update accordingly parent or child.
I am getting error in the above trigger .can any body help me regarding this