Salesforce-Order-of-execution
Salesforce

Salesforce Order of Execution

Salesforce CRM offers an automated and efficient platform to business enterprises, helping them handle their customer database. There are several features, tools, components and add-ons pertaining to Salesforce, all providing specific services. The platform considerably reduces the time and effort put in by your sales team in order to carry out various business processes. Since it was introduced, Salesforce has aided many small and big businesses in recording, storing, assessing and analysing important customer data in their systems. Salesforce Development Companies have also been customising the software platform in order to suit specific needs of varied businesses.

Though Salesforce eases complicated business processes, the mechanism behind its building and implementation is quite complex. A Salesforce Developer needs to know every trick of the trade and understand every nuance of the platform in order to provide suitable services to their clients. It is important to understand the concept of Apex before diving deep into platform management and execution.

What is Apex?

Apex is the most commonly used development platform by Salesforce Developers for building applications on the basis of Software as a Service (SaaS). Apex provides exclusive access to developers into the back-end database of Salesforce CRM as well as multiple client-server interfaces. It includes an Application Program Interface (API) which is prominently used by developers to access necessary user data on the platform of Salesforce CRM.

The applications built using Apex are hosted and run directly on the servers of Salesforce. Owing to this nature of Apex hosting, the developers and operators do not need to install a separate program on the local PCs. Also, any Salesforce user can easily purchase the concerned Apex program with the help of AppExchange, following which they will be able to access the program using the standard interface. It is then in the hands of the user whether they want to publish the application for public use or keep it private.

There are three major tools involved in the platform of Apex:

1. Apex Builder – This is the tool possessing limited features that allows certain specific customisations to be made by the developer to an application. It is essentially an on-demand Apex tool that offers simple drag-and-drop options for the customisations to be carried out.

2. Apex API – This is the Apex tool that allows developers to retrieve raw data from the Salesforce servers. It is generally used by programs that are external to the platform of Salesforce, such as Java applications, that require access to relevant information on a client’s account.

3. Apex Code – This is the programming language used by developers on the platform of Salesforce CRM, having pre-built methods used for accessing data pertaining to Salesforce users. This Apex tool is very important for developers as it offers high degree of flexibility in developing for API. It also reduces the number of calls between a server and a client.

Now that you are familiar with the meaning and use of Apex, you can understand the order of execution in Salesforce that should be followed by developers for dealing with various Salesforce objects and records.

What is Salesforce Order of Execution?

As the name rightly suggests, Salesforce order of execution is the order in which a developer is supposed to carry out multiple events that occur before a record (having an “insert”, “update” or “upsert” statement) is saved in the platform of Salesforce CRM.

A developer needs to make sure that all the necessary events are performed in the set Salesforce execution order to ensure efficient functionality of the platform. Before Salesforce begins with its events in the ideal order of execution, browser would run a validation pertaining to JavaScript in case there are any dependent pick-list fields in the records involved. This validation would restrict each dependent pick-list to the available values.

Here is the ideal Salesforce order of execution followed by the CRM platform:

1. Firstly, Salesforce Developer needs to load an original record from the database or initialise the required record for an upsert statement.

2. The platform now loads new record field values and overwrites the older ones. If a request to do so comes from a standard UI edit page, the platform of Salesforce would carry out checks regarding:

1. Compliance with rules pertaining to layout

2. The values pertaining to the layout and field definition levels

3. Field formats

3. Maximum value of field length

However, if the request comes from any other source, the platform would only validate foreign keys.

1. The developer will now need to execute flows pertaining to before-save updates.

2. After the execution of flows, Salesforce would execute all the before triggers involved.

3. The platform will now run majority of the steps involving system validation again. These steps would largely involve making sure that all the relevant fields have a non-null value. Salesforce would also run the rules pertaining to user-defined valuation (if any). It is important to note that enforcing rules regarding layout is the only system validation that the platform of Salesforce would not run for the second time.

4. The developer will now have to ensure that the platform executes duplicate rules. If this rule ascertains that a record is duplicated, it will use the “block” button, leading to the record not being saved to the system and no further steps will be taken in Salesforce execution order.

5. Salesforce will then save the concerned record to the database. However, it doesn’t commit the record yet.

6. Developers will now have to undertake execution of all necessary after triggers.

7. Here, Salesforce will execute all assignment rules.

8.  The platform will now execute auto response rules.

9.  Now, all the workflows rules are executed.

10. The developer will now have to update the concerned records again if there are any workflow field updates in the system.

11. Salesforce would fire before update and after update triggers again if the records are updated with workflow field updates apart from the standard validations. However, the duplicate rules, custom validation rules, flows, processes and escalation rules are not run for the second time.

12. The platform of Salesforce now executes all the relevant processes and flows using “processes and flow” trigger workflow actions. It is important to note that the involved record would pass through the save procedure when a DML operation is executed by a process or flow.

13. The developer will have to make sure all the escalation rules are executed effectively.

14. Salesforce now executes all the entitlement rules.

15. The platform of Salesforce would undertake necessary calculations and update the record parent’s roll-up summary field, provided the concerned record is a part of cross-object workflow or possesses a roll-up summary field. Also, the parent record would go through the save procedure.

16. In case the concerned parent record is updated and it is the grandparent record that possesses a roll-up summary (or is a part of cross-object workflow), Salesforce would perform relevant calculations and would update the grandparent record’s roll-up summary field. Here, it would b the grandparent record that would go through the save procedure.

17. The developer will now have to make sure that the platform of Salesforce executes sharing evaluation that is criteria-base.

18. Here, Salesforce would commit all the DML operations involved to the database.

19. Finally, the developer will ensure that Salesforce executes post-comment logic like sending an email.

Here are two considerations that should be made regarding the Salesforce execution order:

1. The process of re-firing triggers is not just limited to updates, but is also applicable to operations of all types. If a workflow field update is being fired on record insert, it would rerun any before insert and after triggers for a second time in the form of insert triggers.

2. When the platform of Salesforce undertakes a recursive save, it would skip all the Salesforce execution order steps from 9 (regarding assignment rules) to 18 (regarding the grandparent record and roll-up summary field).

Relationship Between Order of Execution and Apex Transactions

In order to fully understand the order of execution in Salesforce, it is important to have knowledge of the relationship it shared with Apex transactions. This relationship can be explained briefly in the following points:

1. The Apex code contained in all Apex transactions is run during majority of the events involved in the order of execution.

2. Important DML operations that result in several events of Salesforce execution order may be contained in Apex transactions.

3. When the order of execution ends with necessary changes committed to the database, it also marks the end of Apex transactions.

4. Along with every release, there are specific Apex limits attached to every Apex transaction carried out by the developers.

5. It is important for the developers to keep Apex Governor Limits in mind while following the order of execution as exceeding these limits would result in termination of the concerned code.

6. Workflow rules pertaining to an Apex transaction can result in creation of recursive loops.