0

I have one object named "Order_batch__c".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

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.

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>();
    map<string, order_batch__c> ObjMap = new map<string, order_batch__c>();
    for (order_batch__c ob : Trigger.new){
    if(Trigger.isInsert){
   orders.add(new order__c(Or_customer_PO_Number__c = ob.Or_customer_PO_Number__c,Account_code__c=ob.Or_customerid__c));
   ids.add(ob.Or_customer_PO_Number__c);
    }
     if(Trigger.isupdate)
        {
      if (ob.Or_customer_PO_Number__c != Null){
      ObjMap.put(ob.Or_customer_PO_Number__c, ob);
     }
     }
    }
    insert orders;

//list<order_batch__c> obc= new list<order_batch__c>([select id,Or_customer_PO_Number__c from order_batch__c where Or_customer_PO_Number__c in:ids Limit 1]);   
list<order__c> ord= new list<order__c>([select id,Or_customer_PO_Number__c,Account_code__c from order__c where Or_customer_PO_Number__c IN :ObjMap.KeySet()]);
    for (order__c ob : ord){
    order_batch__c obc=ObjMap.get(ob.Or_customer_PO_Number__c);
    uporders.add(new order__c(id=ob.id,Or_customer_PO_Number__c = obc.Or_customer_PO_Number__c,Account_code__c=obc.Or_customerid__c));     
    }
    if(uporders.size() > 0){
    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 :ObjMap.KeySet() and name in:ids1]);
for (order_item__c ob : orderlineitem){
    order_batch__c obc=ObjMap.get(ob.Order_customer_po_number__c);
   upitems.add(new order_item__c(id=ob.id,name = obc.Or_Nicomatic_p_n__c,Quantity__c = obc.Quantity_Ordered__c));     
          }
    if(upitems.size()>0){
    update upitems;
   }  
}

For this :insert is happening for both parent and child (order,orderitem) Update is happening for only parent .

update is not happening for child .Help me regarding this issue

  • Teja -- you might get more assistance if you a) reformatted your code to make it readable (proper indenting) and b) used common courtesies in your entreaty for assistance – cropredy Dec 8 '14 at 17:54
1

Obviously, I don't know exactly how your order system works, but here's what I see that appears to be different that may be the source of your problem:

if(Trigger.isInsert){
   orders.add(new order__c(Or_customer_PO_Number__c = ob.Or_customer_PO_Number__c,Account_code__c=ob.Or_customerid__c));
   ids.add(ob.Or_customer_PO_Number__c);   // <--- This line isn't repeated for if(Trigger.isupdate) to add line items
}
if(Trigger.isupdate)
{
   if (ob.Or_customer_PO_Number__c != Null){
       ids.add(ob.Or_customer_PO_Number__c); // Insert this line here to add the new line items to your PO    
       ObjMap.put(ob.Or_customer_PO_Number__c, ob)
   }
}

You run this query which applies to both after insert and after update conditions. Perhaps you meant to add an if(isInsert) clause here?

list<order__c> ord= new list<order__c>([select id,Or_customer_PO_Number__c,Account_code__c from order__c where Or_customer_PO_Number__c IN :ObjMap.KeySet()]);

// you then start this loop for both:

for (order__c ob : ord){ 
.
.
.
// if(Trigger.isInsert) you do the following:
         }
    items.add(new Order_Item__c(  
.
.
.
       {

// But  if(Trigger.isupdate) you do what's below. If this is to be an after insert loop, this code belongs elsewhere.
{
  ids1.add(ob.Or_Nicomatic_p_n__c); 
  // Are these from your query or did they get added through the line I added above? 
  // You're outside the trigger.new loop at this point in your trigger and aren't coming from there.
} 

Next you do an insert followed by another query on line items

    insert items;

Perhaps you meant to add an `if(isUpdate) here?

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 :ObjMap.KeySet() and name in:ids1]);

Now you have this loop:

for (order_item__c ob : orderlineitem){
    order_batch__c obc=ObjMap.get(ob.Order_customer_po_number__c);
    upitems.add(new order_item__c(id=ob.id,name = obc.Or_Nicomatic_p_n__c,Quantity__c = obc.Quantity_Ordered__c));     
}

Then finally perform your update:

   if(upitems.size()>0){
    update upitems;

It appears to me that these two sections of code need to be isolated with if statements in order to solve your problems. I also suspect you need to add the statement I brought to your attention at the beginning which I suspect adds the line items to your order, but again, I don't know your system.

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.