Suppose in Account
object, it have 3 custom fields, there are invoice_delivery_method
, invoice_delivery_email
and invoice_delivery_print
. For invoice_delivery_method
, the type is picklist, and the possible values are Email
, Email and Print
, Print
and Other
. The rest of two custom fields are checkbox, and default to unchecked, which is false
.
Now when user update the invoice_delivery_method
field to Email
, (either by Salesforce account page or by SOQL), the invoice_delivery_email
is set to true
, and invoice_delivery_print
is set to false
.
The way I did is create a trigger class like following:
trigger InvoiceDeliveryMethodTrigger on Account (before update) {
InvoiceDeliveryMethodTriggerHandler.handleBeforeUpdate(Trigger.new);
}
Inside the handler class I did following:
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;
}
}
}
}
}
}
I also have one trigger class on Account
after update, but I didn't change any value for that 3 custom field.
If I test via application, it seems that two custom field is got updated based on the value of invoice_delivery_method
. But I got problems for my unit testing.
Here is the unit test class I wrote
@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;
}
}
When I run the test case, it fail on System.assert(acct.Invoice_Delivery_Email__c);
That fields still false
. Why this happen?