触发器触发了两次,但第一次 运行 是空的?
Trigger is firing two times, but first run is empty?
以下触发器触发了两次:
trigger AccountTrigger on Account ( before insert, after insert, before update, after update, before delete, after delete) {
AccountTriggerHandler handle = new AccountTriggerHandler(trigger.new, trigger.oldMap);
System.debug('AccountTrigger created a handler instance: ' + handle);
// Currently the Trigger is firing twice with no obvious reason.
if (Trigger.isBefore) {
if (Trigger.isInsert) {
handle.beforeInsert();
}
if (Trigger.isUpdate) {
// Call handler here!
}
if (Trigger.isDelete) {
// Call handler here!
}
}
if (Trigger.isAfter) {
if (Trigger.isInsert) {
// Call handler here!
}
if (Trigger.isUpdate) {
// Call handler here!
}
if (Trigger.isDelete) {
// Call handler here!
}
}
}
调试结果显示了两个处理程序实例。奇怪的是:第一个好像是空的?怎么可能?
编辑 1:
测试代码:
@isTest
public class AccountTestTest {
@isTest
public static void testAccountInsert() {
// Insert an Account
Account a = new Account(name='TestCustomer');
insert a;
Account queryAccount = [SELECT Account.id, Account.name FROM Account WHERE Id = :a.Id];
System.debug('TEST RESULT: ' + queryAccount);
System.debug('AccountTestTest completed.');
// Actually test something...
}
}
我知道它缺少断言,但为了简单起见,我只试了这个。
这是因为“插入前”。在那个阶段,ids 还没有生成。如果您在插入最佳之前没有任何适合的逻辑(复杂的验证?字段预填充?)删除该事件?
以下触发器触发了两次:
trigger AccountTrigger on Account ( before insert, after insert, before update, after update, before delete, after delete) {
AccountTriggerHandler handle = new AccountTriggerHandler(trigger.new, trigger.oldMap);
System.debug('AccountTrigger created a handler instance: ' + handle);
// Currently the Trigger is firing twice with no obvious reason.
if (Trigger.isBefore) {
if (Trigger.isInsert) {
handle.beforeInsert();
}
if (Trigger.isUpdate) {
// Call handler here!
}
if (Trigger.isDelete) {
// Call handler here!
}
}
if (Trigger.isAfter) {
if (Trigger.isInsert) {
// Call handler here!
}
if (Trigger.isUpdate) {
// Call handler here!
}
if (Trigger.isDelete) {
// Call handler here!
}
}
}
调试结果显示了两个处理程序实例。奇怪的是:第一个好像是空的?怎么可能?
编辑 1: 测试代码:
@isTest
public class AccountTestTest {
@isTest
public static void testAccountInsert() {
// Insert an Account
Account a = new Account(name='TestCustomer');
insert a;
Account queryAccount = [SELECT Account.id, Account.name FROM Account WHERE Id = :a.Id];
System.debug('TEST RESULT: ' + queryAccount);
System.debug('AccountTestTest completed.');
// Actually test something...
}
}
我知道它缺少断言,但为了简单起见,我只试了这个。
这是因为“插入前”。在那个阶段,ids 还没有生成。如果您在插入最佳之前没有任何适合的逻辑(复杂的验证?字段预填充?)删除该事件?