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;
         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){
          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){

            for(batch__c b:trigger.new){ 
            order_batch__c ob=ids.get(b.id);
            ob.Quantity_Ordered__c = b.Asked_Quantity_Number__c;
          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

2 Answers 2


As PepeFloyd says there is an upsert database call available that inserts if the ID is not set and updates if it is set. Also your code looks like it is inserting before checking that there is an existing order_batch__c that should be updated and so is likely to result in extra records. The trigger context values often save you from having to work out sets of ID values.

Try this code (may contain typos):

trigger InsertOrderBatch on batch__c (after update, after insert) { 

    // Find the existing (0 or 1) order_batch__c that reference batch__c
    Map<id, order_batch__c> m = new Map<id, order_batch__c>();
    for (order_batch__c ob : [
            select id, batch__c
            from order_batch__c
            where batch__c in :Trigger.newMap.keySet()
            ]) {
        m.put(ob.batch__c, ob);

    // Insert or update the order_batch__c
    List<order_batch__c> upserts = new List<order_batch__c>();
    for (batch__c b : trigger.new) {
        // Get record to update
        order_batch__c ob = m.get(b.id);
        if (ob == null) {
            // If no record to update, add a record to be inserted
            ob = new order_batch__c(Batch__c = 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;
    upsert upserts;

Note that this assumes that there is never more than 1 order_batch__c referencing a batch__c.


Put the records in the same list and use UPSERT instead.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .