SFDC之trigger那些事儿

初学Salesforce 的Apex开发,trigger可以说的开发者的基本功了。

trigger的定义模式如下:

trigger TriggerName on ObjectName (trigger_events) {
   code_block
}

triggerName相当于trigger的命名,ObjectName指trigger绑定的类的名字,trigger_events指能引起代码触发的条件,主要有insert,update,delete,merge,upsert,undelete六种,本文主要介绍insert,update和delete三种。

trigger的类型包括两种

  • Before trigger:通常用于在他们被保存在数据库以前更新或者校验数据;
  • After Trigger: 通常用于保存后访问系统的字段(Id等).

创建一个测试用例TestTrigger__c,里面含字段status__c,触发器代码如下:

trigger testTrigger on TestTrigger__c (before insert,before update,before delete,after insert,after update,after delete) {
	if(trigger.isBefore){
		//插入前
		if(trigger.isInsert){
			System.debug('*******插入前trigger.new********' + trigger.new);		
			System.debug('*******插入前trigger.newMap********' + trigger.newMap);
			System.debug('*******插入前trigger.old********' + trigger.old);
			System.debug('*******插入前trigger.oldMap********' + trigger.oldMap);	
		}
		//更新前
		if(trigger.isUpdate){
			System.debug('*******更新前trigger.new********' + trigger.new);
			System.debug('*******更新前trigger.newMap********' + trigger.newMap);
			System.debug('*******更新前trigger.old********' + trigger.old); 
			System.debug('*******更新前trigger.oldMap********' + trigger.oldMap);
		}
		//删除前
		if(trigger.isDelete){ 
			System.debug('*******删除前trigger.new********' + trigger.new); 
			System.debug('*******删除前trigger.newMap********' + trigger.newMap);
			System.debug('*******删除前trigger.old********' + trigger.old);
			System.debug('*******删除前trigger.oldMap********' + trigger.oldMap);
		}
	}

	if(trigger.isAfter){
		//插入后
		if(trigger.isInsert){ 
			System.debug('*******插入后trigger.new********' + trigger.new);		
			System.debug('*******插入后trigger.newMap********' + trigger.newMap);
			System.debug('*******插入后trigger.old********' + trigger.old);
			System.debug('*******插入后trigger.oldMap********' + trigger.oldMap);		
		}
		//更新后
		if(trigger.isUpdate){ 
			System.debug('*******更新后trigger.new********' + trigger.new);
			System.debug('*******更新后trigger.newMap********' + trigger.newMap);
			System.debug('*******更新后trigger.old********' + trigger.old); 
			System.debug('*******更新后trigger.oldMap********' + trigger.oldMap);
		}
		//删除后
		if(trigger.isDelete){
			System.debug('*******删除后trigger.new********' + trigger.new); 
			System.debug('*******删除后trigger.newMap********' + trigger.newMap);
			System.debug('*******删除后trigger.old********' + trigger.old);
			System.debug('*******删除后trigger.oldMap********' + trigger.oldMap);
		}
	}
}

新建一个 TestTrigger__c对象,控制台输出可以看到:

*******插入前trigger.new********(TestTrigger__c:{Id=null, OwnerId=0057F000000ySVPQA2, IsDeleted=false, Name=null, CreatedDate=null, CreatedById=null, LastModifiedDate=null, LastModifiedById=null, SystemModstamp=null, LastActivityDate=null, LastViewedDate=null, LastReferencedDate=null, address__c=触发器测试数据, status__c=状态一})
*******插入前trigger.newMap********null
*******插入前trigger.old********null
*******插入前trigger.oldMap********null
*******插入后trigger.new********(TestTrigger__c:{Id=a007F000003AzKwQAK, OwnerId=0057F000000ySVPQA2, IsDeleted=false, Name=R-0006, CreatedDate=2017-08-21 03:14:49, CreatedById=0057F000000ySVPQA2, LastModifiedDate=2017-08-21 03:14:49, LastModifiedById=0057F000000ySVPQA2, SystemModstamp=2017-08-21 03:14:49, LastActivityDate=null, LastViewedDate=null, LastReferencedDate=null, address__c=触发器测试数据, status__c=状态一})
*******插入后trigger.newMap********{a007F000003AzKwQAK=TestTrigger__c:{Id=a007F000003AzKwQAK, OwnerId=0057F000000ySVPQA2, IsDeleted=false, Name=R-0006, CreatedDate=2017-08-21 03:14:49, CreatedById=0057F000000ySVPQA2, LastModifiedDate=2017-08-21 03:14:49, LastModifiedById=0057F000000ySVPQA2, SystemModstamp=2017-08-21 03:14:49, LastActivityDate=null, LastViewedDate=null, LastReferencedDate=null, address__c=触发器测试数据, status__c=状态一}}
*******插入后trigger.old********null
*******插入后trigger.oldMap********null

修改对象,控制台输出看到:

*******更新前trigger.new********(TestTrigger__c:{Id=a007F000003AzKwQAK, OwnerId=0057F000000ySVPQA2, IsDeleted=false, Name=R-0006, CreatedDate=2017-08-21 03:14:49, CreatedById=0057F000000ySVPQA2, LastModifiedDate=2017-08-21 03:14:49, LastModifiedById=0057F000000ySVPQA2, SystemModstamp=2017-08-21 03:14:49, LastActivityDate=null, LastViewedDate=null, LastReferencedDate=null, address__c=触发器测试数据, status__c=状态二})
*******更新前trigger.newMap********{a007F000003AzKwQAK=TestTrigger__c:{Id=a007F000003AzKwQAK, OwnerId=0057F000000ySVPQA2, IsDeleted=false, Name=R-0006, CreatedDate=2017-08-21 03:14:49, CreatedById=0057F000000ySVPQA2, LastModifiedDate=2017-08-21 03:14:49, LastModifiedById=0057F000000ySVPQA2, SystemModstamp=2017-08-21 03:14:49, LastActivityDate=null, LastViewedDate=null, LastReferencedDate=null, address__c=触发器测试数据, status__c=状态二}}
*******更新前trigger.old********(TestTrigger__c:{Id=a007F000003AzKwQAK, OwnerId=0057F000000ySVPQA2, IsDeleted=false, Name=R-0006, CreatedDate=2017-08-21 03:14:49, CreatedById=0057F000000ySVPQA2, LastModifiedDate=2017-08-21 03:14:49, LastModifiedById=0057F000000ySVPQA2, SystemModstamp=2017-08-21 03:14:49, LastActivityDate=null, LastViewedDate=null, LastReferencedDate=null, address__c=触发器测试数据, status__c=状态一})
*******更新前trigger.oldMap********{a007F000003AzKwQAK=TestTrigger__c:{Id=a007F000003AzKwQAK, OwnerId=0057F000000ySVPQA2, IsDeleted=false, Name=R-0006, CreatedDate=2017-08-21 03:14:49, CreatedById=0057F000000ySVPQA2, LastModifiedDate=2017-08-21 03:14:49, LastModifiedById=0057F000000ySVPQA2, SystemModstamp=2017-08-21 03:14:49, LastActivityDate=null, LastViewedDate=null, LastReferencedDate=null, address__c=触发器测试数据, status__c=状态一}}
*******更新后trigger.new********(TestTrigger__c:{Id=a007F000003AzKwQAK, OwnerId=0057F000000ySVPQA2, IsDeleted=false, Name=R-0006, CreatedDate=2017-08-21 03:14:49, CreatedById=0057F000000ySVPQA2, LastModifiedDate=2017-08-21 03:16:58, LastModifiedById=0057F000000ySVPQA2, SystemModstamp=2017-08-21 03:16:58, LastActivityDate=null, LastViewedDate=null, LastReferencedDate=null, address__c=触发器测试数据, status__c=状态二})
*******更新后trigger.newMap********{a007F000003AzKwQAK=TestTrigger__c:{Id=a007F000003AzKwQAK, OwnerId=0057F000000ySVPQA2, IsDeleted=false, Name=R-0006, CreatedDate=2017-08-21 03:14:49, CreatedById=0057F000000ySVPQA2, LastModifiedDate=2017-08-21 03:16:58, LastModifiedById=0057F000000ySVPQA2, SystemModstamp=2017-08-21 03:16:58, LastActivityDate=null, LastViewedDate=null, LastReferencedDate=null, address__c=触发器测试数据, status__c=状态二}}
*******更新后trigger.old********(TestTrigger__c:{Id=a007F000003AzKwQAK, OwnerId=0057F000000ySVPQA2, IsDeleted=false, Name=R-0006, CreatedDate=2017-08-21 03:14:49, CreatedById=0057F000000ySVPQA2, LastModifiedDate=2017-08-21 03:14:49, LastModifiedById=0057F000000ySVPQA2, SystemModstamp=2017-08-21 03:14:49, LastActivityDate=null, LastViewedDate=null, LastReferencedDate=null, address__c=触发器测试数据, status__c=状态一})
*******更新后trigger.oldMap********{a007F000003AzKwQAK=TestTrigger__c:{Id=a007F000003AzKwQAK, OwnerId=0057F000000ySVPQA2, IsDeleted=false, Name=R-0006, CreatedDate=2017-08-21 03:14:49, CreatedById=0057F000000ySVPQA2, LastModifiedDate=2017-08-21 03:14:49, LastModifiedById=0057F000000ySVPQA2, SystemModstamp=2017-08-21 03:14:49, LastActivityDate=null, LastViewedDate=null, LastReferencedDate=null, address__c=触发器测试数据, status__c=状态一}}

删除对象,控制台输出可以看到:

*******删除前trigger.new********null
*******删除前trigger.newMap********null
*******删除前trigger.old********(TestTrigger__c:{Id=a007F000003AzKwQAK, OwnerId=0057F000000ySVPQA2, IsDeleted=false, Name=R-0006, CreatedDate=2017-08-21 03:14:49, CreatedById=0057F000000ySVPQA2, LastModifiedDate=2017-08-21 03:16:58, LastModifiedById=0057F000000ySVPQA2, SystemModstamp=2017-08-21 03:16:58, LastActivityDate=null, LastViewedDate=null, LastReferencedDate=null, address__c=触发器测试数据, status__c=状态二})
*******删除前trigger.oldMap********{a007F000003AzKwQAK=TestTrigger__c:{Id=a007F000003AzKwQAK, OwnerId=0057F000000ySVPQA2, IsDeleted=false, Name=R-0006, CreatedDate=2017-08-21 03:14:49, CreatedById=0057F000000ySVPQA2, LastModifiedDate=2017-08-21 03:16:58, LastModifiedById=0057F000000ySVPQA2, SystemModstamp=2017-08-21 03:16:58, LastActivityDate=null, LastViewedDate=null, LastReferencedDate=null, address__c=触发器测试数据, status__c=状态二}}
*******删除后trigger.new********null
*******删除后trigger.newMap********null
*******删除后trigger.old********(TestTrigger__c:{Id=a007F000003AzKwQAK, OwnerId=0057F000000ySVPQA2, IsDeleted=false, Name=R-0006, CreatedDate=2017-08-21 03:14:49, CreatedById=0057F000000ySVPQA2, LastModifiedDate=2017-08-21 03:16:58, LastModifiedById=0057F000000ySVPQA2, SystemModstamp=2017-08-21 03:16:58, LastActivityDate=null, LastViewedDate=null, LastReferencedDate=null, address__c=触发器测试数据, status__c=状态二})
*******删除后trigger.oldMap********{a007F000003AzKwQAK=TestTrigger__c:{Id=a007F000003AzKwQAK, OwnerId=0057F000000ySVPQA2, IsDeleted=false, Name=R-0006, CreatedDate=2017-08-21 03:14:49, CreatedById=0057F000000ySVPQA2, LastModifiedDate=2017-08-21 03:16:58, LastModifiedById=0057F000000ySVPQA2, SystemModstamp=2017-08-21 03:16:58, LastActivityDate=null, LastViewedDate=null, LastReferencedDate=null, address__c=触发器测试数据, status__c=状态二}}

上述结果可以看到,trigger.new等变量不是在任何时候都会有值,当它实际上等于null的时候,在trigger_events下是没有意义的。

对于trigger中的new/old变量需要注意的是,它的实质的一个List集合的存储形式,里面包含的不仅只是一个对象的值,更多的是针对批处理的情况、Apex开发后期使用DataLoader导入用户数据就是很典型的一种批处理场景。所以在开发的时候,我们更多的要针对多条数据进行处理。

List<TestTrigger__c>  list_test = trigger.new
if(trigger.isBefore){
	//插入前
	if(trigger.isInsert){
		for(TestTrigger__c test: trigger.new){
			test.status__c = '状态二';
		}

	}
}

每次新建TestTrigger__c对象的时候,将状态都修改为“状态二”

而对于trigger中的newmap/oldmap变量,实质是一个map存储了对象的ID和对象,通过ID可以取出对应的SObject。

下面举个例子

trigger testTrigger on TestTrigger__c (after update) {
	TestTriggerHandle handler =  new  TestTriggerHandle();
	if(trigger.IsUpdate && trigger.isAfter)}     
	        handler.OnAfterUpdate(trigger.new,trigger.oldMap);
	}
}

/**
 *  Author:小青 
 *  Time:2017-8-21
 *  Function:TestTrigger__c的状态发生变化
 *
 */
public class TestTriggerHandle {
    public void OnAfterUpdate(List<TestTrigger__c> list_test,Map<Id,TestTrigger__c> map_old){       
        Set<Id>  set_id = new Set<Id>();//记录被修改的TestTrigger__c的ID
        for(TestTrigger__c test : list_test){
                set_id.add(test.Id);            
        }        
        List<TestTrigger__c> list_update = [Select Id,status__c From TestTrigger__c  where Id in :set_id];
      
        for(TestTrigger__c test : advList){           
            TestTrigger__c oldTest = map_old.get(ad.Id); 
            if(test.status__c != oldTest.status__c){
            	System.debug('测试数据的状态发生了修改,原状态为:' + oldTest.status__c + '新的状态为' + test.status__c);
            }
        }
}

以上是常用的触发器开发的方法,逻辑处理放在一个Handler类中,在触发器中调用handler类的方法进行处理,从而实现代码的分离,便于控制代码的执行顺序以及避免业务逻辑过于复杂。

最后,对于触发器的开发需要理解,例如trigger.new在before insert中,不需要进行DML操作就可以直接修改记录的数据,然后就能进行保存,而在After Update中,并不能直接使用trigger.new所代表是数据,因为这条记录已经被插入到数据库中,这时候直接进行DML操作会报错,所以需要我们用一个set记录下对应的ID然后再进行一次查询,对这个查询出来的集合重新DML操作就没有问题了。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值