【发布时间】:2018-12-04 11:38:02
【问题描述】:
假设在Account 对象中,它有3 个自定义字段,分别是invoice_delivery_method、invoice_delivery_email 和invoice_delivery_print。对于invoice_delivery_method,类型为picklist,可能的值为Email、Email and Print、Print 和Other。其余两个自定义字段为复选框,默认未选中,即false。
现在,当用户将 invoice_delivery_method 字段更新为 Email(通过 Salesforce 帐户页面或通过 SOQL)时,invoice_delivery_email 设置为 true,invoice_delivery_print 设置为 false。
我的方法是创建一个触发器类,如下所示:
trigger InvoiceDeliveryMethodTrigger on Account (before update) {
InvoiceDeliveryMethodTriggerHandler.handleBeforeUpdate(Trigger.new);
}
在处理程序类中我做了以下操作:
public class InvoiceDeliveryMethodTriggerHandler {
public static void handleBeforeUpdate(Account[] accounts){
RecordType recordType = [select Id from RecordType where sobjecttype = 'Account' and Name =: MSSP_Settings__c.getOrgDefaults().Account_Record_Type__c];
for (Account account : accounts) {
if(account.RecordTypeId == recordType.Id) {
System.debug('Information for Account: ' + account);
System.debug('Information for Invoice Delivery Method: ' + account.Invoice_Delivery_Method__c);
account.Invoice_Delivery_Email__c = false;
account.Invoice_Delivery_Print__c = false;
String delivery_method = account.Invoice_Delivery_Method__c;
System.debug('String is not blank ' + String.isNotBlank(delivery_method));
if (String.isNotBlank(delivery_method)){
if (delivery_method.equals('Email')){
account.Invoice_Delivery_Email__c = true;
account.Invoice_Delivery_Print__c = false;
}
else if (delivery_method.equals('Email and Mail')){
account.Invoice_Delivery_Email__c = true;
account.Invoice_Delivery_Print__c = true;
}
else if (delivery_method.equals('Mail')){
account.Invoice_Delivery_Email__c = false;
account.Invoice_Delivery_Print__c = true;
}
}
}
}
}
}
更新后我在Account 上也有一个触发器类,但我没有更改这 3 个自定义字段的任何值。
如果我通过应用程序进行测试,似乎有两个自定义字段是根据invoice_delivery_method 的值更新的。但是我的单元测试遇到了问题。
这是我写的单元测试类
@isTest
private class InvoiceDeliveryMethodTest {
@isTest(SeeAllData=true)
static void testAccountEmailSelected(){
Account testAccount = new Account();
// populating some of the mandatory field for Account
testAccount.Invoice_Delivery_Method__c = 'Other';
insert testAccount;
Account acct = [Select Id, Invoice_Delivery_Method__c, Invoice_Delivery_Email__c, Invoice_Delivery_Print__c from Account
where Id =: testAccount.Id];
acct.Invoice_Delivery_Method__c = 'Email';
update acct;
acct = [Select Id, Invoice_Delivery_Method__c, Invoice_Delivery_Email__c, Invoice_Delivery_Print__c from Account
where Id =: testAccount.Id];
System.assertEquals('Email', acct.Invoice_Delivery_Method__c);
System.assert(acct.Invoice_Delivery_Email__c);
System.assert(!acct.Invoice_Delivery_Print__c);
delete testAccount;
}
}
当我运行测试用例时,它在 System.assert(acct.Invoice_Delivery_Email__c); 上失败
那个字段仍然是false。为什么会这样?
【问题讨论】:
标签: salesforce