---
schema_version: "1"
id: 7cf9587391fc922b36e4f695d2e644d530c023f35b4a7f0004b1ccb3c0d08895
title: Deferred Creation of Dynamic Components
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/salesforce-pages-developers-guide/guides/pages/dynamic-vf/pages-dynamic-vf-components-deferred-creation.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/guide/pages-dynamic-vf-components-deferred-creation.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: ba0d51ad0ec06eac10eff190a142650c8d5b3ecb812e3175078347414ae0c73f
products:
  - Visualforce Developer Guide
  - Platform Developer Content
---

# Deferred Creation of Dynamic Components

The Apex method that defines a dynamic component is by default executed at page load time, before any action method that’s defined for the page is run. Set the `invokeAfterAction` attribute of a dynamic component to `true` to wait for page actions to be completed before the method that creates the dynamic component runs. This enables you to design dynamic components that change depending on the result of, for example, a page initialization action or a callout.

Here’s a page that has a single dynamic component, which is created after the page’s action method, `pageActionUpdateMessage`, is completed.

<!-- otherprops: apex_page -->
```markup
<apex:page controller="DeferredDynamicComponentController" 
    action="{!pageActionUpdateMessage}" showHeader="false">
    
    <apex:dynamicComponent componentValue="{!dynamicComp}" invokeAfterAction="true"/>
  
</apex:page>
```

Here’s the associated controller that provides the dynamic component definition, and illustrates the effect of the `invokeAfterAction` attribute.

<!-- otherprops: apex_code -->
```controller
public class DeferredDynamicComponentController {

    private String msgText { get; set; }

    public DeferredDynamicComponentController() {  
        this.msgText = 'The controller is constructed.';
    }
    
    public Component.Apex.OutputPanel getDynamicComp() {

        // This is the component to return
        Component.Apex.OutputPanel dynOutPanel= new Component.Apex.OutputPanel();
        dynOutPanel.layout = 'block';
        
        // Child component to hold the message text
        Component.Apex.OutputText msgOutput = new Component.Apex.OutputText();
        msgOutput.value = this.msgText;
        dynOutPanel.childComponents.add(msgOutput);
        
        return dynOutPanel;
    }
    
    public Object pageActionUpdateMessage() {
        this.msgText= 'The page action method has been run.';
        return null;
    }
}
```

With the default behavior for dynamic components, the `msgText` value that’s set in the constructor is displayed by the dynamic component. Setting `invokeAfterAction="true"` on the dynamic component changes that behavior. The page waits for the `pageActionUpdateMethod` to be completed and *then* creates the dynamic component, and so the component displays the value for `msgText` that’s set in the `pageActionUpdateMessage` action method instead.

**note:** The `invokeAfterAction` attribute is available for dynamic components in pages set to API version 31.0 or later.

## Deferred Creation of Dynamic Components and Other Actions

`invokeAfterAction="true"` affects dynamic components immediately at page load time, because that’s when page actions run. Setting `invokeAfterAction="true"` reverses the order of component creation and *any* action method on the page. That is, the order of execution is changed for `action` methods on all of the following components.

-   `<apex:actionFunction>`
-   `<apex:actionPoller>`
-   `<apex:actionSupport>`
-   `<apex:commandButton>`
-   `<apex:commandLink>`
-   `<apex:page>`

When `invokeAfterAction="false"` is set on a dynamic component, the order of execution is as follows. This is the default behavior for dynamic components.

1.  Invoke the dynamic component’s creation method, which constructs the component.
2.  Invoke the action method.
3.  Rerender the page.

When `invokeAfterAction="true"` is set on a dynamic component, the order of execution is as follows.

1.  Invoke the action method.
2.  Invoke the dynamic component’s creation method, which constructs the component.
3.  Rerender the page.

**note:** In the second case, if the action method returns a PageReference, Visualforce will redirect the request to the new page, and the dynamic component’s creation method won’t be run. To avoid a possible order-of-execution bug, it’s a best practice that methods that create dynamic components don’t have side effects.

