Search
  • Tony Nelson

Execution Order of Apex

I would like to give you some tips concerning the execution order of Apex, as I myself once had a hard time trying to figure it out. I realized the Apex class processing was executed in a different order when there is an ‘action processing’ in a page tag on a Visualforce page.


Please refer to the below source codes for your investigation:

<span style="color: #000080;">&lt;apex:page controller="CheckController" action="{!init}"&gt;</span>
<span style="color: #000080;"> &lt;apex:form &gt;</span>
<span style="color: #000080;"> &lt;apex:outputText value="{!Count}" /&gt;</span>
<span style="color: #000080;"> &lt;c:CheckComponent /&gt;</span>
<span style="color: #000080;"> &lt;/apex:form&gt;</span>
<span style="color: #000080;">&lt;/apex:page&gt;</span>
<span style="color: #000080;">public class CheckController extends CheckBaseController {</span>
<span style="color: #000080;"> public CheckController() {</span>
<span style="color: #000080;"> System.debug('◆◆◆◆CheckController:START');</span>
<span style="color: #000080;"> System.debug('◆◆Constructor');</span>
<span style="color: #000080;"> System.debug('◆◆◆◆CheckController:END');</span>
<span style="color: #000080;"> }</span>
<span style="color: #000080;"> public PageReference init() {</span>
<span style="color: #000080;"> System.debug('◆◆◆◆init:START');</span>
<span style="color: #000080;"> System.debug('◆◆PageAction');</span>
<span style="color: #000080;"> System.debug('◆◆◆◆init:END');</span>
<span style="color: #000080;"> return null;</span>
<span style="color: #000080;"> }</span>
<span style="color: #000080;"> public String getCount() {</span>
<span style="color: #000080;"> System.debug('◆◆getCount()');</span>
<span style="color: #000080;"> return '123456789'; </span>
<span style="color: #000080;"> }</span>
<span style="color: #000080;">}</span>
<span style="color: #000080;">public virtual with sharing class CheckBaseController {</span>
<span style="color: #000080;"> public CheckBaseController() {</span>
<span style="color: #000080;"> System.debug('◆◆◆◆CheckBaseController:START');</span>
<span style="color: #000080;"> System.debug('◆◆Inherited Class Constructor');</span>
<span style="color: #000080;"> System.debug('◆◆◆◆CheckBaseController:END');</span>
<span style="color: #000080;"> }</span>
<span style="color: #000080;">}</span>
<span style="color: #000080;">&lt;apex:component controller="CheckComponentController"&gt;</span>
<span style="color: #000080;"> &lt;apex:outputText value=" | " /&gt;</span>
<span style="color: #000080;"> &lt;apex:outputText value="{!ComponentCount}" /&gt;</span>
<span style="color: #000080;">&lt;/apex:component&gt;</span>
<span style="color: #000080;">public class CheckComponentController extends CheckBaseController {</span>
<span style="color: #000080;"> public CheckComponentController() {</span>
<span style="color: #000080;"> System.debug('◆◆◆◆CheckComponentController:START');</span>
<span style="color: #000080;"> System.debug('◆◆Component Constructor');</span>
<span style="color: #000080;"> System.debug('◆◆◆◆CheckComponentController:END');</span>
<span style="color: #000080;"> }</span>
<span style="color: #000080;"> public String getComponentCount() {</span>
<span style="color: #000080;"> System.debug('◆◆getComponentCount()');</span>
<span style="color: #000080;"> return '987654321';</span>
<span style="color: #000080;"> }</span>
<span style="color: #000080;">}</span>

As you can see from above, when there is ‘action’ in the page tag, the processing is performed in the below order.


1. Inherited class constructor 2. Controller constructor 3. ’action’ processing in the page tab 4. The constructor of the inherited class of the component 5. Component constructor 6. Controller GET method 7. Component GET method


You can check the above order by the log.


However, if an ‘action processing’ does not exist in the page tag, the order was collapsed.


Here is the log at that time.


From the log, I found the processing on the component side was executed before the processing on the controller side.


1. The constructor of the inherited class of the component 2. Component constructor 3. Inherited class constructor 4. Constructor controller 5. Controller GET method 6. Component GET method


If there is ‘action’ in the page tag, the processing is executed in the “controller → component” order, however, if there is no ‘action’ the order of “component → controller” is applied.


Usually, there’s no major issue triggered by the difference in order, however, I would like to mention one point; when you are processing using Cookie.


For example, you execute the process to set a value to Cookie in the constructor processing.


You get different results by executing the processing before or after the constructor if there is a process using the Cookie value.


If constructor processing is executed before, there should be no problem as the latest Cookie information is set at all times.

However, if component processing is executed prior, it will be executed with the old Cookie information as it is before renewing Cookie.


Since this could happen, if you need to process a Cookie or if you don’t want the component processing to be done before anything else, please pay additional attention to the presence of ‘action’ in the page tag.


This investigation was done in the API 27.0 environment. I am not sure but perhaps the action could be consolidated in future upgrades. I personally prefer the order of “controller first and the component second”, but we’ll see…

0 views

© 2019 TerraSky. All Rights Reserved, TerraSky, Inc.