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;}
    }
  • 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 Oct 10 '19 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 Oct 10 '19 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 Oct 10 '19 at 19:38
0

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.

|improve this answer|||||

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.