Salesforce事务执行顺序(Triggers and Order of Execution)

因为在Salesforce面试或者自己写代码的过程中,Salesforce的事务执行顺序经常被提到或者用到,所以记录一下,上面中文版的少了最新的Process Builder的顺序,请大家注意!
Salesforce Trigger的执行顺序

原文出自
当您保存与插入,更新,或更新插入语句的记录,Salesforce的可以依次执行以下事件。
注:Salesforce在服务器上执行这些事件之前,浏览器运行JavaScript验证是否有记录包含任何依赖列表字段。验证每个依赖列表字段限制了它的可用值。无其他验证发生在客户端。

在服务器上,Salesforce按以下顺序执行:
1,从数据库加载原始记录或初始化一个用于更新插入(upsert)语句的记录。
2,从请求加载新记录的字段值并覆盖旧的值。
如果请求来自一个标准的UI(user interface)编辑页面,Salesforce会运行系统验证去检查记录是否符合以下规则:
•符合布局的具体规则
•在布局水平和字段定义级别是否是必须入力的值
•有效字段格式
•最大字段长度
注意:当这个请求来自其他资源的时候(比如来自Apex application 或者 SOAP API 调用的时候.),salesforce并没有在此步骤中执行系统验证
3,执行所有的before 触发器。
4,再次运行大部分的系统验证步骤,如验证所有必填字段没有一个非空值,并运行所有用户自定义的验证规则。
5,保存记录到数据库中,但不提交事务(commit)。
6。执行所有的after 触发器。
7,执行分配规则(assignment rules)。
8,执行自动响应规则(auto-response rules)。
9,执行工作流程规则。
10,如果有工作流将一些字段更新了,再更新该记录。
11,如果记录在工作流更新字段值得时候被更新了,再一次(仅仅再一次)触发before update和after updata触发器,同时还有标准的验证规则也会被执行(自定义的验证规则不会被执行)。
12,如果有工作流流程触发器(workflow flow triggers),执行流(flow)。
13,执行升级规则。
14,如果记录包含一个卷起的汇总字段,或者是一个跨对象工作流程的一部分,执行计算并更新父记录卷起汇总字段。父记录经过保存的程序。

15,如果父记录被更新,和祖父母记录包含一个卷起的汇总字段,或者是一个跨对象工作流程的一部分,执行计算并更新父记录卷起汇总字段。祖父母记录经过保存的程序。
16,执行基于共享的评价标准。
17,要求所有DML操作到数据库。
18,执行post-commit的逻辑,如发送电子邮件。
Triggers and Order of Execution

Source Link
When you save a record with an insert, update, or upsert statement, Salesforce performs the following events in order.
Note
Before Salesforce executes these events on the server, the browser runs JavaScript validation if the record contains any dependent picklist fields. The validation limits each dependent picklist field to its available values. No other validation occurs on the client side.

On the server, Salesforce:
1.Loads the original record from the database or initializes the record for an upsert statement.
2.Loads the new record field values from the request and overwrites the old values.
If the request came from a standard UI edit page, Salesforce runs system validation to check the record for:
Compliance with layout-specific rules
Required values at the layout level and field-definition level
Valid field formats
Maximum field length
When the request comes from other sources, such as an Apex application or a SOAP API call, Salesforce validates only the foreign keys. Prior to executing a trigger, Salesforce verifies that any custom foreign keys do not refer to the object itself.
Salesforce runs user-defined validation rules if multiline items were created, such as quote line items and opportunity line items.

3.Executes all before triggers.
4.Runs most system validation steps again, such as verifying that all required fields have a non-null value, and runs any user-defined validation rules. The only system validation that Salesforce doesn’t run a second time (when the request comes from a standard UI edit page) is the enforcement of layout-specific rules.
5.Executes duplicate rules. If the duplicate rule identifies the record as a duplicate and uses the block action, the record is not saved and no further steps, such as after triggers and workflow rules, are taken.
6.Saves the record to the database, but doesn’t commit yet.
7.Executes all after triggers.
8.Executes assignment rules.
9.Executes auto-response rules.
10.Executes workflow rules.
11.If there are workflow field updates, updates the record again.
12.If the record was updated with workflow field updates, fires before update triggers and after update triggers one more time (and only one more time), in addition to standard validations. Custom validation rules, duplicate rules, and escalation rules are not run again.
13.Executes processes.
If there are workflow flow triggers, executes the flows.

The Process Builder has superseded flow trigger workflow actions, formerly available in a pilot program. Organizations that are using flow trigger workflow actions can continue to create and edit them, but flow trigger workflow actions aren’t available for new organizations.

14.Executes escalation rules.
15.Executes entitlement rules.
16.If the record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Parent record goes through save procedure.
17.If the parent record is updated, and a grandparent record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the grandparent record. Grandparent record goes through save procedure.
18.Executes Criteria Based Sharing evaluation.
19.Commits all DML operations to the database.
20.Executes post-commit logic, such as sending email.
Note
During a recursive save, Salesforce skips steps 8 (assignment rules) through 17 (roll-up summary field in the grandparent record).

你可能感兴趣的:(Salesforce学习)