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
karol.freebergkarol.freeberg 

Nested list question.

I have a nested list issue. I have two lists of IDs and I'm trying to combine them into one list so I can return the list and then split it back out after it is returned. This is what I have.

List<List<ID>> AllIDLists = New List<List<ID>>();
AllIDLists.add(mailToIds);
AllIdLists.add(WhatOppIDs);
Return AllIdLists;

So first I'm not sure that the list definition is correct. The definition should accomodate basically one list that contains 2 lists. 
My second question is then what is the best way to split the lists back out. My code for that is below.

List<ID> oppIdsList = New List<ID>{AllIdLists.get(0)};
List<ID> whatIdsList = New List<ID>{AllIdLists.get(1)};

Any help on what I am doing wrong here is appreciated. 
Best Answer chosen by karol.freeberg
Sameer PrasonnSameer Prasonn
Hi Karol,

Here is the solution for your problem
To make Nested List you can use following code

declaration for list that hold lists
List<List<id>> finalList=new List<List<id>>();

now you can add lists in this object by 

finalList.add(new List<Id>());
finalList.add(new List<Id>());

now if we want to add one item in each list
finalList[0].add('Value');
finalList[1].add('next Value');

now its time to retrive both the lists which has single item in it

List<Id> first=finalList[0];
List<id> second=finalList[1];

Hope that resolved your query. please let me know if you need further help. 

All Answers

karol.freebergkarol.freeberg
I should have added these declarations as well so you know that I am adding lists to the list.

List<Id> mailToIds = new List<Id>();
List<ID> WhatOppIDs = new List<ID>();
Sameer PrasonnSameer Prasonn
Hi Karol,

Here is the solution for your problem
To make Nested List you can use following code

declaration for list that hold lists
List<List<id>> finalList=new List<List<id>>();

now you can add lists in this object by 

finalList.add(new List<Id>());
finalList.add(new List<Id>());

now if we want to add one item in each list
finalList[0].add('Value');
finalList[1].add('next Value');

now its time to retrive both the lists which has single item in it

List<Id> first=finalList[0];
List<id> second=finalList[1];

Hope that resolved your query. please let me know if you need further help. 
This was selected as the best answer
Sameer PrasonnSameer Prasonn
i have added anonymous object of list in line #8 and 9. If you want to add it after creating seperate object. you can do it. it is upto your requirement.
karol.freebergkarol.freeberg
Thank you that is what I needed to know. I have one other question you may be able to help me with. The ID lists I am creating are used to send out mass email notifications on opportunities. It seems when I have duplicate IDs in that list, only the first email goes. Is there something with mass emails that does not allow 2 emails to go to the same email address?
Sameer PrasonnSameer Prasonn
Hi Karol,

to filter email address, you can use Set object. Since set is not allowing duplicate value for the same.  once all Ids are filtered you can use it for sending email purpose. hope that resolve your query.

If my answer helps you find the solution of your problem. Please do mark as best Answer for the problem.