-1

Based on my previous question List query results into a string I want to gather a bunch of related records and populate a long text area field. I am able to get a string to display the values I want. Now I want to take the string and update the Account field with the string.

It makes me think that I need to loop through each account id, grab the related records and form the joined string, then put that sting into the field. Finally updating all the Account records that were passed in the batch.

I wrote this but I can't seem to figure out the right syntax to add the accounts into a map/list to update them. Is this the right way to accomplish this?

public static void queryMembers(Map<Id, Account> accId)
{
    Set<String> userSet = new Set<String>();
    Map<Id, Account> accUpdate = new Map<Id, Account>();
    for(Account a :accId.values())
    {
        for(NPD_Account_Team__c aTeam : [SELECT User__r.FirstName,
                                                User__r.LastName 
                                           FROM NPD_Account_Team__c 
                                           WHERE Account__c IN :accId.values() 
                                           AND Team_Member_Status__c = 'Active'
                                           ORDER BY User__r.FirstName ASC])
        {
            userSet.add(ateam.User__r.FirstName + ' ' + aTeam.User__r.LastName);
        }
        a.Account_Team__c = String.join(New List<String>(userSet), ', ');
        accUpdate.putAll(a);
    }
    update accUpdate;
}

I get the compile error:

Method does not exist or incorrect signature: [Map< Id,Account >].putAll(Account)

3
  • Before going on with your main error, your NPD_Account_Team SOQL is inside a for loop. This not recommended unless you are very sure about the number of records being looped in the outer loop are not gonna cause SOQL 101 error.
    – Mahmood
    Feb 22, 2017 at 20:39
  • Have you tried looking at documentation for the map class before you asked this question? If you don't know how to use a particular method (and getting this error is an indication of that), the developer docs should probably be your first stop. Have you tried googling your error before asking? Do know that you can't perform DML on a map? (that would be the next error you'd see after resolving this one)
    – Derek F
    Feb 22, 2017 at 21:05
  • You have provided code and relevant details, but I'm still giving this -1 because it does seem like you haven't attempted to research this at all before asking.
    – Derek F
    Feb 22, 2017 at 21:09

4 Answers 4

1

So the OP asked on the original question in a comment and I did not realize there was a question already out there.

Posting the updated answer here:

Set<ID> accId = new Set<Id>();

//Populate accId with values

Map<Id,Set<String>> accountMap = New Map<Id,Set<String>>(); //Ensures no duplicates

for(NPD_Account_Team__c aTeam : [SELECT User__r.FirstName,
                    User__r.LastName
             FROM NPD_Account_Team__c
             WHERE Account__c IN :accId
             ORDER BY User__r.FirstName DESC]){

        Set<String> userSet = accountMap.get(aTeam.Account__c);
        if(userSet == null) userSet = New Set<String>();
        userSet.add(aTeam.User__r.FirstName +  ' ' + aTeam.User__r.LastName);
        accountMap.put(aTeam.Account__c,userSet);
     }
    System.debug('usersString is:: ' + string.join(New List<String>(usersSet),','); //Need to convert to list before joining
}

Account[] accTBU = New Account[]{};

for(Id aId : accountMap.keySet()){
     accTBU.add(
         New Account(
                Id=aId,
                USERFIELD = string.join(
                                New List<String>(accountMap.get(aId)),
                                ','
                             )
     );
}

update accTBU;
2

Map class putAll() method accepts only List<sObject (or sObject[]), while in your code

accUpdate.putAll(a);

a is a single Account record, hence the error.

Secondly, you can't perform DML on the map as mentioned in one of the comments, so you will need to convert your map<id, account> into List<Account>.

P.S: Also, noted that your NPD_Account_Team SOQL is inside a for loop. This not recommended unless you are very sure about the number of records being looped in the outer loop are not gonna cause SOQL 101 error.

1

The SOQL needs to get out of the for loop and if you want to use the method putAll you need to use a map or array, so putAll(Map) or putAll(Array). Please check SFDC docs on the Map Class.

1

Without using SOQL query in for loop I would prefer this approach.

1) Prepare a map of Account Id and corresponding userSet upon looping through NPD_Account_Team__c query records.

2) Loop through the accounts and assign joined names to the Account_Team__c attribute.

3) Finally update the list of accounts.

public static void queryMembers(Map<Id, Account> accId)
{
    Set<String> userSet = new Set<String>();
    List<Account> lstAccountUpdate = new List<Account>();

    Map<Id, Set<String>> strMap = new Map<Id,Set<String>>(); 

    for(NPD_Account_Team__c aTeam : [SELECT User__r.FirstName,
                                                User__r.LastName 
                                           FROM NPD_Account_Team__c 
                                           WHERE Account__c IN :accId.keySet() 
                                           AND Team_Member_Status__c = 'Active'
                                           ORDER BY User__r.FirstName ASC])
    {
        userSet = new Set<String>();
        if(strMap.containsKey(aTeam.Account__c))
        {
            userSet = strMap.get(aTeam.Account__c);
        }       
        userSet.add(ateam.User__r.FirstName + ' ' + aTeam.User__r.LastName);
        strMap.put(aTeam.Account__c, userSet);
    }


    for(Account a :accId.values())
    {        
        userSet =  strMap.get(a.Id);
        a.Account_Team__c = String.join(New List<String>(userSet), ', ');
        lstAccountUpdate.add(a);
    }
    update accUpdate;
}

Note: I have not compiled the code, just given an idea to achieve this.

You must log in to answer this question.

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