-3

I got this error while executing my batch job

Duplicate id in list: 0062800000AqfihAAB

Here is my batch job:

Global class OpportunityStageChanges implements Database.Batchable<sObject> {

Global Database.QueryLocator start(Database.BatchableContext BC) {
   //String query = 'Select id,Amount, (Select OldValue, NewValue From Histories) From Opportunity where stage <> NULL';
   //String query  = 'Select Id, CreatedById, CreatedDate, Field, NewValue, OldValue from OpportunityFieldHistory ';

    String query = 'Select Id,Name,Amount,LightiningEd__Stage_change_from_Prospecting__c,(Select ID, CreatedDate,OldValue, NewValue from Histories)from Opportunity';        
    return Database.getQueryLocator(query);
}
Global void execute(Database.BatchableContext BC, List<Opportunity> scope) {
  List<Opportunity> opplist = new List<Opportunity>();
  for(Opportunity opp : scope) {

    for(OpportunityFieldHistory opfh : opp.Histories){
          system.debug(opfh.oldValue+'----'+opfh.NewValue+'-----'+opfh.CreatedDate );
          if(opfh.OldValue == 'prospecting' && opfh.NewValue == 'Qualification '|| opfh.NewValue == 'Closed Won'){

              opp.LightiningEd__Stage_change_from_Prospecting__c = opfh.CreatedDate;
              System.debug('Date Value' + opp.LightiningEd__Stage_change_from_Prospecting__c);
              opplist.add(opp);

        }        
    }
    if(opplist.size()>0){
          update(opplist);
     }
   }

}
Global void finish(Database.BatchableContext BC) {

   }
}
Adrian Larson
  • 149,971
  • 38
  • 239
  • 420
subhash
  • 1
  • 5

1 Answers1

0

Just use a Map.

Map<Id, SObject> recordsToUpdate = new Map<Id, SObject>();
for (...)
{
    recordsToUpdate.put(someRecord.Id, someRecord);
}
update recordsToUpdate.values();

Note that you don't need to check if a list is empty before performing DML on it. Please also note that you should use the public access modifier rather than global unless you actually need the latter (you don't for batch jobs nor scheduled).

Adrian Larson
  • 149,971
  • 38
  • 239
  • 420