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
steve456steve456 

Too many SOQL Queries--------Very Urgent

for(Order_Entry__c oe:oldOrderDetails)

{
if(oe.RecordTypeId==[Select Id from RecordType where Name='RKVZ' and SobjectType='Order_Entry__c'].Id)


if(oldOrderDetailMap.get(oe.Order_Number__c)==null)
{
oldOrderDetailMap.put(oe.Order_Number__c,new List<Order_Entry__c>{oe});
}
else
{
oldOrderDetailMap.get(oe.Order_Number__c).add(oe);
}
}

 

 

******************

Error is pointing to the highlighted line.....I know I have written a SOQL inside a for loop....How do i get red of this now........Very Urgent

prady-cmprady-cm

You should not have query inside a for loop.  this would invariably exceed the governor limits.

store the recordtype in a string. Have this before your for loop

string recordtypeid=[Select Id from RecordType where Name='RKVZ' and SobjectType='Order_Entry__c'].Id

 and in the if statement compare it using the string

if(oe.RecordTypeId==recordtypeid)

 Hope this helps

trictric

Hi 

 

Can u try inline soql query in place of red lines.?That way a list will be able to process more records than usual.

 

Thanks,

Trick

 

 

craigmhcraigmh

Yeah, what prady said. You always want to do queries outside of a loop.

 

You can even use collections for a query to reduce the number of SOQL queries.

 

Set<string> TypesToFilterOn = new Set<string>();
//load types here
List<RecordType> recordTypeIDs = [Select Id From RecordType Where Name = 'RKVZ' And SobjectType In :TypesToFilterOn Limit 10000];
for(RecordType rt: recordTypeIDs) {
	//do stuff here
}

 

soni rajputsoni rajput

Hi,
In your code there is Soql query in for loop thats why this error is coming..
put this soql statement outside for loop 

 


RecordType re = [Select Id from RecordType where Name='RKVZ' and SobjectType='Order_Entry__c'];

for(Order_Entry__c oe:oldOrderDetails)

{
if(oe.RecordTypeId==re.Id)

if(oldOrderDetailMap.get(oe.Order_Number__c)==null)
{
oldOrderDetailMap.put(oe.Order_Number__c,new List<Order_Entry__c>{oe});
}
else
{
oldOrderDetailMap.get(oe.Order_Number__c).add(oe);
}
}