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
SFDC ROCKSFDC ROCK 

soql limit in Batch Apex

I am writing Batch Apex Query String where the soql limit I want to avoid.How can we avoid the limit in below batch code ?

     DateTime currentTime = System.now();
      DateTime ddd = currentTime.addHours(-2);
      list<Total__c> campaignInfluenceList = new List<Total__c>();
      campaignInfluenceList = [select Id,CampaignId__c,OpportunityId__c,CampaignId__r.unique__c from Total__c where lastmodifieddate>=:ddd];
            Set<Id> oppIds = new Set<Id>();
//how to avoid soql limit in below for loop.
            for(Total__c d:campaignInfluenceList){
                oppIds.add(d.OpportunityId__c);
             }
             
                  String str = 'opportunityCreatedFromLead';
     string query='Select Id,Name,unique__c,Type,Lead_ID__c,createdby.profile.Name,(select Id,CampaignId__c,OpportunityId__c,CampaignId__r.Source__c from Total__r ) from Opportunity where id=: oppIds ';
     
     return Database.getQueryLocator(query); 

Thanks,
Marvin MichumMarvin Michum
Hi Subodh,

1. Avoid SOQL Queries inside for loop

By moving queries outside of for loops, your code will run faster, and is less likely to exceed governor limits. A common mistake is that queries are placed inside a for loop. There is a governor limit that enforces a maximum number of SOQL queries. When queries are placed inside a for loop, a query is executed on each iteration and governor limit is easily reached. Instead, move the SOQL query outside of the for loop and retrieve all the necessary data in a single query.
Here is an example of a having a query inside a for loop:
https://developer.salesforce.com/page/Best_Practice:_Avoid_SOQL_Queries_Inside_FOR_Loops

2. Bulkify your Code

Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time. When a batch of records initiates Apex, a single instance of that Apex code is executed, but it needs to handle all of the records in that given batch. For example, a trigger could be invoked by an Force.com SOAP API call that inserted a batch of records. So if a batch of records invokes the same Apex code, all of those records need to be processed as a bulk, in order to write scalable code and avoid hitting governor limits.

Please check the below link for example

https://developer.salesforce.com/page/Apex_Code_Best_Practices

3.Querying Large Data Sets

The total number of records that can be returned by SOQL queries in a request is 50,000. If returning a large set of queries causes you to exceed your heap limit, then a SOQL query for loop must be used instead. It can process multiple batches of records through the use of internal calls to query and queryMore.

4. Use Filters in SOQL Query.

Let me know if this helps, or if I can be of more help :)