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
Anurag Reddy KanchimireddyAnurag Reddy Kanchimireddy 

Too many SOQL queries nested loop

Hi,

Im using nested loop for fetching case comments depending on the languages(its a list), To achieve this I have implemented nested Loop.
1st loop fetches all the languages from the custom object and 
2nd loop fetches case comments depending on the language.
While im using this  "Too many SOQL queries" is firing please suggest me solution.

 

Best Answer chosen by Anurag Reddy Kanchimireddy
Dev-FoxDev-Fox
Hi Anurag,
Just make it somple like below:
set<strinng> langSet = new set<string>();
for(language_obj obj:[select lang_field from language_obj where <your query condition>]){
  langSet.add(obj.lang_field);
}
for(casecomment commObj:[select id, other_fields from casecomment where lang_field IN :]langSet){
  //do process here
}
hope this helps

All Answers

Anthony McDougaldAnthony McDougald
Good Afternoon Anurag,
Hope that your day is off to an amazing start. It sounds like you coded the SOQL queries inside the code block of the for loops which is causing you to hit the governor limits. Please modify your code so that it's outside the loops. Hope this helps and may God bless you abundantly.
Best Regards,
Anthony McDougald
Dev-FoxDev-Fox
Hi Anurag,
Just make it somple like below:
set<strinng> langSet = new set<string>();
for(language_obj obj:[select lang_field from language_obj where <your query condition>]){
  langSet.add(obj.lang_field);
}
for(casecomment commObj:[select id, other_fields from casecomment where lang_field IN :]langSet){
  //do process here
}
hope this helps
This was selected as the best answer
Anurag Reddy KanchimireddyAnurag Reddy Kanchimireddy

Hi Dev-Fox,

I have followed your above code and it helped a lot,
there is one more problem in the, In Second loop SOQL where condition I need to add language condition as weel as case comment parent id. Can i use two IN: conditions for language(where lang_field IN :) and also for comment case ID??