Skip to content. | Skip to navigation

Personal tools
Log in
Sections
You are here: Home Database Salesforce Apex & Visualforce Dealing with Apex Limits

Dealing with Apex Limits

Limits stink, but you can learn to deal

Salesforce doesn't want you to run too much code, so they just don't let you.  The Apex Language Ref lays out the limits on page 142.  The basic message is to never run a query in a loop, and try not to get or update the same records more than once.  Instead, do one set of queries, use code to process results, and then do one set of DML statements.

There is this thing called StartTest that you are supposed to run after you create test data.  It is supposed to give you relief from the limits, but I did not quite understand.  To figure it out, I ran the following test:

	public static testmethod void TestStartTest() {
		system.debug('Query row limit: ' + limits.getLimitQueryRows().format());
		system.debug('Query rows used: ' + limits.getQueryRows().format());

		insert CreateMultipleTestContacts(64);
		system.debug([select id from contact where LastName = 'Doppleganger']);

		system.debug('Query row limit: ' + limits.getLimitQueryRows().format());
		system.debug('Query rows used: ' + limits.getQueryRows().format());

		test.startTest();
		account a = new account(name = 'Test');
		insert a;

		insert CreateMultipleTestContacts(9);
		
		system.debug('Query row limit: ' + limits.getLimitQueryRows().format());
		system.debug('Query rows used: ' + limits.getQueryRows().format());
		
		insert CreateMultipleTestContacts(13);
		system.debug([select id from contact where LastName = 'Doppleganger']);
		
		system.debug('Query row limit: ' + limits.getLimitQueryRows().format());
		system.debug('Query rows used: ' + limits.getQueryRows().format());
		
		test.stopTest();

		system.debug('Query row limit: ' + limits.getLimitQueryRows().format());
		system.debug('Query rows used: ' + limits.getQueryRows().format());
	}

And this is what happened:

Debug Log:

line 289, column 9: Query row limit: 500
line 290, column 9: Query rows used: 0

INSERTED 64 CONTACTS

line 295, column 9: Query row limit: 500
line 296, column 9: Query rows used: 266

START TEST and INSERTED 1 ACCOUNT 
NOTE THE MESSAGE ABOUT LIMITS CHANGING:

line 300, column 9:     Changing testing limit context based on DML operation of size: 1

INSERTED 9 CONTACTS

line 302, column 9: Query row limit: 1,000
line 303, column 9: Query rows used: 27

INSERTED 13 CONTACTS

line 308, column 9: Query row limit: 10,000
line 309, column 9: Query rows used: 92

STOP TEST -- LIMITS BACK TO BEFORE START

line 313, column 9: Query row limit: 500
line 314, column 9: Query rows used: 26

So, you get one whole set of limits before StartTest, another after.  Without StartTest, I could do only 500 query rows, but I get another 1000 after StartTest, for a total of 1500.  Same with all the other limits -- StartTest resets them.

By the way, the reason I am using so many query rows just to insert a few contacts is that all our Householding code is getting triggered every time I insert.  It runs queries, and they count against my limit.

 

Document Actions

Comments (0)

« February 2012 »
February
MoTuWeThFrSaSu
12345
6789101112
13141516171819
20212223242526
272829