zoukankan      html  css  js  c++  java
  • APEX初步 [3] —— Apex触发器

    触发器概念

    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
        }
    }
    VariableUsage
    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 insertupdate, 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 updateafter 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.

    触发器和工作流的选择

    工作流

    触发器

    • 用apex做apex能做的任何事情
    • 经常被用在当roll up summary字段不能用的情况下来代替它的作用
    • 经常被用在要在其他对象上创建记录的场合
  • 相关阅读:
    男子利用POS机帮人非法套现1576万余元被起诉
    套现花样很多 弄不好成了诈骗
    刀尖上的无本生意 信用卡套现投资盛行
    新三板是什么—新三板科普知识讲堂
    Android注入事件的三种方法比较
    当今最流行的Node.js应用开发框架简介
    安卓WindowManager注入事件如何跳出进程间安全限制
    伟大创意检验10要
    初创互联网公司简明创业指南
    根据Unix哲学来编写你的HTML5 Websocket服务器来实现全双工通信
  • 原文地址:https://www.cnblogs.com/abovecloud/p/6367007.html
Copyright © 2011-2022 走看看