1

I have a problem, when i try to execute a static method , i get the error

too many soql queries 101 

here is the code that generates the error :

public void sauve(){
        list<poste__c> listepostes = [select id, name from poste__c where annee__c =: annee and id in (select poste__c from rubrique__c where CALENDAR_YEAR(CreatedDate) =: 2016) ];

 if(listepostes.size() > 0)
{
        for(poste__c po: listepostes)
        String var = calculpaie2.Calculrubrique(po.id,mois);
    }

}

this method called Calculrubrique (it has 400 lines of code and it was not written properly with many DML operations inside for loops) but i do not have time to rectify it as i have to deliver it very soon.

i have a list of employees (more than 300) and this method calculates the salary slip for each employee , i used to run tests for each one individually and it works properly but it's not that easy to do this manually , so i created this method to run tests on a bunch of people based on some parameters (month , year) , unfortunately i have this error and i do not know if it's possible to find a workaround without changing the code in the Calculrubrique method.

any ideas ?

5
  • Is it possible for you to post your Calculrubrique() methods code over here?
    – Samir
    Mar 9 '16 at 8:56
  • it's a 400 lines method , you will get ourself in a real mess (as i said it's not properly written) Mar 9 '16 at 9:01
  • i think the only way to overcome this error is to fix that method.Rewrite code that executes queries inside for loops by moving the query outside of the loop and extending it to query all necessary data. Please refer this link for further clarification developer.salesforce.com/page/…
    – Samir
    Mar 9 '16 at 9:05
  • yes , i know samir , i want to know just if there is a another way to do that without eventually changing the code. Mar 9 '16 at 9:31
  • another way would be to use batch as Keith suggested
    – Samir
    Mar 9 '16 at 9:33
3

A work-around is to wrap the code in Batch Apex where the query is done in the start method and the listepostes loop is done in the execute method using the list passed to that method. The batch size can then be set to something small e.g. 10 records (but you can experiment with that value) and the more than 300 employees will then be processed 10 at a time over a period of time.

This approach should allow you to avoid the governor limit error, but the processing will be done asynchrnously i.e. all the results won't be available until some time in the future (e.g. 30 minutes but the time is not predictable).

3
  • Thank you , but ............ i have never used a batch before and i do not know what is it (how much time should i spend to learn how to use batch ?) Mar 9 '16 at 9:07
  • @MerabtiiBRAHIM Only a few lines of code are involved and the link I included is a good place to start. So the core ideas you can get in an hour. You can vary the batch size via this Database.executeBatch(batchClassObject, scope) method. This is a core platform feature that you will probably find use for in other (non-workaround) situations.
    – Keith C
    Mar 9 '16 at 9:13
  • ok, thank you very much , i will take a look at that . Mar 9 '16 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.