What is Order of Execution in Salesforce

Every Salesforce developer or Admin should have a clear understanding of how Salesforce process and validates whenever a record is inserted or updated. From learning as well as from Salesforce interview perspective order of execution in Salesforce is very important.

The order of execution in Salesforce is one of the most common questions asked in an interview and is shown below :

order of execution in salesforce
order of execution in salesforce

Use the below code to share the infographics on your site!

<a href='https://sfdcwisdom.com/order-of-execution-in-salesforce/'><img src='https://i1.wp.com/sfdcwisdom.com/wp-content/uploads/2019/11/order-of-execution-in-salesforce-1-1.png?resize=768%2C1920&ssl=1' alt='Order of Execution in Salesforce' 540px border='0' /></a></p>

Triggers order of execution consideration

Order of execution in Salesforce cannot be guaranteed in case of multiple triggers on the same object. Let’s say there are two triggers that fire when a record is inserted, We cannot control or predict which trigger will be executed first.

   Like we have an order of execution in Salesforce, we also have order of execution in Visualforce Page. This is equally important and all top companies like Deloitte, pwc, Salesforce ask this in the interview however Still many candidates miss this because they only prepare for Order of Execution in Salesforce. Let us see what exactly is the order of execution in Visualforce Page.

Visualforce Page Execution

Server creates an instance of a visualforce component, extensions and controller when user view a page. The order in which all these elements are executed could determine how the page will be displayed to that user.

An instance of a visualforce component, extensions and controller are created by the server. The order in which all these elements are executed could determine how the page will be displayed to that user.

In a life-cycle of visualforce page it is also important to understand how the page was requested.

  • A get request is an initial request made on the page either when a user enters an URL or enter the page by clicking a link or button. 
  • A postback request is made when a page update is required during user interaction like users clicking on the save button.

Let us understand the execution order with the help of diagrams.

 

Get Requests

  1. The constructor methods written in controller or extensions are called.
  2. If the page has a custom component they are created and the controller/extensions are called is executed. If the attributes on the custom components are set using expressions, the expressions are evaluated after the constructors.
  3. <apex: page> component is evaluated and other methods like getter or setter are called.
  4. If the page contains <apex: form> component, all necessary info required to maintain the state of the database between page requests encrypted in a view state.
  5. The resulting HTML is sent to the browser and the browser executes any client-side application like JavaScript. 

 postback Requests

Order of Execution in postback request
  1. First of all the view state is decoded.
  2. After the view state is decoded, expressions are evaluated and set methods on the controller or extensions are executed.
  3. The action that was triggered by the postback request is executed and  If that action completes successfully, the data is updated or If the postback request returns the user to the same page, the view state is updated.
  4. The resulting HTML is sent to the browser.

Read the important Salesforce Interview questions asked in Top MNCs.

Reference: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_lifecycle.htm


1 Comment

Salesforce Interview questions you must know in-2020 - SFDC Wisdom · November 15, 2019 at 7:44 am

[…] you know we have an order of execution in the Visualforce page as well? You must read the Order of Execution blog for complete […]

Did you Like the content? Let me know

%d bloggers like this: