public void execute(Database.BatchableContext BC, List<FinServ__FinancialAccount__c> scope) {
Set<String> Acc_Numbers = new Set<String>();
Map<String,List<Id>> My_Records = new Map<String,List<Id>>();
List<Junction_Object__c> FinAcc_Custom_Rel = new List<Junction_Object__c>();
List<Id> My_List = new List<Id>();
for(FinServ__FinancialAccount__c FACC : scope){
if(!String.isBlank(FACC.FinServ__FinancialAccountNumber__c)){
Acc_Numbers.add(FACC.FinServ__FinancialAccountNumber__c);
}enter code here
}
for(Custom_Object__c usrIds : [Select Id, Account_Number__c, UserId__c from Custom_Object__c where Account_Number__c =: Acc_Numbers]){
if(!String.isBlank(usrIds.Account_Number__c)){
My_List.add(usrIds.Id);
My_Records.put(usrIds.Account_Number__c,My_List);
}
}
for(FinServ__FinancialAccount__c FACC : scope){
if(!String.isBlank(FACC.FinServ__FinancialAccountNumber__c) && My_Records.containsKey(FACC.FinServ__FinancialAccountNumber__c)){
for(ID Q2 : My_Records.get(FACC.FinServ__FinancialAccountNumber__c)){
Junction_Object__c JOBB = new Junction_Object__c();
JOBB.Financial_Account__c = FACC.Id;
JOBB.My_U_ID__c = Q2;
FinAcc_Custom_Rel.add(JOBB);
}
}
}
Database.insert(FinAcc_Custom_Rel);
}
I am trying to create junction object records to establish Many to Many relationship.
Here, FinancialAccount Numbers can be same in multiple records, so have used a set.
The problem is that there could be multiple Custom_Object__c records having same account number, so I have map My_Records.
This works fine but I want to avoid using nested for loop here.
Would appreciate any help.