+ 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++){
                        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,


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


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

Warm Regards,
Shirisha Pathuri
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.