Tuesday, 5 February 2019

invbatch2

/*     Class Name:InvocaCallLogMatchWithLeadBatch     Developer Name:Krishna Nagendra Rao.Yerru               Created Date:   04-Feb-2019     Test Class Name: InvocaCallLogMatchWithLeadBatch_Test     Project Name: SP-3530:Create Batch Process to match Invoca Call Logs with Leads ------------------------------------------------------------------------------------------------------------------     CH#             Description                             Developer                       Modified Date ------------------------------------------------------------------------------------------------------------------ */ global class InvocaCallLogMatchWithLeadBatch implements Database.Batchable<sobject>{        Public List<String> StatusVal = new List<String> {'New'};     Public String query='SELECT Id,Status__c,INVOCA_FOR_SF__calling_phone_number__c,INVOCA_FOR_SF__advertiser_campaign_name__c,INVOCA_FOR_SF__Lead__c, INVOCA_FOR_SF__Lead__r.Phone FROM INVOCA_FOR_SF__Invoca_Call_Log__c where INVOCA_FOR_SF__calling_phone_number__c!=null and Status__c in:StatusVal';       global Database.Querylocator start(Database.BatchableContext BC){         return Database.getQueryLocator(query);     }       global void execute(Database.BatchableContext BC,  List<INVOCA_FOR_SF__Invoca_Call_Log__c> invocaLeadMatch){          List<INVOCA_FOR_SF__Invoca_Call_Log__c> leadinCalllogs = new List<INVOCA_FOR_SF__Invoca_Call_Log__c>();         Map<INVOCA_FOR_SF__Invoca_Call_Log__c,String> leadCalllogsMap = new Map<INVOCA_FOR_SF__Invoca_Call_Log__c,String>();         List<ID> leadCalllogs = new List<ID>();         list<ID> LeadupdStatusIDS= new list<ID>();         boolean LeadFound=false;         List<INVOCA_FOR_SF__Invoca_Call_Log__c> UpdCalllogLeads=New List<INVOCA_FOR_SF__Invoca_Call_Log__c>();         list<ID> updcalllogids= new list<ID>();                  if(invocaLeadMatch!= null){                 for(INVOCA_FOR_SF__Invoca_Call_Log__c invoca: invocaLeadMatch){                     leadinCalllogs.add(invoca);                     leadCalllogsMap.put(invoca,invoca.INVOCA_FOR_SF__calling_phone_number__c);                   }                                if(leadCalllogsMap.size()>0){                     List<Lead> LeadMatches=[Select id,Phone,(SELECT Id,CampaignID,Campaign.name,LeadId FROM CampaignMembers) from Lead where Phone in:leadCalllogsMap.Values()];                     System.debug('LeadMatches is'+LeadMatches);                     for(Lead le:LeadMatches){                                                    for(INVOCA_FOR_SF__Invoca_Call_Log__c calllog:leadCalllogsMap.Keyset()){                             if(le.CampaignMembers.size()>0){                                 CampaignMember Campname=le.CampaignMembers[0];                                 String CampnameIs=Campname.Campaign.name;                                 System.debug('Lead is'+le);                                 System.debug('Call log is'+calllog);                                 System.debug('CampnameIs on Lead'+CampnameIs +'CampnameIs on calllog'+calllog.INVOCA_FOR_SF__advertiser_campaign_name__c);                                 System.debug('Phone on Lead'+le.Phone +'CallerID on calllog'+calllog.INVOCA_FOR_SF__calling_phone_number__c);                                 if(calllog.INVOCA_FOR_SF__advertiser_campaign_name__c==CampnameIs && calllog.INVOCA_FOR_SF__calling_phone_number__c==le.Phone){                                     System.debug('matched Calllog is'+calllog);                                         if(!LeadupdStatusIDS.contains(le.Id)){                                             if(calllog.INVOCA_FOR_SF__Lead__c==null){                                                 calllog.INVOCA_FOR_SF__Lead__c=le.id;                                                 calllog.Status__c='Matched';                                                 LeadupdStatusIDS.add(le.id);                                                 LeadFound= true;                                             }                                             else if(calllog.INVOCA_FOR_SF__Lead__c!=null){                                                 calllog.Status__c='Matched';                                                 LeadupdStatusIDS.add(le.id);                                                 LeadFound= true;                                             }                                         }                                          else if(LeadupdStatusIDS.contains(le.Id)){                                             calllog.Status__c='Multiple Record Matches';                                             //LeadupdStatusIDS.add(le.id);                                           }                                 }                                 else if(!LeadupdStatusIDS.contains(le.Id) ){                                     System.debug('calllog with No match Lead are'+calllog);                                     calllog.Status__c='No Match Found';                                     System.debug('calllog with No match Lead are updated'+calllog);                                     LeadupdStatusIDS.add(le.id);                                     LeadFound= true;                                 }                                 UpdCalllogLeads.add(calllog);                                 updcalllogids.add(calllog.id);                           }                         }                     }                            }                 System.debug('leadinCalllogs are'+leadinCalllogs);                 System.debug('UpdCalllogLeads are'+UpdCalllogLeads);                 System.debug('LeadupdStatusIDS are'+LeadupdStatusIDS);                 System.debug('updcalllogids are'+updcalllogids);                 for(INVOCA_FOR_SF__Invoca_Call_Log__c noleadlogs:leadinCalllogs){                     System.debug('noleadlogs are'+noleadlogs);                     System.debug('noleadlogsid are'+noleadlogs.id);                     System.debug('updcalllogids are'+updcalllogids);                     System.debug('Conatin is'+updcalllogids.contains(noleadlogs.id));                     if(!updcalllogids.contains(noleadlogs.id)){                         noleadlogs.Status__c='No Match Found';                         UpdCalllogLeads.add(noleadlogs);                         updcalllogids.add(noleadlogs.id);                                            }                 }             }                                            try{                 if(UpdCalllogLeads!=null && UpdCalllogLeads.size()>0){                         Database.SaveResult [] UpdCalllogLeadsreslts = Database.update(UpdCalllogLeads, false);                     }                 }                      catch(exception e){                 System.debug('There is problem in updating matching Call log record associated lead records.'+e);             }                      }          global void finish(Database.BatchableContext BC){     } }

No comments:

Post a Comment