Loading

Troubleshooting Slow Salesforce Apex Production Deployments

Julkaisupäivä: Apr 27, 2026
Kuvaus

Salesforce Apex deployments to production environments require all test classes to run before any code can be deployed. This mandatory test execution is the most common reason production deployments are slower than sandbox deployments. While there is no single root cause for slow deployment speed, several factors contribute — including query complexity in test classes, batch class configuration, and test data setup patterns.
This article provides recommendations to optimize Salesforce Apex test classes and reduce deployment time.

Ratkaisu

The following approaches can significantly reduce Apex production deployment time.

1. Optimize SOQL Queries in Test Classes

Always include a WHERE clause in SOQL queries and ensure they operate on a small, specific subset of data. Queries without WHERE clauses or those scanning large data sets increase execution time.

2. Use Test Objects Instead of SOQL Queries in Test Classes

Avoid querying the database in test classes when you can create test data objects directly in memory.
Instead of querying:

Account acc = [Select name from Account where site='Toronto'];

Create objects directly in memory:

List<Account> accList = new List<Account>();
Account acc;
For (int i=0 ; i<10 ; i++) {
acc = new Account();
acc.name = 'test';
acc.site = 'Toronto';
accList.add(acc);
}

Creating objects in memory eliminates database query overhead during test execution, which reduces deployment time.

3. Consolidate Batch Class Tests Using a Single startTest/stopTest Block

Batch Apex classes use Test.startTest() and Test.stopTest() methods to execute asynchronous code synchronously during tests. Rather than having separate test methods for each batch class, consolidate all batch class tests within a single startTest/stopTest block. This reduces the overhead of multiple test execution contexts.

4. Use @testSetup for Shared Test Data

Instead of creating new objects in every test method, create a static utility class with methods such as getNewAccount() to instantiate test objects, or use the @testSetup annotation. Methods annotated with @testSetup create test records once per test class and make them accessible in every test method, reducing redundant data creation.
For additional reference, see Improve Performance with Custom Indexes using Selective SOQL Queries.

Knowledge-artikkelin numero

000384936

 
Ladataan
Salesforce Help | Article