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
Jagadeesh MJagadeesh M 

How to avoid nested For Loops ?

Hi, I want to redefine and remove nested for loop and fix couple of issues with below code,

My Method:
------------
public void buildQueueInfo(List<Group> queues, Map<Id, List<GroupMember>> queueIdToMembersMap, List<User> allUsers, Map<Id, User> userMap) {

        for (Group queue : queues) {
            List<GroupMember> gmList = new List<GroupMember>();
            gmList = queueIdToMembersMap.get(queue.Id);
            List<User> nonUsers = new List<User>();

            for (User u : allUsers) {
                Boolean notAMember = true;
                if (gmList != null) {
                    for (GroupMember gm : gmList) {
                        if (u.Id == gm.UserOrGroupId) {
                            notAMember = false;
                        }
                    }
                }
                if (notAMember) {
                    nonUsers.add(u);
                }
            }

            populateQueueInfo(queue, gmList, nonUsers, userMap);
            //To Reduce heap size
            queues=null;
            //allUsers=null;
            gmList=null;
        }
        
    }


your help in this regrads would be greatly appreciates.

Thanks!!
Jagadeesh. M
SUCHARITA MONDALSUCHARITA MONDAL

Hi Jagadeesh,

You can try something as below :

 

        Set<Id> ugIds = new Set<Id>();
        List<User> nonUsers = new List<User>();
        for(Group queue : queues){
            if(queueIdToMembersMap.containsKey(queue.Id)){   // You can use existing Map
                    for(GroupMember gm : queueIdToMembersMap.get(queue.Id)){ // Iterate the List based from Queue Id
                        ugIds.add(gm.UserOrGroupId));  // Create set
                    }
            }
        }

        if(ugIds.size() > 0){
            Boolean notAMember = true;
            for(User u : allUsers){
                if(ugIds.contains(u.Id)){ // use the set for checking the same.
                    notAMember = false;
                }else{
                    nonUsers.add(u);
                }
            }
        } 
 

PS.- For avoiding  nesting , go with Collections.

 

Hope this helps!

Thanks,
Sucharita

Jagadeesh MJagadeesh M
Hi Sucharita,
Thanks for your reply and your code..
I believe this will help but in the code I'm calling the "populateQueueInfo" method which I need “queue” value, that is the problem where I’m unable to re write the code. Could you please help me on this. 

Your quick response would be greatly appreciates. Thanks!!!






 
SUCHARITA MONDALSUCHARITA MONDAL

Hi Jagadeesh,

You already have Queue which is coming from 'buildQueueInfo()'. Therefore you can pass it to "populatedQueueInfo()".  Please let me know if this answers your question or where exactly you need help.

Thanks,
Sucharita

Jagadeesh MJagadeesh M
Hi Sucharitha,

Thanks for you Reply,

Still Im getting the error "Collection size 1,428 exceeds maximum size of 1,000." with modified code.

Please see below modified code as you suggested,


/*******************************************************************************************************/

public void buildQueueInfo(List<Group> queues, Map<Id, List<GroupMember>> queueIdToMembersMap, List<User> allUsers, Map<Id, User> userMap) {

        Set<Id> usergroupIds = new Set<Id>();
        List<User> nonUsers = new List<User>();
        List<GroupMember> gmList = new List<GroupMember>(); 
        for(Group queue : queues){
            gmList = queueIdToMembersMap.get(queue.Id);
                if(queueIdToMembersMap.containsKey(queue.Id)){   
                    for(GroupMember gm : queueIdToMembersMap.get(queue.Id)){ 
                         usergroupIds.add(gm.UserOrGroupId);  
                    }
                }
          }  
        
         if(usergroupIds.size() > 0){
            Boolean notAMember = true;
            for(User u : allUsers){
                if(usergroupIds.contains(u.Id)){ 
                    notAMember = false;
                }else{
                    nonUsers.add(u);
                }
            }
         }
        for(Group queue : queues){
             if(queueIdToMembersMap.containsKey(queue.Id)){
               populateQueueInfo(queue, gmList, nonUsers, userMap);   
              }         
          }   
            //To Reduce heap size
            queues=null;
            //allUsers=null;
          gmList=null;    
    }

    public void populateQueueInfo(Group theQueue, List<GroupMember> currentMembers, List<User> nonMembers, Map<Id, User> userMap) {
        queueList.put(theQueue.Id, new QueueInfoWrapper(theQueue, currentMembers, nonMembers, userMap));
    }

/*******************************************************************************************************/
Thanks,
M. Jagadeesh