Monday, 4 February 2019

batchclassinvoca

/* /*     Class Name:InvocaCallLogMatchWithLeadBatch     Developer Name:Krishna Nagendra Rao.Yerru     Description:This batch class that will query and :         1.          2.          3.          4.              Created Date:   04-Feb-2019     Test Class Name:      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,Lead_Match_Status__c,INVOCA_FOR_SF__calling_phone_number__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 Lead_Match_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<ID> NoleadCalllogs = new List<ID>();         Map<INVOCA_FOR_SF__Invoca_Call_Log__c,String> NoleadCalllogsMap = new Map<INVOCA_FOR_SF__Invoca_Call_Log__c,String>();         List<ID> leadCalllogs = new List<ID>();         Map<INVOCA_FOR_SF__Invoca_Call_Log__c,String> leadCalllogsMap = new Map<INVOCA_FOR_SF__Invoca_Call_Log__c,String>();         //Map<Id,String> lstLeads = new Map<Id,String>();         List<INVOCA_FOR_SF__Invoca_Call_Log__c> UpdCalllogLeads=New List<INVOCA_FOR_SF__Invoca_Call_Log__c>();         List<INVOCA_FOR_SF__Invoca_Call_Log__c> UpdateCallLogStatusrecs=New List<INVOCA_FOR_SF__Invoca_Call_Log__c>();         List<INVOCA_FOR_SF__Invoca_Call_Log__c> UpdCalllogStatus=New List<INVOCA_FOR_SF__Invoca_Call_Log__c>();         if(invocaLeadMatch!= null){             for(INVOCA_FOR_SF__Invoca_Call_Log__c invoca: invocaLeadMatch){                 if(invoca.INVOCA_FOR_SF__Lead__c==null){                     NoleadCalllogs.add(invoca.id);                     NoleadCalllogsMap.put(invoca,invoca.INVOCA_FOR_SF__calling_phone_number__c);                 }               }                                if(NoleadCalllogsMap.size()>0){                    List<Lead> LeadMatches=[Select id,Phone,(SELECT Id, CampaignId, LeadId FROM CampaignMembers) from Lead where Phone in:NoleadCalllogsMap.Values()];                     for(Lead le:LeadMatches){                         for(INVOCA_FOR_SF__Invoca_Call_Log__c calllog:NoleadCalllogsMap.Keyset()){                             //if(calllog.CampaignId==le.CampaignMembers.CampaignId[0] && calllog.INVOCA_FOR_SF__calling_phone_number__c==le.Phone){                             //if(calllog.INVOCA_FOR_SF__advertiser_campaign_name__c==le.CampaignMembers.CampaignId[0] && calllog.INVOCA_FOR_SF__calling_phone_number__c==le.Phone){                             if(calllog.INVOCA_FOR_SF__calling_phone_number__c==le.Phone){                                 calllog.INVOCA_FOR_SF__Lead__c=le.id;                                 UpdCalllogLeads.add(calllog);                             }                         }                                }                                      }                                try{                 if(UpdCalllogLeads!=null && UpdCalllogLeads.size()>0){                         Database.SaveResult [] UpdCalllogLeadsreslts = Database.update(UpdCalllogLeads, false);                         //UpdateCallLogStatusrecs.add(UpdCalllogLeads);                     }                 }                      catch(exception e){                 System.debug('There is problem in updating matching Call log record associated lead records.'+e);             }                          //2nd part                 for(INVOCA_FOR_SF__Invoca_Call_Log__c invocalog: invocaLeadMatch){                     if(invocalog.INVOCA_FOR_SF__Lead__c!=null){                         leadCalllogs.add(invocalog.id);                         leadCalllogsMap.Put(invocalog,invocalog.INVOCA_FOR_SF__calling_phone_number__c);                     }                   }                                if(leadCalllogsMap.size()>0){                    List<Lead> CallLogmatchesLead=[Select id,Phone,(SELECT Id, CampaignId, LeadId FROM CampaignMembers) from Lead where Phone in:leadCalllogsMap.Values()];                     for(Lead le:CallLogmatchesLead){                         for(INVOCA_FOR_SF__Invoca_Call_Log__c calllog:leadCalllogsMap.Keyset()){                             //Need to Correct below                             if(calllog.INVOCA_FOR_SF__calling_phone_number__c==le.Phone){                                 if(calllog.Status__c=='New'){                                     if(calllog.Status__c!='Matched'){                                         calllog.Status__c='Matched';                                         UpdCalllogStatus.add(calllog);                                     }                                 }                             }                         }                                }                                      }                                try{                 if(UpdCalllogStatus!=null && UpdCalllogStatus.size()>0){                         Database.SaveResult [] UpdCalllogLeadsreslts = Database.update(UpdCalllogStatus, false);                     }                 }                      catch(exception e){                 System.debug('There is problem in updating matching Call log record associated Status.'+e);             }                  }     }     global void finish(Database.BatchableContext BC){     } }//Main

No comments:

Post a Comment