function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Felipe CzkFelipe Czk 

How to create a list or map from 2 different lists/map

I have this Map, and it's on my repository class
 

public Map<Id,List<AggregateResult>> buscaPBsAtivacao(){
        Map<Id, List<AggregateResult>> membroCampanhaMap = new Map<Id, List<AggregateResult>>();
        List<String> Territories= new List<String>{
           'Some Territories Names'
        };
       
        List<AggregateResult> territorioAssociado = [
            SELECT
                Object.Name, //here is the account name
                ObjectId, // here is the account Id
                Territory2Id,
                Territory2.DeveloperName
            FROM
                ObjectTerritory2Association
            WHERE
                Territory2.DeveloperName IN : Territories
            GROUP BY
                Territory2.DeveloperName,
                Object.Name,
                ObjectId,
                Territory2Id  
            ORDER By
                Territory2.DeveloperName
        ];
        for (AggregateResult agr : territorioAssociado) {
            Id objectId = (Id) agr.get('ObjectId');
            if (!membroCampanhaMap.containsKey(objectId)) {
                membroCampanhaMap.put(objectId, new List<AggregateResult>());
            }
            membroCampanhaMap.get(objectId).add(agr);
        }
        System.debug('MembroCampanhaMap -> ' + MembroCampanhaMap);
        return membroCampanhaMap;
    }

and I have a List<Account> with all the accounts (including the ones that are on the membroCampanhaMap). This account list is passed on the function parameter.

I have to iterate over just the accounts that are on the membroCampanhaMap, so there is a way to like to compare these 2 and maybe create a new one with just the needed Accounts?

so I can do what I need with these records.

By the way, I even tried to Query more fields on the membroCampanhaMap, but when I added Object.LastModifiedDate (for example), it says that is an invalid field, with the ones that are on the select working fine

Best Answer chosen by Felipe Czk
Prateek Prasoon 25Prateek Prasoon 25
Yes, you can iterate over the List<Account> parameter passed to the function and check if the account's Id is present in the Map<Id, List<AggregateResult>> returned by the buscaPBsAtivacao() function. If an account's Id is present in the map, it means that the account has a Territory2Association record associated with it for one of the territories specified in the Territories list.
Here's an example of how you can create a new list of accounts that have a Territory2Association record associated with them for one of the specified territories:
public List<Account> getAccountsForTerritories(List<Account> allAccounts) {
    List<Account> accountsWithTerritories = new List<Account>();
    Map<Id, List<AggregateResult>> membroCampanhaMap = buscaPBsAtivacao();
    for (Account acc : allAccounts) {
        if (membroCampanhaMap.containsKey(acc.Id)) {
            accountsWithTerritories.add(acc);
        }
    }
    return accountsWithTerritories;
}

If you find this answer helpful, Please mark it as the best answer.