Created
June 26, 2019 17:21
-
-
Save mhamzas/810ae1c67eed84241df1c41d357ea56f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CJA_RMtoSFAccCntcts { | |
//First part | |
List<Roadmunk_User__c> allRMCntLst = new List<Roadmunk_User__c>(); | |
List<Roadmunk_Account__c> allRMAccLst = new List<Roadmunk_Account__c>(); | |
//Excluded domains set | |
Set<String> excludDomains = new Set<String>{'gmail.com','hotmail.com', 'yahoo.com'}; | |
List<Account> accToUpsert = new List<Account>(); | |
public void parseAndUpsertAUDs (List<Roadmunk_User__c> allRMCntLst) | |
{ | |
//allRMCntLst = [select User_ID__c, First_Name__c, Last_Name__c, AccountID__c, Account_Role__c, Job_Title__c, Last_Login__c, Member_Number__c, Email__c from Roadmunk_User__c where Last_Name__c <> '' limit 500];//limit 50 offset 700 | |
set<String> accExtId = new set<String>(); | |
for(Roadmunk_User__c rmUser : allRMCntLst) | |
{ | |
accExtId.add(rmUser.AccountID__c); | |
} | |
allRMAccLst = [select AccountID__c, Company_Name__c, Company_Size__c, chargebeeCustomerID__c, chargebeeSubscriptionID__c, Total_Collaborators__c, Total_Reviewers__c, Unlimited_Free_Reviewers__c, Signup_Date__c, Bill_With__c, Deployment__c, Package__c, Package_Expiry__c, Billing_Interval__c, AddOn_Package__c, Trial_Extended__c, Free_Collaborators__c, Free_Reviewers__c, Became_Paid_Customer__c, Billing_Email__c from Roadmunk_Account__c where AccountID__c IN : accExtId AND Company_Name__c <> '']; | |
System.debug('SYA :: Size :: '+allRMAccLst.size() + ' :: allRMAccLst :: '+allRMAccLst); | |
List<Contact> cntToUpsert = new List<Contact>(); | |
Map<String,ID> accIDMap = new Map<String, ID>(); | |
Map<String,String> accIDBillEmailMap = new Map<String, String>(); | |
//Accounts Insertion | |
if(allRMAccLst.size() > 0) | |
{ | |
for(Roadmunk_Account__c rmAcc : allRMAccLst) | |
{ | |
Account acc = new Account(RM_Account_ID__c = rmAcc.AccountID__c, | |
Name = rmAcc.Company_Name__c, | |
Company_Size__c = rmAcc.Company_Size__c, | |
chargebeeCustomerID__c = rmAcc.chargebeeCustomerID__c, | |
chargebeeSubscriptionID__c = rmAcc.chargebeeSubscriptionID__c, | |
Total_Collaborators__c= rmAcc.Total_Collaborators__c, | |
Total_Reviewers__c = rmAcc.Total_Reviewers__c, | |
Unlimited_Free_Reviewers__c = rmAcc.Unlimited_Free_Reviewers__c, | |
Signup_Date__c = rmAcc.Signup_Date__c, | |
Bill_With__c = rmAcc.Bill_With__c, | |
Deployment__c = rmAcc.Deployment__c, | |
Package__c = rmAcc.Package__c, | |
Package_Expiry__c = rmAcc.Package_Expiry__c, | |
RM_Billing_Interval__c = rmAcc.Billing_Interval__c, | |
Add_On_Package__c = rmAcc.AddOn_Package__c, | |
Trial_Extended__c = rmAcc.Trial_Extended__c, | |
Free_Collaborators__c = rmAcc.Free_Collaborators__c, | |
Free_Reviewers__c = rmAcc.Free_Reviewers__c, | |
Became_Paid_Customer__c = rmAcc.Became_Paid_Customer__c, | |
Billing_Email__c = rmAcc.Billing_Email__c, | |
Billing_Email_Domain__c = rmAcc.Billing_Email__c.split('@').get(1).trim().tolowercase()); | |
accToUpsert.add(acc); | |
} | |
} | |
if(accToUpsert.size() > 0) | |
{ | |
try | |
{ | |
upsert accToUpsert RM_Account_ID__c; | |
}catch(exception ex) | |
{ | |
system.debug('SYA::' + ex); | |
} | |
for(Account acc: accToUpsert) | |
{ | |
accIDMap.put(acc.RM_Account_ID__c, acc.id); | |
//accIDBillEmailMap.put(acc.RM_Account_ID__c, acc.Billing_Email_Domain__c); | |
} | |
} | |
Map<String,Set<String>> accToEmailDomainMap = new Map<String,Set<String>>(); | |
Map<String, AUD__c> audExtToRMMap = new Map<String, AUD__c>(); | |
//Users Insertion into contacts | |
if(allRMCntLst.size() > 0) | |
{ | |
for(Roadmunk_User__c rmCnt : allRMCntLst) | |
{ | |
String emailDomain = rmCnt.Email__c.split('@').get(1).trim().tolowercase(); | |
Contact cnt = new Contact(RM_User_ID__c = rmCnt.User_ID__c, | |
FirstName = rmCnt.First_Name__c, | |
LastName = rmCnt.Last_Name__c, | |
Email = rmCnt.Email__c, | |
AccountId = (accIDMap.get(rmCnt.AccountID__c) <> null) ? accIDMap.get(rmCnt.AccountID__c) : null, | |
AccountID__c = rmCnt.AccountID__c, | |
Account_Role__c = rmCnt.Account_Role__c, | |
Job_Title__c = rmCnt.Job_Title__c, | |
Last_Login__c = rmCnt.Last_Login__c, | |
Account_Member_Number__c = rmCnt.Member_Number__c, | |
Email_domain__c = emailDomain | |
); | |
cntToUpsert.add(cnt); | |
//collecting domains against Account IDs | |
if(accIDMap.get(rmCnt.AccountID__c) <> null) | |
{ | |
if(excludDomains.contains(emailDomain) == false) | |
{ | |
audExtToRMMap.put(accIDMap.get(rmCnt.AccountID__c)+'-'+emailDomain , new AUD__c(RM_Account_ID__c = rmCnt.AccountID__c, RM_User_ID__c = rmCnt.User_ID__c)); | |
if(accToEmailDomainMap.get(accIDMap.get(rmCnt.AccountID__c)) <> null) | |
{ | |
Set<String> domains = accToEmailDomainMap.get(accIDMap.get(rmCnt.AccountID__c)); | |
domains.add(emailDomain); | |
accToEmailDomainMap.put(accIDMap.get(rmCnt.AccountID__c), domains); | |
//domains.add(accIDBillEmailMap.get(rmCnt.AccountID__c)); | |
}else | |
{ | |
Set<String> domains = new Set<String>(); | |
domains.add(emailDomain); | |
accToEmailDomainMap.put(accIDMap.get(rmCnt.AccountID__c), domains); | |
//domains.add(accIDBillEmailMap.get(rmCnt.AccountID__c)); | |
} | |
} | |
} | |
} | |
} | |
//Inserting Contacts | |
if(cntToUpsert.size() > 0) | |
{ | |
try | |
{ | |
upsert cntToUpsert RM_User_ID__c; | |
}catch(exception ex) | |
{ | |
system.debug('SYA::' + ex); | |
} | |
} | |
//Inserting AUDs | |
if(accToEmailDomainMap.size() > 0) | |
{ | |
List<AUD__c> InsrtAUD = new List<AUD__c>(); | |
for(String accID : accToEmailDomainMap.keyset()) | |
{ | |
if(accToEmailDomainMap.get(accID).size() > 0) | |
{ | |
for(String domain: accToEmailDomainMap.get(accID)) | |
{ | |
InsrtAUD.add(new AUD__c(Account_With_Domain__c = accID +'-'+ domain, | |
Account__c = accID, | |
Email_Domain__c = domain, | |
RM_User_ID__c = (audExtToRMMap.get(accID +'-'+ domain) <> null) ? audExtToRMMap.get(accID +'-'+ domain).RM_User_ID__c : null, | |
RM_Account_ID__c = (audExtToRMMap.get(accID +'-'+ domain) <> null) ? audExtToRMMap.get(accID +'-'+ domain).RM_Account_ID__c : null)); | |
} | |
} | |
} | |
if(InsrtAUD.size() > 0) | |
{ | |
try | |
{ | |
upsert InsrtAUD Account_With_Domain__c; | |
}catch(exception ex) | |
{ | |
system.debug('SYA::' + ex); | |
} | |
} | |
} | |
} | |
//Second part | |
Map<Id, Account> allAccMap = new Map<Id, Account>(); | |
List<AUD__c> allAUDLst = new List<AUD__c>(); | |
Map<String, List<Account>> domainAccsMap = new Map<String, List<Account>>(); | |
Map<id, List<DQS_Line__c>> DQSToDQSLineMap = new Map<id, List<DQS_Line__c>>(); | |
Map<String, List<Account>> allBillDomainAccMap = new Map<String, List<Account>>(); | |
Map<string, id> domainToDQSIDMap = new Map<string, id>(); | |
Map<string, id> accNameToDQSIDMap = new Map<string, id>(); | |
List<AUD__c> DQSToInsert = new List<AUD__c>(); | |
Map<String, Set<String>> allAccIDtoDomainsMap = new Map<String, Set<String>>(); | |
Map<String, List<String>> allAccIDtoDomainsListMap = new Map<String, List<String>>(); | |
Set<Id> accIDs = new Set<Id>(); | |
Set<Id> accParentSet = new Set<Id>(); | |
//List of all upserted accounts | |
//accToUpsert | |
public void parseAUDsAndUpsertDQSs(List<AUD__c> allAUDLst)// List<AUD__c> allAUDLst // List<Account> accsLst | |
{ | |
//Accounts which have been newly created ::temporary Query | |
accToUpsert = [select id, Billing_Email__c, Billing_Email_Domain__c from Account limit 5000];// where name like '%CJA%' | |
allAUDLst = [select id, name, Account_With_Domain__c, Email_Domain__c, Account__c, RM_User_ID__c, RM_Account_ID__c, Account__r.Billing_Email__c from AUD__c]; | |
// Real Part - Hamza | |
//// Making a list of Domain Ids from AUD List : AllDomains_AUD | |
// | |
////// Making a list of Billing email domain from AUD. | |
//// Add where clause with these lists in the below query. | |
//// Example : from AUD where account__r.Billing_Email_Domain__c := LISTOF_BILL_DOMAIN OR Domains =:AllDomains_AUD | |
for(Account acc: [select id, name, Billing_Email__c, Billing_Email_Domain__c, ParentId from Account where Billing_Email_Domain__c <> null])//accsLst)// AND name like '%CJA%' | |
/*where id = '0010r000009HiDoAAK' or id = '0010r000009HiDfAAK' | |
OR id= '0010r000009HiDeAAK' OR id = '0010r000009HiDZAA0'])*/ | |
{ | |
//Acc ID to Acc Map | |
allAccMap.put(acc.id, acc); | |
if(acc.ParentId <> null) | |
accParentSet.add(acc.ParentId); | |
} | |
/*system.debug('#allBillDomainAccMap='+allBillDomainAccMap); | |
//TODO::Uncomment the below | |
//Exact Match on Billing Email Domains | |
if(allBillDomainAccMap.size() > 0)// && false | |
exactMatchAccounts(allBillDomainAccMap);*/ | |
for(AUD__c aud : allAUDLst) | |
{ | |
//All Domain to Account Map from AUD | |
//NOT BEING USED | |
/*if(domainAccsMap.get(aud.Account__c) <> null) | |
{ | |
List<Account> accLst = domainAccsMap.get(aud.Account__c); | |
accLst.add(allAccMap.get(aud.Account__c)); | |
domainAccsMap.put(aud.Email_Domain__c, accLst); | |
} | |
else | |
{ | |
List<Account> accLst = new List<Account>(); | |
accLst.add(allAccMap.get(aud.Account__c)); | |
domainAccsMap.put(aud.Email_Domain__c, accLst); | |
}*/ | |
//Account IDs to Domains map | |
if(allAccIDtoDomainsMap.get(aud.Account__c) <> null) | |
{ | |
Set<String> domSet = allAccIDtoDomainsMap.get(aud.Account__c); | |
domSet.add(aud.Email_Domain__c); | |
allAccIDtoDomainsMap.put(aud.Account__c, domSet); | |
} | |
else | |
{ | |
Set<String> domSet = new Set<String>(); | |
domSet.add(aud.Email_Domain__c); | |
allAccIDtoDomainsMap.put(aud.Account__c, domSet); | |
} | |
} | |
//DQS Map Fill | |
//NOT BEING USED | |
/* | |
for(DQS_Line__c dqsl: [select id, Data_Quality_Suggestion__r.Suggested_Parent_Account__r.Billing_Email_Domain__c, Data_Quality_Suggestion__r.id, Data_Quality_Suggestion__r.Suggested_Parent_Account__c, Data_Quality_Suggestion__r.Suggested_Parent_Account__r.Name, Data_Quality_Suggestion__r.Suggestion_Type__c, Data_Quality_Suggestion__r.Account_Billing_Email__c from DQS_Line__c where Data_Quality_Suggestion__r.Suggestion_Type__c <> 'Confirmed']) | |
{ | |
if(DQSToDQSLineMap.get(dqsl.Data_Quality_Suggestion__r.id) <> null) | |
{ | |
List<DQS_Line__c> dqslLst = DQSToDQSLineMap.get(dqsl.Data_Quality_Suggestion__r.id); | |
dqslLst.add(dqsl); | |
DQSToDQSLineMap.put(dqsl.Data_Quality_Suggestion__r.id, dqslLst); | |
} | |
else | |
{ | |
List<DQS_Line__c> dqslLst = new List<DQS_Line__c>(); | |
dqslLst.add(dqsl); | |
DQSToDQSLineMap.put(dqsl.Data_Quality_Suggestion__r.id, dqslLst); | |
} | |
//Domain to DQS ID Map | |
domainToDQSIDMap.put(dqsl.Data_Quality_Suggestion__r.Suggested_Parent_Account__r.Billing_Email_Domain__c, dqsl.Data_Quality_Suggestion__c); | |
//Account Name to DQS ID Map | |
accNameToDQSIDMap.put(dqsl.Data_Quality_Suggestion__r.Suggested_Parent_Account__r.Name, dqsl.Data_Quality_Suggestion__c); | |
}*/ | |
List<DQS__c> dqsToInsert = new List<DQS__c>(); | |
Map<String , List<DQS_Line__c>> dqsUKeytoDQSL = new Map<String , List<DQS_Line__c>>(); | |
Map<Id, Integer> accToContactCountMap = new Map<Id, Integer>(); | |
Map<String, Set<Id>> accSimlrEmailMap = new Map<String, Set<Id>>(); | |
// Hamza | |
// Not needed | |
//Get Contacts count for the newly updated accounts | |
for(Account acc: accToUpsert) | |
{ | |
accIDs.add(acc.id); | |
} | |
// Hamza | |
// This part can be done by RollUp | |
for(Contact cnt: [select id, Account.id from Contact where Account.id IN : accIDs]) | |
{ | |
if(accToContactCountMap.get(cnt.Account.id) <> null) | |
{ | |
Integer count = accToContactCountMap.get(cnt.Account.id); | |
count++; | |
accToContactCountMap.put(cnt.Account.id, count); | |
}else | |
{ | |
Integer count = 0; | |
count++; | |
accToContactCountMap.put(cnt.Account.id, count); | |
} | |
} | |
//Parsing through the new accounts | |
map<string, set<Account>> mapOfMatchKeyWithAccounts = new map<string, set<Account>>(); | |
for(Account acc: accToUpsert) | |
{ | |
//Creating Suggestions for Hierarchy | |
/* | |
//Exact match on Billing Email Basis | |
if(excludDomains.contains(acc.Billing_Email_Domain__c) == false) | |
{ | |
//checking for existing DQS for exact match | |
if(domainToDQSIDMap.containsKey(acc.Billing_Email_Domain__c)) | |
{ | |
//Add to existing DQS | |
} | |
else | |
{ | |
//Check for AUDs under the Account | |
} | |
}*/ | |
//Hamza | |
// Optimize | |
//Match of Domains of the Contacts under Account | |
//Pick on new accounts created | |
if(accToContactCountMap.get(acc.Id) > 0 && false) | |
{ | |
String uniqueID = createUID(); | |
if(allAccIDtoDomainsMap.size() > 0) | |
{ | |
for(Id accMatch : allAccIDtoDomainsMap.keySet()) | |
{ | |
if(accMatch <> acc.id && allAccIDtoDomainsMap.get(acc.id) <> null && allAccIDtoDomainsMap.get(accMatch) <> null) | |
{ | |
system.debug('SYA :: allAccIDtoDomainsMap.get(acc.id) :: '+allAccIDtoDomainsMap.get(acc.id)+' :: allAccIDtoDomainsMap.get(accMatch) :: '+allAccIDtoDomainsMap.get(accMatch)); | |
if(allAccIDtoDomainsMap.get(acc.id).containsall(allAccIDtoDomainsMap.get(accMatch))) | |
{ | |
//Create a Map of unique ID and accounts along if there is a match | |
list<string> domainsLst = new list<string>(); | |
domainsLst.addAll(allAccIDtoDomainsMap.get(acc.id)); | |
//domainsLst.remove(null); | |
domainsLst.sort(); | |
string mapKey = string.join(domainsLst,','); | |
if(mapOfMatchKeyWithAccounts.containsKey(mapKey)) | |
{ | |
Set<account> accSet = mapOfMatchKeyWithAccounts.get(mapKey); | |
accSet.add(acc); | |
mapOfMatchKeyWithAccounts.put(mapKey, accSet); | |
} | |
else | |
{ | |
Set<account> accSet = new set<account>(); | |
accSet.add(acc); | |
mapOfMatchKeyWithAccounts.put(mapKey, accSet); | |
} | |
/*if(accSimlrEmailMap.get(uniqueID) <> null) | |
{ | |
Set<Id> accSet = accSimlrEmailMap.get(uniqueID); | |
accSet.add(accMatch); | |
accSimlrEmailMap.put(uniqueID, accSet); | |
} | |
else | |
{ | |
Set<Id> accSet = new Set<Id>(); | |
accSet.add(accMatch); | |
//accSet.add(acc.Id); Creating extra entry | |
accSimlrEmailMap.put(uniqueID, accSet); | |
}*/ | |
} //Hamza : ELSE - Match Billing domain for EXACT Crtieria from both acc & acc Match & add them in a different list. | |
} | |
} | |
} | |
/*system.debug('#accSimlrEmailMap='+accSimlrEmailMap); | |
//Create DQS after checking the size | |
if(accSimlrEmailMap.containskey(uniqueID) && accSimlrEmailMap.get(uniqueID).size() > 0) | |
{ | |
//Create DQS for Merge | |
//String uniqueKey = 'Account Merge Detected'+acc.id; | |
//Potential New Hierarchy :: New Child Records of Existing Hierarchy Suspected | |
DQS__c dqs = new DQS__c(status__c = 'Open', | |
Unique_Key__c = uniqueID); | |
List<DQS_Line__c> dqslLst = new List<DQS_Line__c>(); | |
dqslLst.add(new DQS_Line__c(Suggested_Child_Account__c = acc.id)); | |
for(String accId : accSimlrEmailMap.get(uniqueID)) | |
{ | |
//Check for any existing parent | |
if(accParentSet.contains(accId)) | |
{ | |
dqs.Suggested_Parent_Account__c = accId; | |
dqs.Suggestion_Type__c = 'New Child Records of Existing Hierarchy Suspected'; | |
} | |
dqslLst.add(new DQS_Line__c(Suggested_Child_Account__c = accId)); | |
} | |
if(dqs.Suggestion_Type__c == null) | |
dqs.Suggestion_Type__c = 'Potential New Hierarchy'; | |
dqsToInsert.add(dqs); | |
dqsUKeytoDQSL.put(uniqueID, dqslLst); | |
}*/ | |
} | |
/* | |
Integer matchCount = 0; | |
for(String domain: allAccIDtoDomainsMap.get(acc.id)) | |
{ | |
for(Id accMatch : allAccIDtoDomainsMap.keySet()) | |
{ | |
if(accMatch <> acc.id) | |
for(String domainToMatch : allAccIDtoDomainsMap.get(accMatch)) | |
{ | |
if(domainToMatch == domain && !excludDomains.contains(domain)) | |
{ | |
matchCount++; | |
} | |
} | |
} | |
} | |
*/ | |
//Creating Suggestions for MERGE | |
// Hamza - Remove map and get value from roll-up field. | |
//check if the account is having no contacts then look for other accounts to merge it with | |
if(accToContactCountMap.get(acc.Id) == 0 && false) //TODO:: REMOVE FALSE // && false | |
{ | |
List<Account> accLst = new List<Account>(); | |
//Account a = new account(name='test', website='test.com', phone = '123456', BillingStreet = 'abc 123', BillingState = 'state', BillingCity = 'city', BillingCountry = 'USA'); | |
accLst.add(acc); | |
//system.debug('SYA :: '+Datacloud.FindDuplicates.findDuplicates(accLst).MatchResults().getMatchRecords()); | |
List<String> ids = new List<String>(); | |
Datacloud.FindDuplicatesResult[] results = Datacloud.FindDuplicates.findDuplicates(accLst); | |
for (Datacloud.FindDuplicatesResult dupeResult : results) { | |
for (Datacloud.DuplicateResult dupeRes : dupeResult.getDuplicateResults()) { | |
for (Datacloud.MatchResult matchRes : dupeRes.getMatchResults()) { | |
for (Datacloud.MatchRecord matchRec : matchRes.getMatchRecords()) { | |
//allClear = false; | |
ids.add((string)matchRec.getRecord().get('id')); | |
} | |
} | |
} | |
} | |
if(Ids.size() > 0) | |
{ | |
//Create DQS for Merge | |
String uniqueKey = 'Account_Merge_Detected'+acc.id; | |
DQS__c dqs = new DQS__c(Suggestion_Type__c = 'Account Merge Detected', | |
status__c = 'Open', | |
Unique_Key__c = uniqueKey); | |
dqsToInsert.add(dqs); | |
List<DQS_Line__c> dqslLst = new List<DQS_Line__c>(); | |
dqslLst.add(new DQS_Line__c(Suggested_Child_Account__c = acc.id)); | |
for(String accId : Ids) | |
{ | |
dqslLst.add(new DQS_Line__c(Suggested_Child_Account__c = accId)); | |
} | |
dqsUKeytoDQSL.put(uniqueKey, dqslLst); | |
} | |
} | |
//system.debug('SYA :: IDs :: '+Ids); | |
} | |
system.debug('#AccMap='+mapOfMatchKeyWithAccounts); | |
map<string, DQS__c> mapOfMatchNDQS = new map<string, DQS__c>(); | |
for(string matchKeys :mapOfMatchKeyWithAccounts.keyset()){ | |
DQS__c dqs = new DQS__c(Suggestion_Type__c = 'Account Merge Detected', | |
status__c = 'Open', | |
Unique_Key__c = matchKeys); | |
mapOfMatchNDQS.put(matchKeys,dqs); | |
} | |
system.debug('#mapOfMatchNDQS='+mapOfMatchNDQS); | |
//Insert DQS | |
if(mapOfMatchNDQS.values().size() > 0) | |
{ | |
try | |
{ | |
//upsert dqsToInsert; //Hamza - Why double insertion | |
upsert mapOfMatchNDQS.values(); | |
}catch(exception ex) | |
{ | |
system.debug('SYA::' + ex); | |
} | |
} | |
List<DQS_Line__c> dqslToInsert = new List<DQS_Line__c>(); | |
List<DQS_Line__c> dqslTemp = new List<DQS_Line__c>(); | |
//Insert DQSL using the unique key | |
for(string matchKey: mapOfMatchNDQS.keyset()) | |
{ | |
//dqslTemp = dqsUKeytoDQSL.get(dqs.Unique_Key__c); | |
for(Account acc : mapOfMatchKeyWithAccounts.get(matchKey)) | |
{ | |
DQS_Line__c dqsl = new DQS_Line__c(); | |
dqsl.Data_Quality_Suggestion__c = mapOfMatchNDQS.get(matchKey).id; | |
dqsl.Suggested_Child_Account__c = acc.id; | |
dqslToInsert.add(dqsl); | |
} | |
} | |
//Insert DQSL | |
if(dqslToInsert.size() > 0) | |
{ | |
try | |
{ | |
upsert dqslToInsert; | |
}catch(exception ex) | |
{ | |
system.debug('SYA::' + ex); | |
} | |
} | |
} | |
public void exactMatchInitiate(List<Account> accsLst) | |
{ | |
//Accounts which have been newly created ::temporary Query | |
accToUpsert = [select id, Billing_Email__c, Billing_Email_Domain__c from Account limit 5000];// where name like '%CJA%' | |
for(Account acc: [select id, name, Billing_Email__c, Billing_Email_Domain__c, ParentId from Account where Billing_Email_Domain__c <> null])//accsLst)// AND name like '%CJA%' | |
/*where id = '0010r000009HiDoAAK' or id = '0010r000009HiDfAAK' | |
OR id= '0010r000009HiDeAAK' OR id = '0010r000009HiDZAA0'])*/ | |
{ | |
if(excludDomains.contains(acc.Billing_Email_Domain__c) == false && acc.ParentId == null) | |
{ | |
//Domain to Accs Map | |
if(allBillDomainAccMap.get(acc.Billing_Email_Domain__c) <> null) | |
{ | |
List<Account> accLst = allBillDomainAccMap.get(acc.Billing_Email_Domain__c); | |
accLst.add(acc); | |
allBillDomainAccMap.put(acc.Billing_Email_Domain__c, accLst); | |
} | |
else | |
{ | |
List<Account> accLst = new List<Account>(); | |
accLst.add(acc); | |
allBillDomainAccMap.put(acc.Billing_Email_Domain__c, accLst); | |
} | |
} | |
} | |
system.debug('#allBillDomainAccMap='+allBillDomainAccMap); | |
//Exact Match on Billing Email Domains | |
if(allBillDomainAccMap.size() > 0)// && false | |
exactMatchAccounts(allBillDomainAccMap, accsLst); | |
} | |
public void exactMatchAccounts( Map<String, List<Account>> exMatchAccMap, List<Account> newAccounts) | |
{ | |
Map<String, List<Account>> exMatchAccToProcessMap = new Map<String, List<Account>>(); | |
List<Account> accParentInsert = new List<Account>(); | |
for(account ac : newAccounts) | |
{ | |
if(exMatchAccMap.containskey(ac.Billing_Email_Domain__c)) | |
{ | |
List<Account> tempAccLst = new List<Account>(); | |
tempAccLst.add(ac); | |
exMatchAccToProcessMap.put(ac.Billing_Email_Domain__c, tempAccLst); | |
} | |
} | |
for(String domain : exMatchAccToProcessMap.keyset()) | |
{ | |
if(exMatchAccToProcessMap.get(domain).size() > 1) | |
{ | |
Account acc = new Account(); | |
acc.Name = domain.contains('.') ? domain.substringbefore('.') : domain; | |
acc.RM_Account_ID__c = domain; | |
acc.OwnerId = Label.IntegrationOwner; | |
acc.Billing_Email_Domain__c = domain; | |
accParentInsert.add(acc); | |
/*accParentInsert.add(new Account(Name = domain.contains('.') ? domain.substringbefore('.') : domain, | |
RM_Account_ID__c = domain, | |
OwnerId = Label.IntegrationOwner, | |
Billing_Email_Domain__c = domain));*/ | |
} | |
} | |
if(accParentInsert.size() > 0) | |
{ | |
Upsert accParentInsert RM_Account_ID__c; | |
Map<String, Id> accExtIdToAccId = new Map<String, Id>(); | |
Map<String, DQS__c> dqsToInsert = new Map<String, DQS__c>(); | |
List<DQS_Line__c> dqslList = new List<DQS_Line__c>(); | |
for(Account acc: accParentInsert) | |
{ | |
String uniqueKey = 'Exact Match Suggestion'+acc.id; | |
DQS__c dqs = new DQS__c(Suggestion_Type__c = 'Exact Match Suggestion', | |
status__c = 'Confirmed', | |
Unique_Key__c = uniqueKey, | |
Suggested_Parent_Account__c = acc.Id | |
); | |
dqsToInsert.put(acc.RM_Account_ID__c, dqs); | |
} | |
if(dqsToInsert.size() > 0 ) | |
{ | |
insert dqsToInsert.values(); | |
List<Account> ListupdateParentAcc = new list<Account>(); | |
for(string emailDomain : dqsToInsert.keyset()) | |
{ | |
for(Account childAcc : exMatchAccToProcessMap.get(emailDomain)) | |
{ | |
dqslList.add(new DQS_Line__c(Data_Quality_Suggestion__c = dqsToInsert.get(emailDomain).id, | |
Suggested_Child_Account__c = childAcc.id)); | |
if(childAcc.ParentID == null) | |
{ | |
Account updAcc = new Account(); | |
updAcc.Id = childAcc.id; | |
updAcc.ParentId = dqsToInsert.get(emailDomain).Suggested_Parent_Account__c; | |
ListupdateParentAcc.add(updAcc); | |
} | |
} | |
} | |
System.debug('SYA :: dqslList.size() :: '+dqslList.size()); | |
System.debug('SYA :: ListupdateParentAcc.size() :: '+ListupdateParentAcc.size()); | |
if(dqslList.size()>0) | |
insert dqslList; | |
System.debug('SYA :: dqslList :: '+dqslList); | |
if(ListupdateParentAcc.size()>0) | |
update ListupdateParentAcc; | |
System.debug('SYA :: ListupdateParentAcc :: '+ListupdateParentAcc); | |
} | |
} | |
} | |
public string createUID() | |
{ | |
Blob b = Crypto.GenerateAESKey(128); | |
String h = EncodingUtil.ConvertTohex(b); | |
String guid = h.SubString(0,8)+ '-' + h.SubString(8,12) + '-' + h.SubString(12,16) + '-' + h.SubString(16,20) + '-' + h.substring(20); | |
return guid; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment