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
Vignesh NayakVignesh Nayak 

collection

Hi All,

Following code when executed in Anonymous window, gets executed successfully.
But when i am trying to save the same code in develloper console, it is throwing error "   unexpected token: ','   " at the line that has :  M.put(accId,lst); statement.

id accId='00190000010nBb6';
id accId1='00190000010nPg5';
List<Contact> lst=[select Name,LastName,AccountId from Contact where AccountId =: accId];
List<Contact> lst1=[select Name,LastName,AccountId from Contact where AccountId =: accId1];
List<Contact> c2 = new List<Contact>();
   
map<id, List<Contact>> M = new map<id, List<Contact>>();
M.put(accId,lst); 
M.put(accId1,lst1);
   
system.debug('map<id, List<Contact>> ='+M);

List<Contact> C = M.get(accId);
system.debug('List<Contact> C='+C);

List<List<Contact>> C1 = M.values();
system.debug('List<List<Contact>> ='+C1);

For(List<Contact> L1 : C1)
{
C2.addAll(L1);
}

system.debug('List<Contact> c2 ='+C2);



Regards,
Vignesh
Best Answer chosen by Vignesh Nayak
Vignesh NayakVignesh Nayak
Hi Shashank,

Code when executed in Anonymous window, gets executed successfully.
But In classes, Otherthan Declarations, we need to include all logic statements inside a method.
So the code when modified as below worked.

Code :

public class Collections
{
    id accId='00190000010nBb6';
    id accId1='00190000010nPg5';
    List<Contact> lst=[select Name,LastName,AccountId from Contact where AccountId =: accId];
    List<Contact> lst1=[select Name,LastName,AccountId from Contact where AccountId =: accId1];
    List<Contact> c2 = new List<Contact>();
    map<id,List<Contact>> M = new map<id,List<Contact>>();

    Public void AssigningValueToMap()
    {
         M.put(accId , lst);
         M.put(accId1 , lst1);
   
         List<Contact> C = M.get(accId);
         List<List<Contact>> C1 = M.values();
         for(List<Contact> L1 : C1)
         {
              C2.addAll(L1);
          }
    }

}

All Answers

ShashForceShashForce
Hi Vignesh,

It could probably be because of copy-pasting from elsewhere. Please try manually typing the faulty line of code and check if this resolves the error.

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
Vignesh NayakVignesh Nayak
Hi Shashank,

Its not Copy-Pasted code, I tried it by myself. As i am new to salesforce, trying to play around collections.
Please help me out if u know the solution and i will definetly mark your answer as best answer if it resolves my problem.

Regards,
Vignesh
ShashForceShashForce
I see nothing wrong with the syntax, so I would suggest you to try typing the same code again and check if you are still facing the error. When it worked fine in anonymous window, it means the syntax is fine.
Vignesh NayakVignesh Nayak
Hi Shashank,

Code when executed in Anonymous window, gets executed successfully.
But In classes, Otherthan Declarations, we need to include all logic statements inside a method.
So the code when modified as below worked.

Code :

public class Collections
{
    id accId='00190000010nBb6';
    id accId1='00190000010nPg5';
    List<Contact> lst=[select Name,LastName,AccountId from Contact where AccountId =: accId];
    List<Contact> lst1=[select Name,LastName,AccountId from Contact where AccountId =: accId1];
    List<Contact> c2 = new List<Contact>();
    map<id,List<Contact>> M = new map<id,List<Contact>>();

    Public void AssigningValueToMap()
    {
         M.put(accId , lst);
         M.put(accId1 , lst1);
   
         List<Contact> C = M.get(accId);
         List<List<Contact>> C1 = M.values();
         for(List<Contact> L1 : C1)
         {
              C2.addAll(L1);
          }
    }

}
This was selected as the best answer