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
gautam maheshwarigautam maheshwari 

too many soql statements

Hi,

 

Our sfdc instance is highly customized and most of the code has been optimized using best practices.

Becuase of the no of triggers, classes, workflows that run while deploying, we hit the too many soqls limit.

 

What are the things we can do to avoid hitting this limit all the time.

Navatar_DbSupNavatar_DbSup

Hi,

 

Apex Code is the Force.com programming language used to write custom, robust business logic. As with any programming language, there are key coding principles and best practices that will help you write efficient, scalable code.

 

Best Practice #1: 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.

 

Best Practice #2: Avoid SOQL Queries inside FOR Loops

 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 the 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.

 Note: Executing queries or DML operations within an iteration adds risk that the governor limits will be exceeded.

 

Best Practice #3: Using Collections, Streamlining Queries, and Efficient For Loops

 It is important to use Apex Collections to efficiently query data and store the data in memory. A combination of using collections and streamlining SOQL queries can substantially help writing efficient Apex code and avoid governor limits.

 

Best Practice #4: Streamlining Multiple Triggers on the Same Object

 It is important to avoid redundancies and inefficiencies when deploying multiple triggers on the same object. If developed independently, it is possible to have redundant queries that query the same dataset or possibly have redundant for statements.

Note that it is very important to detail exactly how governor limits are applied when multiple triggers are deployed on the same object.

 

Best Practice #5: 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.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

Niket SFNiket SF

Hello gautam,

                        Try to optimise the code as much as possible. If you have too many trigger on a single object these type of errors will come. Just check it out that can you merge the trigger's i.e only one trigger for the one object and all the code will be pushed in the Trigger class so SOQL queries will be reduced.

 

In the classes try to use Map and List as much as you can.

 

Thanks

Nik's

 

Skype Name : niket.chandane

gautam maheshwarigautam maheshwari

we have followed all the best practices so far, but due to complexity we hit the limits !

Any other options we have !

Niket SFNiket SF

If you are sure you can directly contact to Salesforce.

All the best 

 

 

Thanks 

Nik's