0

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;
    }
| improve this question | | | | |
2

This sounds like a variant (or possibly regression) of this Known Issue, which is noted as being fixed; you may want to contact Support regarding this specific issue, as it should work.

The easiest way to solve this problem is to use Set objects instead of List objects. Also, you could optimize...

static List<Account> removeSatisfiedAccounts(List<Account> allMatchingAccounts, String campaignId) {
    // Get a Set of Id values using Map's keySet function.
    Set<Id> accountIds = new Map<Id, Account>(allMatchingAccounts).keySet();
    // Alias Id in an AggregateResult query to get the same effect.
    Set<Id> accountsToRemove = new Map<Id, AggregateResult>([
          SELECT Account__c Id
          FROM CampaignPatientDetails__c
          WHERE Account__c = :accountIds and RA_Campaign__c = :campaignId
          GROUP BY Account__c]).keySet();
    return buildAccountListRemoveDetails(allMatchingAccounts, accountsToRemove);
}
static List<Account> buildAccountListRemoveDetails(List<Account> allMatchingAccounts, Set<Id> accountsToRemove) {
    // Rest of the code should be fine //
| improve this answer | | | | |
  • Thank you for this fantastic answer! – blastocyst.jones Feb 21 at 22:21
  • @blastocyst.jones You're welcome! – sfdcfox Feb 21 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.