4

I am working on a class that searches for public groups, matches them to an Account Id and then takes that data to create AccountShare records for manual sharing.

I am creating a Map(String, List) to store my accounts that I want to loop through. the problem is that when I try to loop through the list of Accounts I get an error.

If I do this:

public static GroupMatch matchGroups(List<Group> groupList, Map<String, List<Account>> mapParentAccountNameToLstAccount)
{
    for(Account acc : mapParentAccountNameToLstAccount.values())
    {

I get the compile error:

Invalid loop variable type expected List(Account) was Account

If I do this:

public static GroupMatch matchGroups(List<Group> groupList, Map<String, List<Account>> mapParentAccountNameToLstAccount)
{
    for(Account acc : mapParentAccountNameToLstAccount)
    {

I get the compile error:

Loop must iterate over collection: Map(String,List(Account))

How can I loop through each of the accounts in the list?

Full method:

public static GroupMatch matchGroups(List<Group> groupList, Map<String, List<Account>> mapParentAccountNameToLstAccount)
{
    Boolean groupFound = FALSE;
    GroupMatch gMatch = new GroupMatch();

    for(Account acc : mapParentAccountNameToLstAccount.values())
    {
        groupFound = FALSE;
        if(acc.RecordTypeId == Schema.SObjectType.Account.getRecordTypeInfosByName().get('Practice Account').getRecordTypeId())
        {
            // 'X' + acc.Company_Account__c
            for(Group g : groupList)
            {
                if('X' + acc.Company_Account__c == g.DeveloperName)
                {
                    AccountShare accShare = new AccountShare();
                        accShare.AccountId = acc.Id;
                        accShare.AccountAccessLevel = 'Edit';
                        accShare.OpportunityAccessLevel = 'Read';
                        accShare.UserOrGroupId = g.Id;
                    gmatch.accShareList.add(accShare);
                    groupFound = TRUE;
                    break;
                }
            }
            if(!groupFound)
            {
                Group newGroup = new Group();
                    newGroup.Name = 'X' + acc.Company_Account__c;               
                gMatch.groupsToCreateList.add(newGroup);

                if(!gMatch.accNotFound.containsKey('X' + acc.Company_Account__c))
                {
                    gMatch.accNotFound.put('X' + acc.Company_Account__c, new List<Account>{acc});
                }
                else {
                    gMatch.accNotFound.get('X' + acc.Company_Account__c).add(acc);
                }
            }
        } else
        {
            // 'X' + acc.Id
            for(Group g : groupList)
            {
                if('X' + acc.Id == g.DeveloperName)
                {
                    AccountShare accShare = new AccountShare();
                        accShare.AccountId = acc.Id;
                        accShare.AccountAccessLevel = 'Edit';
                        accShare.OpportunityAccessLevel = 'Read';
                        accShare.UserOrGroupId = g.Id;
                    gmatch.accShareList.add(accShare);
                    groupFound = TRUE;
                    break;
                }
            }
            if(!groupFound)
            {
                Group newGroup = new Group();
                    newGroup.Name = 'X' + acc.Id;               
                gMatch.groupsToCreateList.add(newGroup);

                if(!gMatch.accNotFound.containsKey('X' + acc.Id))
                {
                    gMatch.accNotFound.put('X' + acc.Id, new List<Account>{acc});
                }
                else {
                    gMatch.accNotFound.get('X' + acc.Id).add(acc);
                }
            }
        }
    }       
    return gMatch;
}

1 Answer 1

6

Map<String, List<Account>>.values() has a return type of List<List<Account>>. You'll have to do an outer loop to "unroll" this data structure, if you want to iterate over all of the Accounts contained:

for (List<Account> outerList : mapParentAccountNameToLstAccount.values()) {
    for (Account a : outerList) {

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .