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
InspiredInspired 

Max number of records retrieved by SOQL queries - TEST METHOD

I have a question about the location of a user MAP in the following code. The line is

 

 

Map<ID, User> u = new Map<ID, User>([select id, email from user where isActive = true]);

 

When I place the code before the first SET the test method returns an error because the user MAP exceeds 500. When I place the user MAP further into the code the test method does not returns an error because the user MAP does not exceed 500.

 

Why does the location of the user MAP impact the test code?

 

 

public with sharing class opportunityStrategyInitialization {
public static void start( Opportunity[] o) {

datetime d = datetime.now();
// I place the Map here and exceed 500
// I have 285 active users
Map<ID, User> u = new Map<ID, User>([select id, email from user where isActive = true]);

Set<ID> oppIDs = new Set<ID>();
for ( Opportunity i:o ) {
if (i.ccsREquestStrategy__c == True) {
oppIDs.add(i.id);
i.ccsRequestStrategy__c = False; // reset the flag
i.ccsStrategyFinalized__c = False; // reset the flag
i.ccsFinalizedDate__c = null;
i.ccsLastStrategyRequest__c = d; // stamp the time
}

}


if (oppIDs.size() > 0) {
List<Opportunity> opps = new List<Opportunity>();
opps = [select ID, Name, ccsRegistrationLevel__c, Account.Name, ccsChannel__c, ownerid, (select ID from StrategyQueue__r) from Opportunity where ID in :oppIDs];

Map<ID, StrategyQueue__c[]> oppStrategyRequests = new Map<ID, StrategyQueue__c[]>();
for (Opportunity eachOpportunity: opps){
oppStrategyRequests.put(eachOpportunity.ID, eachOpportunity.StrategyQueue__r); // child relationship name ( + __r)
}

List<StrategyQueue__c> sqx = new List<StrategyQueue__c>();

// I place the Map here and do not exceed 500
// I have 285 active users
Map<ID, User> u = new Map<ID, User>([select id, email from user where isActive = true]);


for (Opportunity x:opps) {
// delete any existing requests
sqx = oppStrategyRequests.get(x.ID);
delete sqx;

//create a strategy queue object
StrategyQueue__c sq = new StrategyQueue__c();
sq.ccsAccountName__c = x.account.name;
sq.ccsChannel__c = x.ccsChannel__c;
sq.ccsOpportunity__c = x.ID;
sq.ccsOpportunityName__c = x.name;
sq.ccsOpportunityOwner__c = x.ownerid;
sq.ccsRegistrationLevel__c = x.ccsRegistrationLevel__c;
insert sq;

// email link to opportunity owner
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {u.get(x.ownerID).email};
mail.setToAddresses(toAddresses);
mail.setSenderDisplayName('SFDC Strategy Request');
mail.setSubject('Please finalize opportunity strategy ASAP');
mail.setPlainTextBody('http://xxx.test.cs3.force.com/xxx?id=' + sq.id);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}
}
}

static testMethod void testOpportunityStrategyInitialization() {
//create a new Account
Account a = new Account();
a.Name = 'TestCodeCoverage';
a.BillingCountry = 'USA';
insert a;

//create a new opportunity
Opportunity o = new Opportunity();
o.Name = 'Test';
o.AccountId = a.ID;
o.Amount = 100;
o.StageName = 'New';
o.CloseDate = Date.valueOf('2050-01-01');
o.ccsRequestStrategy__c = False;
insert o;

//create a new strategyQueue
StrategyQueue__c sq = new StrategyQueue__c();
sq.ccsOpportunity__c = o.id;
sq.ccsStrategyFinalized__c = False;
sq.ccsRegistrationLevel__c = 'Test';
sq.ccsChannel__c = 'Test';
insert sq;

o.ccsRequestStrategy__c = True;
update o;
}
}

 

 

 

 

 

Message Edited by Inspired on 01-11-2010 12:48 PM
ahab1372ahab1372

there is a limit on the total numbers of rows returned by all queries that are executed in the test method context. That includes any triggers that are fired upon the DML statements for account or opportunity. So it is not necessarily that one query alone which you are using to populate the map..

I assume that if you put this one line further down (within the if statement), it is not be executed. I suggest you add some more System.Debug statements and use the Debug Log to see what happens in detail. That will also tell you what query returned how many rows, including any other triggers that were executed.

 

Check here for governor limits:

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_gov_limits.htm