global class Batch_OpportunityBatch implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator('SELECT id,Name,Mobile,URN FROM Opportunity WHERE URN LIKE \'' + 7 + '%\' order by CreatedDate desc') ;
}
global void execute(Database.BatchableContext BC, List<Opportunity> scope){
System.debug('scope@@@@@@@@'+scope);
//Map<String, List<Opportunity>> mapMobileKeyWithOpportunitys = new Map<String, List<Opportunity>>();
Map<String, String> mapMobileKeyWithUniqueOpportunitys = new Map<String, String> ();
List <Opportunity> listOpportunitytoDelete = new list<Opportunity>();
for(Opportunity s : scope){
if(!mapMobileKeyWithUniqueOpportunitys.containsKey(s.Mobile)){
mapMobileKeyWithUniqueOpportunitys.put(s.Mobile,s.URN);
}
}
System.debug('mapMobileKeyWithUniqueOpportunitys@@@@-->'+mapMobileKeyWithUniqueOpportunitys);
if(mapMobileKeyWithUniqueOpportunitys.size()>0 && !mapMobileKeyWithUniqueOpportunitys.isempty()){
listOpportunitytoDelete = [select id, Name from Opportunity where URN NOT IN : mapMobileKeyWithUniqueOpportunitys.values() and URN LIKE \'' + 7 + '%\'];
}
System.debug('$$$$$$$$'+listOpportunitytoDelete.size());
if(listOpportunitytoDelete.size()>0){
Delete listOpportunitytoDelete ;
}
}
global void finish(Database.BatchableContext BC){
}
}