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
ashish gupta 61ashish gupta 61 

Need to avoid SOQL inside for Loop

Hi All,
I have to avoid User Soql inside for loop. I have a custom object filldata__c. I need each user record for every filldata ID and want to avoid this user SOql query in for loop

 List<fill_Data__c> fillDatalist = [Select id,CreatedById,Action_Type__c from fill_Data__c Where Action_Type__c='BulkParent'];
for(fill_Data__c obj:fillDatalist){
    fillIDWithCreatedBYIDMap.put(obj.ID,obj.CreatedById); 
}
for(ID fillID :fillIDWithCreatedBYIDMap.keyset()){
    if(fillIDWithCreatedBYIDMap.containsKey(fillID)){
        
       User user=[select ID,FirstName,LastName,email from user where Id =:fillIDWithCreatedBYIDMap.get(fillID) limit 1];  // I  want to avoid this user SOql query in for loop
    }
}
Please check above code and provide your solution It would be great help.
AbhishekAbhishek (Salesforce Developers) 
Please refer to the following link,
 
http://wiki.developerforce.com/page/Apex_Code_Best_Practices

It will help you.