0

I am trying to reuse a map in my code which is originally populated by a query for account. I am creating another query, which results should also populate the same map. Not sure if that is possible. Here is what I have:

//This is the map I would like to reuse
Map<String,list<Account>> acctMap = new Map<String,list<Account>>();
//This is the new query I would like to add to my acctMap    
if(accountid != null && constType == 'IN'){

    for(Contact c:[
        SELECT Id, Master_ID__c, account.id, account.Status__c, account.Include_in_Funnel__c
        FROM Contact 
        WHERE Master_ID__c IN :masterIdSet
    ]){

        if(acctMap.containsKey(c.Master_ID__c)){
            //acctMap.get(c.Master_ID__c).add(c.account.id);//need to add the 3 account fields for account

        }else{

            //creating a new ExistingAccountResult for this account
            ExistingAccountResult exResult = new ExistingAccountResult();                                            
            exResult.accountId = c.accountId;
            exResult.status = c.account.Status__c;
            exResult.includeinfunnel = c.account.Include_in_Funnel__c;
            //acctMap.put(c.Master_Id__c, exResult); // the map is looking for a list of Account
            //acctMap.put(c.Master_Id__c, new list<Account>{exResult});
        }                           
    } 
} else{
    //this is my original query that I am using for my logic
    for(Account a:[
        SELECT Id, Owner.Id, Owner.Name, Salesforce_Account_ID__c, Name, Status__c,Include_in_Funnel__c, Master_ID__c 
        FROM Account 
        WHERE Master_ID__c IN :masterIdSet
    ]){
        if(acctMap.containsKey(a.Master_ID__c)){
            acctMap.get(a.Master_ID__c).add(a);//a.id
        }else{
            acctMap.put(a.Master_ID__c, new list<Account>{a});
        }                            
    }

    public class ExistingAccountResult{
        public String accountId {get;set;}
        public String status {get;set;}
        public Boolean includeinfunnel {get;set;}
    }
3
  • When you use such excessive indentation, it makes your code much harder to read. You also appear to be missing at least a closing bracket.
    – Adrian Larson
    Commented Oct 10, 2019 at 13:27
  • Thank you. The indentation kept getting messed up every time I was marking the text as code. I tried to display the most relevant code in here which is why I may have missed a closing bracket. Thanks
    – Ivo Dimov
    Commented Oct 10, 2019 at 13:30
  • 1
    better variable naming conventions can often illuminate how to solve a problem. Rather than acctmap it should be something like accountsByMasterId.
    – cropredy
    Commented Oct 10, 2019 at 19:38

1 Answer 1

1

You can't put 'existingAccountResult into a List<Account> - you need to turn it into an account first.

For example:

Account exResult= new Account();
exREsult.Id = c.accountId;
exResult.status = c.account.Status__c;
exResult.includeinfunnel = c.account.Include_in_Funnel__c;

Then you can add exResult into your list.

You must log in to answer this question.

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