trigger InsertOrderBatch on Batch__c(after update,after insert){
List<order_batch__c> oblist= new List<order_batch__c>();
List<order_batch__c> itemupdateList= new List<order_batch__c>();
Map<id,order_batch__c> ids=new Map<id,order_batch__c>();
if(trigger.IsAfter && trigger.Isinsert){
for(batch__c acct:Trigger.new){
order_batch__c ob = new order_batch__c();
ob.Quantity_Ordered__c = acct.Asked_Quantity_Number__c;
ob.Discount__c=acct.Discount__c;
ob.Unit_price__c=acct.Unit_Price__c;
ob.Batch__c=acct.id;
ob.Quote_Line_Item__c=acct.Quote_Line_Item__c;
oblist.add(ob);
}
if(oblist.isEmpty()== false){
insert oblist;
}
}
// list<batch__c> obclist=[select id from batch__c where id=:ids.Keyset()];
if(trigger.IsAfter && trigger.IsUpdate){
set<id> unique =new set<id>();
for(batch__c b:trigger.new){
unique.add(b.id);
//ids(b.id,)
}
list<order_batch__c> obclist=[select batch__c,Quantity_Ordered__c,Discount__c,Unit_price__c,Quote_Line_Item__c from order_batch__c where batch__c=:unique];
ids=new Map<Id,order_batch__c>([select batch__c,Quantity_Ordered__c,Discount__c,Unit_price__c,Quote_Line_Item__c from order_batch__c where batch__c IN:unique]);
for(order_batch__c oc:obclist){
ids.put(oc.batch__c,oc);
}
for(batch__c b:trigger.new){
order_batch__c ob=ids.get(b.id);
ob.Quantity_Ordered__c = b.Asked_Quantity_Number__c;
ob.Discount__c=b.Discount__c;
ob.Unit_price__c=b.Unit_Price__c;
ob.Quote_Line_Item__c=b.Quote_Line_Item__c;
itemupdateList.add(ob);
}
if(itemupdateList.size() > 0 ){
update itemupdateList;
}
}
}
I am triggering data inserting on one object to other .while record is new it will create a new record in other object .record is old it will update the record in other object.
I have interface in such a way that user can insert the record and update the old record.
Error : i am doing action 3 batch update and 1 insert .
I am seeing 8 records are coming with 1 which i have inserted getting duplicated 4 times again.
I cant able to trace the error. Please help me regarding this .
thanks in advance