+ Start a Discussion
Radhe Shyam.Radhe Shyam. 

Query: Bulkify Apex Methods Using Collections In Methods

Hello Guys,  In my scan report, I am getting the issue : Query: Bulkify Apex Methods Using Collections In Methods
I have written some nested for loops but no any DML and Query in any loop, why I am getting this error,
Here is the code snipt.

Please HELP
 
if(!rule_wise_records.isEmpty() && !rule_wise_Users.isEmpty()){
            System.debug('I am in Maps');
            list<string> subscridersId = new list<string> ();
            list<sObject> recordsToFollow2 = new list<sObject> ();
            System.debug('Rule Wise Records ' +json.serialize(rule_wise_records));  //This map Contains key and List Of Sobjects
            System.debug('Rule Wise rule_wise_Users ' +json.serialize(rule_wise_Users)); //This map Contains key and List Of Strings
            for(id rule : rule_wise_records.keySet()){
                
                System.debug('Key =====>' +rule);
                
                if(rule_wise_records.containsKey(rule))
                    recordsToFollow2 = (list<sObject>) rule_wise_records.get(rule);
                System.debug('I am in Maps Records '+recordsToFollow2);
                if(rule_wise_Users.containsKey(rule))
                    subscridersId = rule_wise_Users.get(rule);
                System.debug('I am in Maps Users '+subscridersId);
                Set < String > toRemoveDuplicates = new Set < String > ();
                if(subscridersId!=null && subscridersId.size()>0)
                    toRemoveDuplicates.addAll(subscridersId);
                List < String > uniqueId = new List < String > ();
                uniqueId.addAll(toRemoveDuplicates);
				
				
                string parentId;
                string uesrIds ='';
                for (sObject eachRec: recordsToFollow2) {
                    parentId = ((ID) eachRec.get('id')); //.substring(0,15);
                    //for (integer i = 0; i < uniqueId.size(); i++) {
                    for (String sId : uniqueId) {
                        EntitySubscription e = new EntitySubscription();
                        uesrIds = sId;
                        string key = parentId.substring(0, 15) + '@' + uesrIds.substring(0, 15);
                        if (Record_Wise_SubscriberId.ContainsKey(key) == false) { 
                            e.ParentId = parentId;
                            e.SubscriberId = sId;//userIds[i];
                            ES_ListToinsert.add(e);
                            System.debug('I am in Maps ES_ListToinsert '+ES_ListToinsert);
                            
                            if(!map_SubEntityWiseFollowRules.containsKey(key)){
                                map_SubEntityWiseFollowRules.put(key, rule);
                            }
                        }
                    }
                    
                }
                
            }
            
}

 
Tej PalTej Pal
Hi Radhe,

Bulkify Apex Methods Using Collections In Methods is not related to DML/SOQL error. It is scan warning & tell us we can utilize uses of list/set/map in our code.

If this answers your question mark Best Answer it as solution and then hit Like!
 
Radhe Shyam.Radhe Shyam.
Hi, thnks. But I used all collections wherever it's required. Then why it is showing error Warm Regards Radhe Shyam
Nikhil Verma 6Nikhil Verma 6
Hi Radhe,
You are having 3 nested for loops and that is why you are getting the warning message. You can use Maps to avoid your nested loops. You will still have multiple loops (maybe more than 3, depending on your logic) but neither will be inside another. It simply reduces the processing time of the code.