0

I have written the following nested query:

List<Account> accountList = [SELECT Id, Name,(Select Id,Name,Inactive__c FROM Contacts WHERE AccountId =:accountToInactive.Id AND Inactive__c=false),(Select Subject,Status,Type,whatid from Tasks where whatid=:accountToInactive.Id AND (Status!='Completed' AND Status!='Deferred')),(Select id,name,StageName from opportunities where AccountId =:accountToInactive.Id AND (StageName!='Won' AND StageName!='Lost' ) )from Account where Id=:accountToInactive.Id];

whenever I run the test class to complete the code coverage this query is imapacting the SOQL query limits because there are some other queries running already in the existing apex class. If i want to split this query using map how can i do it? How can i avoid the Query limit...

0

It is difficult to see how to fix this without seeing the rest of the test class - one suggesting however would be to wrap the code within the test method that is doing your work within a Test.startTest() and a test.stopTest() pair of calls. The best practice structure for apex tests is:

//Here is my setup code to prepare the system into the state it should be before I execute my test action

Test.startTest();

//Here I execute my code I am testing

Test.stopTest();

//Here I do my assertions

Calling these methods effectively renews all your governor limits for the code that follows Test.startTest() so that you can check the code under test does not break said limits.

  • if I want to use <Map> here instead of nested queries how can i user Map in this case?? how can split the above query using Map – Dhananjay Patil Jun 16 '16 at 14:26
  • Whether you select into a Map or into a List it will still count as the same number of queries. – Doug B Jun 16 '16 at 14:46
0

The query you cite just happens to be the query that is #101 in the transaction. Rewriting it won't save on the limits.

Most likely, you have queries nested in a for loop. A way to debug this is to turn debugging to trace SOQL calls (you should probably set System = ERROR, Validation = ERROR, WORKFLOW=ERROR, Apex=INFO to avoid noise in the log) and then you'll see what is happening over the life of the transaction.

@pbattison's suggestion is a good one to remove all queries used in setting up test data from the limits but even if you do so, the underlying objects/transaction being tested within the boundaries of Test.startTest() and Test.stoptest() may still exceed limits due to a non-bulkified implementation

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.