Compile All Classes?

Does anyone know of any documentation on this function, “compile all classes” on the Apex Classes page? I’m finding nothing on or elsewhere.

(UPDATE regarding desire to automate at bottom of Question)

I’m being told by a less-than-trustworthy source (the maker of an SF plugin we use) that this is supposed to be used ANY TIME you upgrade an SF plugin. That alone is an overstatement i’m sure, and they are using it to stave off complaints about 1-2 minute Apex routines (processing a single record).

enter image description here

Any insights would be helpful. I’m not sure I even recall seeing this option in my 6+ years with Salesforce products.

UPDATE:   What I’d like is to be able to automatically run the “Compile All Classes” function daily at 6:45am. Being that I’m told by another source (SF Support) that it’s declarative, that apparently translates into “it cannot be scheduled”. I’d like to believe you can create a class (trigger?) which could be scheduled to run the function on a daily (possibly twice daily) basis. Once again, this is because the maker of a plugin to Salesforce believes this will fix our speed problems in THEIR product).

Scheduling code to run is “easy enough” (console or GUI) but I apparently don’t have something that CAN be scheduled to actually run this function.

I am in the midst of testing this, manually running it daily, to see if the performance increase happens… given that this all stems from a Salesforce “Pre-Compile” feature (likely what’s being described somewhat in comments below), and that it’s now fixed, in our Org, for SF standard objects, but it’s NOT doing anything for third party plugins, the hope is that this suggestion to auto run “Compile” each day will solve things.

UPDATE 2:   Salesforce support is “investigating” writing the code necessary to schedule Compile All Classes. If(/when?) I receive this, I will post it as an answer here. (noted on 2017-03-31)


The ability to Compile All Code has been around as long as Apex Code has been a thing. You’ll want to watch Peek Under the Hood of the New Apex Compiler, a Dreamforce 16 presentation that was later uploaded, that explains basically how the system works today, as well as plans for the near future. To summarize the video though, code is compiled mostly “on demand”, and generally occurs the first time some code requests a class has that expired from the caches, usually because it has been modified or one of its dependencies have been modified.

Generally speaking, though, you don’t usually need to use this feature in production. It’s primary benefit is for use during development in a Sandbox or Developer Edition org. For example, given class A and B, if B is called from A, and you delete B entirely or change it, A is invalidated, and has to be recompiled the next time it’s called. You can skip this compilation time for the first request after invalidation by forcing a compilation before you need it. This only speeds up the first call to the class after it’s been invalidated, as they later use the cache, as explained in the video I linked.

If a piece of code is constantly performing slowly, it is because the code is slow (not optimized/bulkified/whatever). There should only be one “slow load” that occurs after each upgrade, not every single call, or until you “compile all classes.” It’s a convenience feature that’s not often needed, and primarily of benefit during development to speed up load times while testing.

Also, as explained in the video, in the near future, compilation during deployment will occur before the actual deployment takes place, so deployments will take slightly longer, but will reduce the occurrences of the “slow load” that occurs for the first request after an upgrade, because the code will be pre-compiled and only has to be loaded from the cache.

Source : Link , Question Author : AMM , Answer Author : sfdcfox

Leave a Comment