I'm confident that I'm missing something here. I'm not able to evaluate a list of Ids until I print it using System.debug. In the snippet below, I take a list of Accounts and extract the Ids from those records and make a list of Ids. I use that list of Ids to query another object (CampaignPatientDetails) then make a list of Ids from those returned records (accountsToRemove
). I then attempt to build a new list that would contain any account not referenced in accountsToRemove
. I use Apex's List function contains()
to make this evaluation. The problem is that this will not work unless the System.debug
statement in buildAccountListRemoveDetails()
is present. If I remove that System.debug
the evaluation does not occur or rather, I suspect, accountsToRemove
is an empty list until the print happens. It's as if the list does not exist until it is printed. Is there some way to force the list to build without a print statement?
private static List<Account> removeSatisfiedAccounts(List<Account> allMatchingAccounts, String campaignId) {
List<Id> accountIds = new List<Id>();
for(Account account : allMatchingAccounts) {
accountIds.add(account.Id);
}
List<CampaignPatientDetails__c> details = [SELECT Account__c FROM CampaignPatientDetails__c WHERE Account__c IN :accountIds AND RA_Campaign__c = :campaignId];
List<Id> accountsToRemove = new List<Id>();
for(CampaignPatientDetails__c detail : details) {
accountsToRemove.add(detail.Account__c);
}
List<Account> campaignAccounts = buildAccountListRemoveDetails(allMatchingAccounts, accountsToRemove);
return campaignAccounts;
}
private static List<Account> buildAccountListRemoveDetails(List<Account> allMatchingAccounts, List<Id> accountsToRemove) {
System.debug(accountsToRemove);
List<Account> campaignAccounts = new List<Account>();
for (Account account : allMatchingAccounts) {
if (!accountsToRemove.contains(account.Id)) {
System.debug(account.Id);
campaignAccounts.add(account);
}
}
return campaignAccounts;
}