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
D'Mario LewisD'Mario Lewis 

How to get more than the last element of a list

I have this list of lists and when I used this code I only get the last element of the list

//////////////////////////////////////////////////////////// APEX CODE ////////////////////////////////////////////////////////////////////////////////////   
private List<List<MinistrySharesSummary>> ListCollector (List<MinistrySharesSummary> suggestedAmountsList){
        List<List<MinistrySharesSummary>> classisList = new List<List<MinistrySharesSummary>>();
        List<MinistrySharesSummary> churchList = new List<MinistrySharesSummary>();
        
        String tempParent = suggestedAmountsList.get(0).parent;
        for(MinistrySharesSummary ms : suggestedAmountsList){
            
            if(ms.parent == tempParent ){
                churchList.add(ms);     
            }
            else{
                classisList.add(churchList);
                churchList.clear();
                churchList.add(ms);
                tempParent = ms.parent;
            }
        }
        
        return classisList;
    }

 
Best Answer chosen by D'Mario Lewis
lrw757lrw757
Because the churchlist is passed to the classisList by reference, calling "clear" on the list clears it in the list of lists as well. Instead, declare church list as a new list.

//////////////////////////////////////////////////////////// APEX CODE ////////////////////////////////////////////////////////////////////////////////////   
private List<List<MinistrySharesSummary>> ListCollector (List<MinistrySharesSummary> suggestedAmountsList){
        List<List<MinistrySharesSummary>> classisList = new List<List<MinistrySharesSummary>>();
        List<MinistrySharesSummary> churchList = new List<MinistrySharesSummary>();
        
        String tempParent = suggestedAmountsList.get(0).parent;
        for(MinistrySharesSummary ms : suggestedAmountsList){
            
            if(ms.parent == tempParent ){
                churchList.add(ms);     
            }
            else{
                classisList.add(churchList);
                churchList = new List<MinistrySharesSummary>();
                churchList.add(ms);
                tempParent = ms.parent;
            }
        }
        
        return classisList;
    }

All Answers

lrw757lrw757
Because the churchlist is passed to the classisList by reference, calling "clear" on the list clears it in the list of lists as well. Instead, declare church list as a new list.

//////////////////////////////////////////////////////////// APEX CODE ////////////////////////////////////////////////////////////////////////////////////   
private List<List<MinistrySharesSummary>> ListCollector (List<MinistrySharesSummary> suggestedAmountsList){
        List<List<MinistrySharesSummary>> classisList = new List<List<MinistrySharesSummary>>();
        List<MinistrySharesSummary> churchList = new List<MinistrySharesSummary>();
        
        String tempParent = suggestedAmountsList.get(0).parent;
        for(MinistrySharesSummary ms : suggestedAmountsList){
            
            if(ms.parent == tempParent ){
                churchList.add(ms);     
            }
            else{
                classisList.add(churchList);
                churchList = new List<MinistrySharesSummary>();
                churchList.add(ms);
                tempParent = ms.parent;
            }
        }
        
        return classisList;
    }
This was selected as the best answer
D'Mario LewisD'Mario Lewis
Thanks so much that fixed the problem