+ Start a Discussion
Susan LoveSusan Love 

Too many SOQL queries@ 101

I have inherited some code and I am getting a SOQL error for below:

for(Contract c:contractListToCreate){ if(c.RecordTypeId == INITIAL_CONTRACT_RT){ contractLineItemsToCreate.addAll(CreateLineItemsForContract(c,c.Related_Opportunity__c,oppToLineItemMap.get(c.Related_Opportunity__c))); } else { contractLineItemsToCreate.addAll(CreateRenewalLineItemsForContract(c,c.Related_Opportunity__c,renewalProductList,initialSubscriptionPrice,initialSubscriptionListPrice)); } }

Can anybody help resolve this?

UC InnovationUC Innovation
Hi Susan,

You're probably querying inside CreateLineItemsForContract or CreateRenewalLineItemsForContract. If you are, then you would be calling the query inside a for loop, which may be called more than 101 times. I would suggest altering your code to avoid calling any queries inside for loops!

Hope this helps!
Hi Susan,
               You are getting SOQL 101 Error because of  Salesforce Governor Limits.
What is Governor Limit?
Apex runs in a multitenant environment, the Apex runtime engine strictly enforces limits to ensure that runaway Apex code or processes don’t monopolize shared resources.
How to Overcome Governor limit or how to Solve SOQL 101 Error:
Batch Apex Governor Limits
These are the governor Limits you need to keep in mind when dealing with Batch Apex
Up to five queued or active batch jobs are allowed for Apex.
A user can have up to 50 query cursors open at a time. For example, if 50 cursors are open and a client application still logged in as the same user attempts to open a new one, the oldest of the 50 cursors is released. Note that this limit is different for the batch Apex startmethod, which can have up to five query cursors open at a time per user. The other batch Apex methods have the higher limit of 50 cursors. Cursor limits for different Force.com features are tracked separately. For example, you can have 50 Apex query cursors, 50 batch cursors, and 50 Visualforce cursors open at the same time.

A maximum of 50 million records can be returned in the Database.QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed.

If the start method returns a QueryLocator, the optional scope parameter ofDatabase.executeBatch can have a maximum value of 2,000. If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 2,000 records. If the start method returns an iterable, the scope parameter value has no upper limit; however, if you use a very high number, you may run into other limits.

If no size is specified with the optional scope parameter of Database.executeBatch, Salesforce chunks the records returned by the start method into batches of 200, and then passes each batch to the execute method. Apex governor limits are reset for each execution of execute.

The start, execute, and finish methods can implement up to 10 callouts each.

Batch executions are limited to 10 callouts per method execution.

The maximum number of batch executions is 250,000 per 24 hours.

Only one batch Apex job's start method can run at a time in an organization. Batch jobs that haven’t started yet remain in the queue until they're started.
Note that this limit doesn't cause any batch job to fail and execute methods of batch Apex jobs still run in parallel if more than one job is running.

Sample Code
Batch Class :
global class ExampleBatchClass implements Database.Batchable<sObject>
global ExampleBatchClass()
{ // Batch Constructor } //
Start Method global Database.QueryLocator start(Database.BatchableContext BC)
{ return Database.getQueryLocator(query);
// Execute Logic global void execute(Database.BatchableContext BC, List<sObject>scope)
{ //
Logic to be Executed batch wise
global void finish(Database.BatchableContext BC)
{ // Logic to be Executed at finish