Salesforce Certified Application Architect

Friday 8 January 2016

Delete duplicate mobile number with different URN from Opportunity through batch apex

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



No comments:

Post a Comment

Salesforce Certified Application Architect & Certified Data Architecture and Management Designer Exam

How to pass Salesforce Certified Data Architecture and Management Designer Exam This exam was 1st architect exam for me. Its not that muc...