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
SFDC _ CornerSFDC _ Corner 

Too many SOQL's: 101 error

/>List<Country__c> mcs = Country__c.getall().values();
System.debug('Rakesh'+ mcs);
for(GFQuote__c a : qr1)
{
qrStatus = a.Quote_Request_Status__c;
List <Opportunity> ct = New List<Opportunity>( [select Name, id,CloseDate,StageName, Deal_Review_Date__c, Reason_Closed_Lost__c, Deal_Lost_Description__c, Lost_Comments__c, Next_Opportunity_To_Quote__c from Opportunity where id = :a.Deal__c Limit 1]);
for(Opportunity d : ct)
{
qr2 = [select Id,Launch_Date__c,name from GFQuote__c where Deal__c = : d.id];
qr3 = [select Id,name,Quote_Request_Status__c from GFQuote__c where Deal__c = : d.id AND Quote_Request_Status__c != 'QR Lost'];
//FORCE-246
if (a.Launch_Date__c== NULL)
{
a.Launch_Date__c=d.CloseDate;
oppList.add(d);


vishal@forcevishal@force

Within this part of your code,

 

 

for(GFQuote__c a : qr1)
{
qrStatus = a.Quote_Request_Status__c;
List <Opportunity> ct = New List<Opportunity>( [select Name, id,CloseDate,StageName, Deal_Review_Date__c, Reason_Closed_Lost__c, Deal_Lost_Description__c, Lost_Comments__c, Next_Opportunity_To_Quote__c from Opportunity where id = :a.Deal__c Limit 1]);
for(Opportunity d : ct)
{
qr2 = [select Id,Launch_Date__c,name from GFQuote__c where Deal__c = : d.id];
qr3 = [select Id,name,Quote_Request_Status__c from GFQuote__c where Deal__c = : d.id AND Quote_Request_Status__c != 'QR Lost'];
//FORCE-246
if (a.Launch_Date__c== NULL)
{
a.Launch_Date__c=d.CloseDate;
oppList.add(d);

}

 

 

You have so many queries inside a FOR LOOP. This shouldn't be done. You'll need to re-code this with a more efficient logic that doesn't need you to QUERY inside a FOR loop.

 

 

NOTE: 

 

This query: qr2 = [select Id,Launch_Date__c,name from GFQuote__c where Deal__c = : d.id]; is also within the loop but I do not see qr2 getting used anywhere?

Yoganand GadekarYoganand Gadekar

Hey, you are getting the error as you are having a query within for Loop. As a best practice you should avaid that

VenkataNarasimhaRao.vutlaVenkataNarasimhaRao.vutla

Many ways we have to handle "Too Many SOQL 101" 

 

Below are the few ways.

 

1. We can use inner queries.
2. Use Test.startTest() and Test.stopTest()
3. Don't write the SOQL inside a loop
4. Use List,set and maps efficiently instead of query the same data multiple times.
5. If we face this in trigger, use trigger.newmap.keyset() and trigger.oldmap.keyset() and list context variables in triggers.
6. we use aggregateresult to handle this.
7. If the data available in custom settings, then use Custom settings methods to get the records.

 

 

Thanks,

VenkataNarasimhaRao.Vutla

nara.dotnet@gmail.com

9705898616