0

I have one object named "Order_batch__c" .In that i have records in such a way

enter image description here

I want to separate the data into two records one is parent object "order" and child object "order item"

  1. order consists of fields : customer po,account name, customer id
  2. 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

  • Hi, what error are you getting, do you mean as update trigger fires or insert trigger fires, you want to create Parent/Child records? – Amit Bangad Dec 4 '14 at 9:14
  • @AmitBangad Error Error: Compile Error: Illegal assignment from String to SOBJECT:order__c at line 12 column 26 – Teja Dec 4 '14 at 9:18
  • Both update and insert i want to fire triger .currently i am trying for insertion after i will update code for updation – Teja Dec 4 '14 at 9:19
  • Error: Compile Error: Invalid foreign key relationship: order__c.temp at line 12 column 28 – Teja Dec 4 '14 at 9:21
  • @Keith C .my records updated functionality not working – Teja Dec 5 '14 at 9:24
1

Assuming you do want to create an ID-based relationship between Order_Item__c and Order__c, you will first have to insert the Order__c objects so their IDs are allocated. Bulkifying too, you end up with (may contain typos):

trigger orderitem on order_batch__c (after insert , after update) {

    order__c[] orders = new order__c[] {};
    for (order_batch__c ob : Trigger.new)
    {
        orders.add(new order__c(
                Customer_Po__c = ob.Or_customer_PO_Number__c
                ));
    }
    insert orders;

    Order_Item__c[] items = new Order_Item__c[] {};
    Integer index = 0;
    for (order_batch__c ob : Trigger.new)
    {
        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++;
    }
    insert items;
}

But by including "after update" you run the danger of many objects being created; perhaps for the update case the trigger should search for and update the Order__c and Order_Item__c records.

  • Thanks for your answer ,but update will be done from main order_batch__c – Teja Dec 4 '14 at 9:30
  • @Teja This trigger will fire and do more inserts as written at the moment. – Keith C Dec 4 '14 at 9:32
  • k fine .thankyou – Teja Dec 4 '14 at 9:33
  • @Teja Your current edits won't compile; I suggest if you choose to edit a answer you add to the answer not replace it. – Keith C Dec 5 '14 at 9:26
  • .i am sorry .I thought i am editing question . – Teja Dec 5 '14 at 9:51
1

If you can replace line 12 by :

o.Customer_Po__c= temp.Or_customer_PO_Number__c;

your primary error will be removed!

Cheers,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.