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
Pranav SanvatsarkarPranav Sanvatsarkar 

LIMIT is not working in my SOQL query

I am trying to run this snippet in Developer Console,
Set<Id> setProdIds = new Set<Id>();
for( Product2 prod : [ SELECT Id FROM Product2 WHERE Division__c IN ('SIOUX FALLS','RAPID CITY')])
	setProdIds.add(prod.Id);
Set<Id> setOppIds = new Set<Id>();	
for( Integer iCount = 0; iCount <= 28; iCount++ )
{
	List<OpportunityLineItem> lstLineItems = [SELECT OpportunityId FROM OpportunityLineItem WHERE Product2Id IN: setProdIds AND Id NOT IN: setOppIds LIMIT 25000];
	system.debug('Size of list:'+lstLineItems);
	for( OpportunityLineItem lineItem : lstLineItems )
		setOppIds.add(lineItem.OpportunityId);    
}
List<Opportunity> lstOpportunities = [SELECT Id FROM Opportunity WHERE Division__c IN ('SIOUX FALLS','RAPID CITY') AND Id IN: setOppIds LIMIT 25000];
system.debug('Total opps to migrate:'+lstOpportunities.size());
Now, even if I have kept the LIMIT 25000, it throws error "System.LimitException:50001 rows".

Thanks in advance!
 
sandeep sankhlasandeep sankhla
Hi Pranav,

Line no 2 can also give the error as there is no limit in the query...please check ..
sandeep sankhlasandeep sankhla
Hey,

As a best practise we should not use for inside for and SOQL inside for..in your code you have used both inside for..that will always hit the limits..Please optmize your code ..

Thanks,
Sandeep
Madhanprabhu Thangadurai 1Madhanprabhu Thangadurai 1
Hi Pranav,

Since you are running the SOQL Select statement inside the for loop. Take out the Select statement from for loop.
Set<Id> setProdIds = new Set<Id>();
for( Product2 prod : [ SELECT Id FROM Product2 WHERE Division__c IN ('SIOUX FALLS','RAPID CITY')])
	setProdIds.add(prod.Id);
Set<Id> setOppIds = new Set<Id>();	
for( Integer iCount = 0; iCount <= 28; iCount++ )
{
	List<OpportunityLineItem> lstLineItems = [SELECT OpportunityId FROM OpportunityLineItem WHERE Product2Id IN: setProdIds AND Id NOT IN: setOppIds LIMIT 25000];
	system.debug('Size of list:'+lstLineItems);
	for( OpportunityLineItem lineItem : lstLineItems )
		setOppIds.add(lineItem.OpportunityId);    
}
List<Opportunity> lstOpportunities = [SELECT Id FROM Opportunity WHERE Division__c IN ('SIOUX FALLS','RAPID CITY') AND Id IN: setOppIds LIMIT 25000];
system.debug('Total opps to migrate:'+lstOpportunities.size());
Pranav SanvatsarkarPranav Sanvatsarkar
1. First query returns 3500 records. So, no issue.
2. I needed to run SOQL queries in FOR LOOP only, because I can get 50000 records at a time. I need to access 700000 records of OpportunityLineItems. The code is already optimized to take care of all governor LIMITS!

Can you provide any other solution?
nishad basha 7nishad basha 7

Hi, sandeep sankhla

 my requirement is creating visualforce page when i click sumbit button new user should be created. but  i need now user enter a values click submit button it will be display on error message.give one example for user object.How to solve that  one please give any ideas
nishad basha 7nishad basha 7

Hi, pranav_sanvatsarkar

  
 my requirement is creating visualforce page when i click sumbit button new user should be created. but  i need now user enter a values click submit button it will be display on error message.give one example for user object.How to solve that  one please give any ideas