Loading

Increase Apex code character limit

Fecha de publicación: Oct 21, 2025
Descripción

Apex runs in a multi-tenant environment. The Apex runtime engine strictly enforces limits to ensure that runaway Apex code or processes don’t monopolize shared resources. We receive requests from customers to increase the Apex code character limit in an org.

 

As of Summer ’18, the default Apex code character limit is 6 MB. To review the current limit and utilization in your org, from Setup, enter Apex Classes in the Quick Find box, then select  Apex Classes. Review the information in the Percent of Apex Used section.

 

Salesforce urges you to be proactive in monitoring your org’s utilization, especially as you approach the character limit.  Be aware that the process of increasing the limit will not occur within hours. Salesforce recommends that you start the process at least a week before you expect to hit the limit, and even earlier if remedial steps are required.

 

NOTE: As a general rule, regularly compile all Apex code in production and fix compilation failures. You must also ensure that all tests pass, and provide at least the required minimum level of code coverage.

Solución

Salesforce Support can help with increasing the default Apex code limit for eligible orgs. However, we want to ensure you are following all the best practices before we approve the limit increase. Here are a few key points to note.

 

Managed packages: This limit does not apply to managed packages. The code in the packages belongs to a namespace that’s different from the code in your org.

 

Test classes: This limit also does not apply to any code included in a class defined with the @isTest annotation. Ensure all the test classes are correctly annotated. To identify test classes that aren’t annotated correctly, you can run this sample code in an anonymous block.

 

NOTE: The following code snippet is provided for convenience. It may not be accurate in all cases. All Apex in the org must use Salesforce API version 24.0 and later, which is when including @isTest on test classes became mandatory.

Integer totalchar=0;  
for(ApexClass a: [Select Id, Name, Body from ApexClass]) { 
    if(a.body.containsIgnoreCase('testmethod') && !a.body.containsIgnoreCase('@isTest')){ 
        System.debug(a.Id + ' ' + a.name + ' ' + a.body.length()); 
        totalchar = totalchar + a.body.length(); 
    } 
} 
System.debug(totalchar); 

 

Invalid test practices: Check classes and make sure they don't have testing anti-patterns or redundant code that increases code coverage without being valid functional tests or business logic. A common anti-pattern is a class that has a considerable number of characters, but just arbitrary variable declarations and assignments:

// Don't use arbitrary declarations
Integer i1;
i1 = 1;

...

Integer i1000;
i1000 = 1;​

 

Invalid classes: Having Apex that fails to compile, such as an invalid class, can result in the denial of your Apex character limit increase request. An isValid flag determines whether a class is invalid or not. The isValid flag is set to true as long as dependent metadata has not changed since the class was last compiled. If any changes are made to object names or fields that are used in the class, including superficial changes such as edits to an object or field description, or if changes are made to a class that calls this class, then the class’s isValid flag is set to false

 

Recompile all the classes via Setup by entering Apex Classes in the Quick Find box. Then select  Apex Classes and then use Compile All classes to capture the correct code utilization. The following queries can help identify invalid Apex classes.

Select count() from ApexClass where isValid = false
Select Id, Name from ApexClass where isValid = false 

 

After you implement the best practices above, if you still require an Apex character limit increase, log a case with Salesforce Support and provide this information:

  •  A summary of the above analysis verifying that:

    • Test classes are correctly annotated.

    • There are no testing anti-patterns to artificially manipulate code coverage.

    • All Apex classes and triggers compile.

  •  The reason for the request.

  •  Your org’s current limit.

  •  The requested limit. Increases will be reviewed in increments to the nearest multiple of five million.

NOTES:

  • Professional Edition orgs cannot have this limit increased. 

  • Any anti-patterns present will restrict your ability to have this limit increased until they are removed. 

  • Any increases above 10 MB may require additional scrutiny from Support.

  • When updating orgs with zero Apex present, Support may request access to source orgs where the existing code is present.

Número del artículo de conocimiento

000382172

 
Cargando
Salesforce Help | Article