/*
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
CH0001 Modified the Logic to use Krishna Yerru Feb-8th-2018
MRC_Code__c value instead of
Campaign member ID and Removed
unwanted data
------------------------------------------------------------------------------------------------------------------
*/
global class InvocaCallLogMatchWithLeadBatch implements Database.Batchable<sobject>{
Public List<String> StatusVal = new List<String> {'New','No Match Found'};
Public String query='SELECT Id,Status__c,INVOCA_FOR_SF__calling_phone_number__c,INVOCA_FOR_SF__Customer_String_Value_7__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);
}
//CH0001-Modified below method
global void execute(Database.BatchableContext BC, List<INVOCA_FOR_SF__Invoca_Call_Log__c> invocaLeadMatch){
Id RsagLeadRt = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('RSAG New Account Lead').getRecordTypeId();
List<INVOCA_FOR_SF__Invoca_Call_Log__c> Calllogs = new List<INVOCA_FOR_SF__Invoca_Call_Log__c>();
Map<String,String> leadCalllogsMap = new Map<String,String>();
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>();
Map<ID,Set<Lead>> leadtoupdateMap = new Map<ID,Set<Lead>>();
set<Lead> Lead_upd= new set<Lead>();
if(invocaLeadMatch!= null){
for(INVOCA_FOR_SF__Invoca_Call_Log__c invoca: invocaLeadMatch){
Calllogs.add(invoca);
leadCalllogsMap.put(invoca.INVOCA_FOR_SF__Customer_String_Value_7__c,invoca.INVOCA_FOR_SF__calling_phone_number__c);
}
if(leadCalllogsMap.size()>0 && Calllogs.size()>0){
List<Lead> LeadMatches=[Select id,Phone,MRC_Code__c from Lead where RecordtypeID=:RsagLeadRt and MRC_Code__c in:leadCalllogsMap.Keyset()];
System.debug('LeadMatches is'+LeadMatches);
for(INVOCA_FOR_SF__Invoca_Call_Log__c calllog:Calllogs){
boolean flagval=false;
for(Lead le:LeadMatches){
if((le.MRC_Code__c!=null) && (le.Phone!=null && le.Phone!='') ){
String CampIDIs=le.MRC_Code__c;
String LeadPhone=le.Phone;
String ValLeadPhone=validatephone(LeadPhone);
String CalllogPhone=calllog.INVOCA_FOR_SF__calling_phone_number__c;
String VallogPhone=validatephone(CalllogPhone);
System.debug('Lead is'+le + 'Call log is'+calllog);
System.debug('CampIDIs on Lead'+CampIDIs +'CampIDIs on calllog'+calllog.INVOCA_FOR_SF__Customer_String_Value_7__c);
System.debug('Phone on Lead'+le.Phone +'CallerID on calllog'+calllog.INVOCA_FOR_SF__calling_phone_number__c);
System.debug('formated Phone on Lead'+ValLeadPhone +'formated CallerID on calllog'+VallogPhone);
if(calllog.INVOCA_FOR_SF__Customer_String_Value_7__c==CampIDIs &&VallogPhone==ValLeadPhone){
System.debug('matched Calllog is'+calllog+'matched Lead is'+le);
if(flagval==false){
System.debug('machedSection***1');
calllog.INVOCA_FOR_SF__Lead__c=le.id;
calllog.Status__c='Matched';
LeadupdStatusIDS.add(calllog.id);
LeadFound= true;
flagval=true;
//leadtoupdateMap.put(calllog.id,le);
Lead_upd.add(le);
}
else if(flagval==true){
System.debug('Multiple Record Matches');
calllog.INVOCA_FOR_SF__Lead__c=null;
calllog.Status__c='Multiple Record Matches';
LeadupdStatusIDS.add(calllog.id);
//leadtoupdateMap.put(calllog.id,le);
Lead_upd.add(le);
LeadFound= true;
}
}
else if(!LeadupdStatusIDS.contains(calllog.Id) || LeadFound==false){
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(calllog.id);
//Lead_upd.add(le);
LeadFound= true;
}
}
}
if(LeadFound==false){
calllog.Status__c='No Match Found';
System.debug('calllog with No match Lead are updated'+calllog);
}
if(Lead_upd.size()>0){
leadtoupdateMap.put(calllog.id,Lead_upd);
System.debug('updating leads with log map is are'+leadtoupdateMap);
}
UpdCalllogLeads.add(calllog);
}
}
}
try{
if(UpdCalllogLeads!=null && UpdCalllogLeads.size()>0){
Database.SaveResult [] UpdCalllogLeadsreslts = Database.update(UpdCalllogLeads, false);
}
if(leadtoupdateMap.size()>0){
LeadupdateWithlogData(leadtoupdateMap);
}
}
catch(exception e){
System.debug('There is problem in updating matching Call log record associated lead records.'+e);
}
}
global void finish(Database.BatchableContext BC){
}
public String validatephone(String Phoneis) {
String phone=Phoneis;
if(phone.contains(' ')||phone.contains('-')|| phone.contains('(') || phone.contains(')')){
string Updphoneis=phone;
String Updphone=phone;
if(Updphoneis.contains('-')){
String Updphone1=Updphoneis.replace('-','');
Updphone=Updphone1;
System.debug('Updated Phone is1 '+Updphone);
}
if(Updphone.contains('(')){
String Updphone2=Updphone.replace('(','');
Updphone=Updphone2;
System.debug('Updated Phone is 2'+Updphone);
}
if(Updphone.contains(')')){
String Updphone3=Updphone.replace(')','');
Updphone=Updphone3;
System.debug('Updated Phone is3 '+Updphone);
}
if(Updphone.contains(' ')){
String Updphone4=Updphone.replace(' ','');
Updphone=Updphone4;
System.debug('Updated Phone is4 '+Updphone);
}
System.debug('Updated Phone is '+Updphone);
phone=Updphone;
System.debug('The final returning Updated Phone is '+phone);
}
System.debug('The final returning Updated Phone is '+phone);
return phone;
}
public Void LeadupdateWithlogData( Map<ID,Set<lead>> Leadtoupdate) {
List<Lead> ledsupdates=New list<Lead>();
System.debug('The Leads updated Map is '+Leadtoupdate);
if(Leadtoupdate.size()>0){
for(INVOCA_FOR_SF__Invoca_Call_Log__c logdata:[SELECT Id,Status__c,INVOCA_FOR_SF__calling_phone_number__c,
INVOCA_FOR_SF__Customer_String_Value_7__c,INVOCA_FOR_SF__Lead__c,
INVOCA_FOR_SF__Lead__r.Phone FROM INVOCA_FOR_SF__Invoca_Call_Log__c
where ID in:Leadtoupdate.Keyset()]){
for(Set<Lead> leupdlist:Leadtoupdate.Values()){
for(Lead leupd:leupdlist){
System.debug('***Contains '+Leadtoupdate.get(logdata.id));
if(Leadtoupdate.get(logdata.id).contains(leupd)){
//if(Leadtoupdate.get(logdata.id).Id==leupd.id){
leupd.Title= logdata.INVOCA_FOR_SF__calling_phone_number__c;
leupd.X2nd_Contact_Name__c= logdata.INVOCA_FOR_SF__Customer_String_Value_7__c;
ledsupdates.add(leupd);
}
}
}
}
}
//Updating Leads
try{
if(ledsupdates!=null && ledsupdates.size()>0){
Database.SaveResult [] UpdLeadsreslts = Database.update(ledsupdates, false);
}
}
catch(exception e){
System.debug('There is problem in updating matching Lead record associated log records.'+e);
}
//End
}
}
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
CH0001 Modified the Logic to use Krishna Yerru Feb-8th-2018
MRC_Code__c value instead of
Campaign member ID and Removed
unwanted data
------------------------------------------------------------------------------------------------------------------
*/
global class InvocaCallLogMatchWithLeadBatch implements Database.Batchable<sobject>{
Public List<String> StatusVal = new List<String> {'New','No Match Found'};
Public String query='SELECT Id,Status__c,INVOCA_FOR_SF__calling_phone_number__c,INVOCA_FOR_SF__Customer_String_Value_7__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);
}
//CH0001-Modified below method
global void execute(Database.BatchableContext BC, List<INVOCA_FOR_SF__Invoca_Call_Log__c> invocaLeadMatch){
Id RsagLeadRt = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('RSAG New Account Lead').getRecordTypeId();
List<INVOCA_FOR_SF__Invoca_Call_Log__c> Calllogs = new List<INVOCA_FOR_SF__Invoca_Call_Log__c>();
Map<String,String> leadCalllogsMap = new Map<String,String>();
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>();
Map<ID,Set<Lead>> leadtoupdateMap = new Map<ID,Set<Lead>>();
set<Lead> Lead_upd= new set<Lead>();
if(invocaLeadMatch!= null){
for(INVOCA_FOR_SF__Invoca_Call_Log__c invoca: invocaLeadMatch){
Calllogs.add(invoca);
leadCalllogsMap.put(invoca.INVOCA_FOR_SF__Customer_String_Value_7__c,invoca.INVOCA_FOR_SF__calling_phone_number__c);
}
if(leadCalllogsMap.size()>0 && Calllogs.size()>0){
List<Lead> LeadMatches=[Select id,Phone,MRC_Code__c from Lead where RecordtypeID=:RsagLeadRt and MRC_Code__c in:leadCalllogsMap.Keyset()];
System.debug('LeadMatches is'+LeadMatches);
for(INVOCA_FOR_SF__Invoca_Call_Log__c calllog:Calllogs){
boolean flagval=false;
for(Lead le:LeadMatches){
if((le.MRC_Code__c!=null) && (le.Phone!=null && le.Phone!='') ){
String CampIDIs=le.MRC_Code__c;
String LeadPhone=le.Phone;
String ValLeadPhone=validatephone(LeadPhone);
String CalllogPhone=calllog.INVOCA_FOR_SF__calling_phone_number__c;
String VallogPhone=validatephone(CalllogPhone);
System.debug('Lead is'+le + 'Call log is'+calllog);
System.debug('CampIDIs on Lead'+CampIDIs +'CampIDIs on calllog'+calllog.INVOCA_FOR_SF__Customer_String_Value_7__c);
System.debug('Phone on Lead'+le.Phone +'CallerID on calllog'+calllog.INVOCA_FOR_SF__calling_phone_number__c);
System.debug('formated Phone on Lead'+ValLeadPhone +'formated CallerID on calllog'+VallogPhone);
if(calllog.INVOCA_FOR_SF__Customer_String_Value_7__c==CampIDIs &&VallogPhone==ValLeadPhone){
System.debug('matched Calllog is'+calllog+'matched Lead is'+le);
if(flagval==false){
System.debug('machedSection***1');
calllog.INVOCA_FOR_SF__Lead__c=le.id;
calllog.Status__c='Matched';
LeadupdStatusIDS.add(calllog.id);
LeadFound= true;
flagval=true;
//leadtoupdateMap.put(calllog.id,le);
Lead_upd.add(le);
}
else if(flagval==true){
System.debug('Multiple Record Matches');
calllog.INVOCA_FOR_SF__Lead__c=null;
calllog.Status__c='Multiple Record Matches';
LeadupdStatusIDS.add(calllog.id);
//leadtoupdateMap.put(calllog.id,le);
Lead_upd.add(le);
LeadFound= true;
}
}
else if(!LeadupdStatusIDS.contains(calllog.Id) || LeadFound==false){
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(calllog.id);
//Lead_upd.add(le);
LeadFound= true;
}
}
}
if(LeadFound==false){
calllog.Status__c='No Match Found';
System.debug('calllog with No match Lead are updated'+calllog);
}
if(Lead_upd.size()>0){
leadtoupdateMap.put(calllog.id,Lead_upd);
System.debug('updating leads with log map is are'+leadtoupdateMap);
}
UpdCalllogLeads.add(calllog);
}
}
}
try{
if(UpdCalllogLeads!=null && UpdCalllogLeads.size()>0){
Database.SaveResult [] UpdCalllogLeadsreslts = Database.update(UpdCalllogLeads, false);
}
if(leadtoupdateMap.size()>0){
LeadupdateWithlogData(leadtoupdateMap);
}
}
catch(exception e){
System.debug('There is problem in updating matching Call log record associated lead records.'+e);
}
}
global void finish(Database.BatchableContext BC){
}
public String validatephone(String Phoneis) {
String phone=Phoneis;
if(phone.contains(' ')||phone.contains('-')|| phone.contains('(') || phone.contains(')')){
string Updphoneis=phone;
String Updphone=phone;
if(Updphoneis.contains('-')){
String Updphone1=Updphoneis.replace('-','');
Updphone=Updphone1;
System.debug('Updated Phone is1 '+Updphone);
}
if(Updphone.contains('(')){
String Updphone2=Updphone.replace('(','');
Updphone=Updphone2;
System.debug('Updated Phone is 2'+Updphone);
}
if(Updphone.contains(')')){
String Updphone3=Updphone.replace(')','');
Updphone=Updphone3;
System.debug('Updated Phone is3 '+Updphone);
}
if(Updphone.contains(' ')){
String Updphone4=Updphone.replace(' ','');
Updphone=Updphone4;
System.debug('Updated Phone is4 '+Updphone);
}
System.debug('Updated Phone is '+Updphone);
phone=Updphone;
System.debug('The final returning Updated Phone is '+phone);
}
System.debug('The final returning Updated Phone is '+phone);
return phone;
}
public Void LeadupdateWithlogData( Map<ID,Set<lead>> Leadtoupdate) {
List<Lead> ledsupdates=New list<Lead>();
System.debug('The Leads updated Map is '+Leadtoupdate);
if(Leadtoupdate.size()>0){
for(INVOCA_FOR_SF__Invoca_Call_Log__c logdata:[SELECT Id,Status__c,INVOCA_FOR_SF__calling_phone_number__c,
INVOCA_FOR_SF__Customer_String_Value_7__c,INVOCA_FOR_SF__Lead__c,
INVOCA_FOR_SF__Lead__r.Phone FROM INVOCA_FOR_SF__Invoca_Call_Log__c
where ID in:Leadtoupdate.Keyset()]){
for(Set<Lead> leupdlist:Leadtoupdate.Values()){
for(Lead leupd:leupdlist){
System.debug('***Contains '+Leadtoupdate.get(logdata.id));
if(Leadtoupdate.get(logdata.id).contains(leupd)){
//if(Leadtoupdate.get(logdata.id).Id==leupd.id){
leupd.Title= logdata.INVOCA_FOR_SF__calling_phone_number__c;
leupd.X2nd_Contact_Name__c= logdata.INVOCA_FOR_SF__Customer_String_Value_7__c;
ledsupdates.add(leupd);
}
}
}
}
}
//Updating Leads
try{
if(ledsupdates!=null && ledsupdates.size()>0){
Database.SaveResult [] UpdLeadsreslts = Database.update(ledsupdates, false);
}
}
catch(exception e){
System.debug('There is problem in updating matching Lead record associated log records.'+e);
}
//End
}
}
No comments:
Post a Comment