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

 

Hi I am finding the same 101 error with the follwoing code. I tried chainging the code by taking the query with in the for loop but still finding the same issue . Please help me in this

 

trigger QuoteRequest_AfterInsertUpdate on GFQuote__c (after insert, after update)
{
List <GFQuote__c> qrList = new List <GFQuote__c>();
List <GFQuote__c> qrList1 = new List <GFQuote__c>();
List <GFQuote__c> qrList2 = new List <GFQuote__c>();
List <Opportunity> oppList = New List<Opportunity>();
if(KeyContactsController.stopTrigger == true)
{
Map <Id, GFQuote__c> qrOldMap = new Map <Id, GFQuote__c>();
Map <Id, String> currMap = new Map <Id, String>();
string qrStatus;
String currentUserID = Userinfo.getUserId();
User current_user=[SELECT Email FROM User WHERE Id = :UserInfo.getUserId()] ;
//Boolean qrFlag = false;
//Force-166

List <GFQuote__c> qr1 = new List <GFQuote__c>();
List <GFQuote__c> qr2= new List<GFQuote__c>();
List <GFQuote__c> qr3= new List<GFQuote__c>();

qr1 = [select Id,Launch_Date__c,Name,Deal__r.Product_Family__c,Error_Message__c,Deal__r.Country__c,Deal_Review_Comments_QR__c,Deal_Review_Date_QR__c, Deal_approved_by_QR1__c, Next_Oppty_To_Quote__c,Closed_Hold_Comments_QR__c,Deal__r.Product__c,Product_Type__c,Sub_Product__c,Deal__c, Quote_Request_Status_Number__c,Quote_Request_Status__c,BDM_Name__c,Deal_name__c from GFQuote__c where id IN :Trigger.newMap.keySet()];
//qrStatus = qr1.Quote_Request_Status__c;
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);

}

hitesh90hitesh90

Hi,

 

Here in your code you have written SOQL query in for loop because of this query limit has been exceed. you have to optimize your code.

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

SFDC _ CornerSFDC _ Corner

I have also changed the code like following but still unable to deploy this code getting exceptions

 

List <GFQuote__c> qr1 = new List <GFQuote__c>();
List <GFQuote__c> qr2= new List<GFQuote__c>();
List <GFQuote__c> qr3= new List<GFQuote__c>();

qr1 = [select Id,Launch_Date__c,Name,Deal__r.Product_Family__c,Error_Message__c,Deal__r.Country__c,Deal_Review_Comments_QR__c,Deal_Review_Date_QR__c, Deal_approved_by_QR1__c, Next_Oppty_To_Quote__c,Closed_Hold_Comments_QR__c,Deal__r.Product__c,Product_Type__c,Sub_Product__c,Deal__c, Quote_Request_Status_Number__c,Quote_Request_Status__c,BDM_Name__c,Deal_name__c from GFQuote__c where id IN :Trigger.newMap.keySet()];
//qrStatus = qr1.Quote_Request_Status__c;
List<Country__c> mcs = Country__c.getall().values();
System.debug('Rakesh'+ mcs);
//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 = :Trigger.new]);
for(GFQuote__c a : qr1)
{
qrStatus = a.Quote_Request_Status__c;
// Instead of a new list with SOQL added cd directly to the for loop
// 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 :[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])
{
//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);

Bhawani SharmaBhawani Sharma

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 queries in loop. Use collections to hold the query result and use these collection variables.