+ Start a Discussion
omm ommomm omm 

hi guys i had issue with a trigger

trigger amountnegative on test1__c (after insert, after update) 
{
set<id> stid= new set<id>();
list <opportunity>opp= new list<opportunity>();
 for(test1__c t:[select id , name ,amount__c, trantortest__oppo__r.id from test1__c ])
 {
 stid.add(t.trantortest__oppo__r.id);
 system.debug('-------------------'+stid);
  if(t.amount__c < 0)
  {
  for(opportunity op: [select id, name, status__c,trantortest__Knightdate__c, (select id, name from test__r) from opportunity where ID IN : stid ])
  {
  system.debug('opppp----------------'+op);
   op.status__c='knight';
      op.trantortest__Knightdate__c=system.now();
   opp.add(op);
   system.debug('oppppppppppppppppppppppppppppppppppppp'+opp);
  }
  update opp;
  }
 }/*
    list<test1__c> t12= new list<test1__c>();
    for(test1__c t:trigger.new)
    {
     t12.add(t);   
    }
    amountnegative.hitme(t12);*/
}
I am getting this error execution of AfterUpdate caused by: System.ListException: Duplicate id in list: 00628000004R54cAAC: Trigger.trantortest.amountnegative: line 19, column 1
when i had checked  my debug logs
this lines are  getting executed repetedly 8 13 17
Best Answer chosen by omm omm
KaranrajKaranraj
Here is the update code
trigger amountnegative on test1__c (after insert, after update) 
{
set<id> stid= new set<id>();
list <opportunity> opp= new list<opportunity>();
 for(test1__c t:[select id , name ,amount__c, trantortest__oppo__r.id from test1__c where Id IN: trigger.New ])
 {
  if(t.amount__c < 0)
  {
    stid.add(t.trantortest__oppo__r.id);
  }
}
 
  for(opportunity op: [select id, name, status__c,trantortest__Knightdate__c, (select id, name from test__r) from opportunity where ID IN : stid ])
  {

   op.status__c='knight';
   op.trantortest__Knightdate__c=system.now();
   opp.add(op);
 
  }
  update opp;
  
 }

You should not use SOQL query statement inside the for loop and your code will execute and update for all the test1 object not only for the new created or updated test1 record