+ Start a Discussion
Prateek Jain 52Prateek Jain 52 

Need to take out For Loop Inside another For loop

In apex i trying to take out below for loop inside for loop .
Below Code base is iterating list if Sobject records.
for(SObject sObj: listRecords){
      Map<String,Vals> RowsValsItemListMap = new Map<String,Vals>();
      List<Vals> ValsItemList = new List<Vals>();
      Map<String, Object> fieldsToValue = sObj.getPopulatedFieldsAsMap();
      system.debug('Value for is: fieldsToValue' +fieldsToValue);
      map01.put(sObj, fieldsToValue);
      for (String fieldName : fieldsToValue.keySet()){
          system.debug('Value for fieldName is: ' +fieldName);                  
          Vals ValsItem = new Vals();                    
          ValsItem.val = fieldsToValue.get(fieldName);                
          //ValsItem.val = (String)fieldsToValue.get(fieldName);                        
          RowsValsItemListMap.put(fieldName.toLowerCase(), ValsItem);                

Any help will be really appriciated.
ShirishaShirisha (Salesforce Developers) 
Hi Prateek,


I can see that you have only one for loop in the provided code for now.However,if you have for loop inside for loop then you can query and store the results in the list as suggested in the below blog.

I would suggest you to go through the blog which has examples to make you understand easily.


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

Warm Regards,
Shirisha Pathuri