触发器概念
apex触发器就像数据库中的触发器一样,用来在增删改之前或之后对记录执行自定义的操作,也可以添加触发的条件。它可以做任何apex能做的功能,比如执行SOQL语句或者调用其他apex方法。它可以被定义在Sobejct上,比如account,contact或者自定义对象上。
尽量在无法通过普通的点击操作的设置完成的情况下才使用触发器,比如如果要验证一个字段的值或更新一条记录上的某个字段,就应该使用validation rule 和 workflow。
触发器格式
trigger TriggerName on ObjectName (trigger_events) {
code_block
}
有下面的trigger_events
before insert
before update
before delete
after insert
after update
after delete
after undelete
触发器分为两种:before和after
before发生在记录被更新到数据库之前,可以用来更新和验证数据。
after在记录保存之后,可以获得比如record id ,lastmodifiedDate之类的数据
Context Variables 上下文变量
Trigger.New 用来获取新插入或者新更新的记录
Trigger.Old 用来获取被更新或被删除签的记录。
这些记录有可能是批量操作的,可以对他们进行枚举来获得里面的每一条记录。
trigger HelloWorldTrigger on Account (before insert) { for(Account a : Trigger.New) { a.Description = 'New description'; } }
还有一些变量的值可以判断触发器是在哪个事件下被触发的
trigger ContextExampleTrigger on Account (before insert, after insert, after delete) { if (Trigger.isInsert) { if (Trigger.isBefore) { // Process before insert } else if (Trigger.isAfter) { // Process after insert } } else if (Trigger.isDelete) { // Process after delete } }
Variable | Usage |
---|---|
isExecuting | Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous()API call. |
isInsert | Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API. |
isUpdate | Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API. |
isDelete | Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API. |
isBefore | Returns true if this trigger was fired before any record was saved. |
isAfter | Returns true if this trigger was fired after all records were saved. |
isUndelete | Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.) |
new | Returns a list of the new versions of the sObject records. This sObject list is only available in insert, update, andundelete triggers, and the records can only be modified inbefore triggers. |
newMap | A map of IDs to the new versions of the sObject records. This map is only available in before update, after insert,after update, and after undelete triggers. |
old | Returns a list of the old versions of the sObject records. This sObject list is only available in update and delete triggers. |
oldMap | A map of IDs to the old versions of the sObject records. This map is only available in update and delete triggers. |
size | The total number of records in a trigger invocation, both old and new. |
触发器和工作流的选择
工作流
- 不能创建记录
- 只能做task, email, field update和outbound message
- 跨对象只能在master detail关系中,从detail 到 master, 而且只能在有限的场景下 (所有自定义到自定义, 某些自定义到标准, 少数标准到标准) https://help.salesforce.com/HTViewHelpDoc?id=workflow_cross_object_field_updates.htm&language=en_US
触发器
- 用apex做apex能做的任何事情
- 经常被用在当roll up summary字段不能用的情况下来代替它的作用
- 经常被用在要在其他对象上创建记录的场合