We have some legacy code in one of Salesforce orgs. While going through code, I encountered this piece of code
List<query_metadata__c> dyQueries=[select id,query__c,object__c from query_metadata__c]; for(String query:dyQueries) { List<Sobject> objResults = new List<Sobject>(); objResults = Database.query(query); } SOQL queries are getting executed inside for loop which is against best practices. On further diving, I found out that these whole queries are getting fetched from a custom metadata and these queries are of different objects. This query list is going to static. Only 35 or less. No more queries are going to be added.
I was wondering if there is any way we could avoid this query inside for loop. To me they look okay for multiple reasons.
- Even if we execute each of these query individually out of for loop that is going to be 35 queries plus unnecessary repetitive code
- Since this list is static, no more queries are going to be added so it is always going to be 35 or less.
- This code is getting executed inside batch apex so the limit gets increased to 200.
Still, looking out for suggestions, to conform to best practises.