+ Start a Discussion
Rajendra Prasad 44Rajendra Prasad 44 

need help for - nested for loop

how to avoid for loop in for loop here

for(account a: accounts){ 
                string[] perCamps = a.Campaign_new__pc.split(';');
                for(integer i=0; i<perCamps.size(); i++){
                    if(!conIdByCamName.containsKey(perCamps[i]))
                        conIdByCamName.put(perCamps[i], new list<string>{a.PersonContactId});
                    else conIdByCamName.get(perCamps[i]).add(a.PersonContactId);
                }

here "Campaign_new__pc" is Multi select Picklist field.
ShirishaShirisha (Salesforce Developers) 
Hi Rajendra,

Greetings!

Please refer the below thread for the example code which will help you in making changes according to your code.

https://developer.salesforce.com/forums/?id=906F0000000kEXaIAM

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
RituSharmaRituSharma
Ideally, you should not have one loop inside another. But trust me that sometimes you can't really avoid it. Obviously, going one more level is Big No. Based on shared code, I would not be able to suggest how exactly you may adjust your code. Would need to understand the complete logic.