You need to sign in to do that
Don't have an account?
Apex Code Deployment and Limits
Hi,
I'm trying to deploy some code but I get an Error that I am over the 100 SOQL limit in one of my test Classes.
I don't have a lot of code (8%) which is covered over 80% by the test classes.
I recently installed unmanaged packages and it seems then when trying to deploy everything is tested (it takes a very long time).
Does the limit apply in a cumulative way to all test classes at once ? Should I have less than 100 SOQL queries in all my code?
Or, is the class on which the error is triggered the one that I should review to have less SOQL in this one only?
Why were I able to deploy if my classe is above the limits?
How can I made my test class independant from other packages in terms of these limitations?
thanks,
I would do a "Run All Tests" in your Sandbox org to identify which of the classes, triggers, or unit tests is spinning up >100 SOQL queries. Your limits apply per class invocation, not over your entire code base.
Odds are you have SOQL in a loop somewhere in your code, but the "Run All Tests" will help you identify it.
-Andy
Hi,
Thank you for your answer, I identified the Class, I was calling a class function in my trigger which was doing an SOQL query.
I tryed to reuse a function looking for specific contact on an account from a trigger on a custom object linked to accounts.
What is the more efficient way in the case I want to mutualize code in an apex function?
- initialize lists in the trigger and pass them to the function
- pass the trigger list in a function and there initialize lists with SOQL and loops
- do the SOQL in a for condition
What would you do?
thanks
The two key things to keep in mind when writing all of your code is bulkification and reusability (mutualization). Write your class method assuming you're going to get a list of 1000 Accounts.
In the class method, you're going to do some upfront queries to fill appropriate Sets, Maps, or Lists - then roll through your code after that.
Remember - never under any circumstances do any SOQL or DML within a for loop. Do it all upfront to fill Sets, Maps, and Lists - then iterate through those.
-Andy
Then, if I need a function giving a specific contact from an account, the good practice is to pass a list of account and to return a map<Account, Contact> that I call at the beginning of my code?
Correct.
Thank you for this answer.
Assuming that it's the same case case for DML statements, what is the good practice here if I need to insert Objects and related subObjects in a loop?
should I use temporary IDs to keep track of the relations?
Declare a new "List<Account> lstAccountsToInsert = new List<Account>();" before your loop, instanciate and populate your new Account object within your loop and add them to the List where you'd normally think to issue the DML insert.
For subobjects, insert all of your primary objects first, load them into a Map, and then you can reference them in your subobject.
Pseudocode: