Apex Triggers

Salesforce菜鸟刚刚接触Salesforce,发现salesforce一大准则就是:能用配置配置出来的东西,千万不要自己写代码。对于Trigger也是一样,对于你的需求第一想法应该是Validation Rule,Process Builder,Work Flow能不能实现,如果这几种方式都不能做,那么开始写代码。

Trigger有一下几种对数据的操作:

before insert

before update

before delete

after insert

after update

after delete

after undelete

有两种类型的Trigger:

Before Trigger are used to update or validate record values before they're save to the database.(即在存入数据库之前进行字段的更新与校验)。

After Trigger are used to access field values that are set by the system(such as a record's Id or LastModifiedDate field), and to affect changes in other records. The records that fire the after trigger are read-only.(即after trigger就是为了获取record的field值,用于更新其他record的值,触发after trigger的记录是只读的。)

使用全局变量:

Trigger.new contains all the records that were inserted in insert or update triggers.(包含即将插入数据库或者更新数据库的所有记录)

Trigger.old provide the old version of sObject before they were updated in update triggers,or a list of deleted sObject in delete trigger.(即包含所有更新或者删除之前的所有记录)

我们还有其他的一些全局变量,可以在我们执行不同的事件时提供帮助:

isInsert,isUpdate, isDelete, isAfter, oldMap. newMap

Trigger Exception

在trigger中,如果你想加一些限制条件比如满足某些限制条件的记录不能把保存在数据库中,我们使用addError()方法来做限制。

Example 1:

Example 2:


你可能感兴趣的:(Apex Triggers)