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