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
SimrinSimrin 

more number if SOQL queries error.

Hi,

I have a simple SOQL query which is executed many times.

for(1000 times){
List<custom_Object_c > pList = [SELECT x__c FROM custom_Object_c ];
}

It gives me error that i execute more than required queries and gives a exception on page.

I want to find a solution where it will not give exception on page and execute the maximum times it could and retrived the maxium number of data in pList  and continue with that list, it may ignore the rest.

What can be solution.
Best Answer chosen by Simrin
Amit Chaudhary 8Amit Chaudhary 8

Try below code :--

Option 1:-
List<custom_Object_c > pList = [SELECT x__c FROM custom_Object_c ];
for(1000 times){
}

Option 2:- If you are using Loop to getting data then try below code

Set<Id> setId = new Set<Id>();
for(Account acc : trigger.New){
setId.add(acc.customObject__c);
}

Map<ID,custom_Object_c > pMap = new Map<Id,custom_Object_c >( [SELECT x__c FROM custom_Object_c where id in :setId]) ;


for(Account acc: trigger.new)
{
    if(acc.customObject__c != null && pMap.containsKey(acc.customObject__c))
   {
      custom_Object_c obj = pMap.get(acc.custom_Object_c );
   }
}

Please mark this as solution if this will help you

All Answers

ManojjenaManojjena
HI Simrin,

Basically what I understod is there is query inside for loop .Which should not .As we can query only 100 in one execution context .So you should remove query from loop  .

If possible post your code snippet fro which I will help you !

Let me know if it helps !

Thnaks 
Manoj
Amit Chaudhary 8Amit Chaudhary 8

Try below code :--

Option 1:-
List<custom_Object_c > pList = [SELECT x__c FROM custom_Object_c ];
for(1000 times){
}

Option 2:- If you are using Loop to getting data then try below code

Set<Id> setId = new Set<Id>();
for(Account acc : trigger.New){
setId.add(acc.customObject__c);
}

Map<ID,custom_Object_c > pMap = new Map<Id,custom_Object_c >( [SELECT x__c FROM custom_Object_c where id in :setId]) ;


for(Account acc: trigger.new)
{
    if(acc.customObject__c != null && pMap.containsKey(acc.customObject__c))
   {
      custom_Object_c obj = pMap.get(acc.custom_Object_c );
   }
}

Please mark this as solution if this will help you
This was selected as the best answer
sandeep sankhlasandeep sankhla
Hi Simirin, 

AS a Best Practise we should not use SOQL inside for loop...you can store data into set whatever data you are using inside query filter..then after for loop you can do the same query where that filter IN: setFiltersIds....

You can make use of set and map to avoid SOQL inside for loop..

Thanks,
Sandeep
SimrinSimrin

cant i resolve this issue with try catch.
The logic is bit complex and the code too for me to explain and paster here...

Can the try catch catch the error and let the code to continue after the block but it should get the rows it has retireved before failing.

isuue is my rows is not always 100 or 1000 it depends on many parameters and i can fix limit

ManojjenaManojjena
Hi Simrin,

Row you can retrive 50000  in on eexecution if you will use @readonly annotation then you can skip .However as your SOQL query is inside for loop it can call  the query method 100 times not mote then that .When it will reach 101 it will throw an error 101 SOQL error .

 
varun_vatsavarun_vatsa
No, the error you are getting is a Governor limit error and this cannot be catched, in order to run the code successfuly you need to optimise it. if you could paste the code, one of us will surely be able to help you out, and that will not only help you here but will save you trouble in future as well. 
ManojjenaManojjena
Hi Simrin ,

You can check the limit once you reach that then you can handle it By the help of LIMIT class in apex .

Please check the below link it will help  !

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_limits.htm  

Check two methds in the class 
1.getDMLRows()
2.getLimitDMLRows()

Let me know if it helps .

Thanks 
Manoj