I have written a test for a Saleforce trigger. The trigger changes the Account_Status_Change_Date__c value to current date whenever the ‘Account_Status__c’ changes.
I have physically changed the value of the Account Status through the Web GUI and the Account Status Change Date is indeed set to the current date. However, my test code does not seem to be setting off this trigger. I wonder if people know of any reasons for this.
My code is as follows:
@isTest
public class testTgrCreditChangedStatus {
public static testMethod void testAccountStatusChangeDateChanged() {
// Create an original date
Date previousDate = Date.newinstance(1960, 2, 17);
//Create an Account
Account acc = new Account(name='Test Account 1');
acc.AccountNumber = '123';
acc.Customer_URN_Number__c = '123';
acc.Account_Status_Change_Date__c = previousDate;
acc.Account_Status__c = 'Good';
insert acc;
// Update the Credit Status to a 'Bad Credit' value e.g. Legal
acc.Account_Status__c = 'Overdue';
update acc;
// The trigger should have updated the change date to the current date
System.assertEquals(Date.today(), acc.Account_Status_Change_Date__c);
}
}
trigger tgrCreditStatusChanged on Account (before update) {
for(Account acc : trigger.new) {
String currentStatus = acc.Account_Status__c;
String oldStatus = Trigger.oldMap.get(acc.id).Account_Status__c;
// If the Account Status has changed...
if(currentStatus != oldStatus) {
acc.Account_Status_Change_Date__c = Date.today();
}
}
}
You do not need a trigger to do this. It can be done with a straightforward workflow rule on Account. However, the issue with your test code is that you need to query for the account after you update to get the updated values in the account field.