I am trying to write a test class for this trigger that keeps track of updates on the account object. I created a test class and in it created an Account with couple fields inserted, then updated those fields. That didn't give me any coverage. Would i need to create a test class and update the fields from the Account history object? Any tips would be helpful. Thanks

Trigger AccountHistoryTracker on Account (after update) {

final List<Schema.FieldSetMember> trackedFields = 

if (trackedFields.isEmpty()) return;

final List<AccountHistoryTracking__c> fieldChanges = 
    new List<AccountHistoryTracking__c>();


for (Account newAccount : trigger.new) {

    final Account oldAccount = trigger.oldmap.get(newAccount.Id);

    for (Schema.FieldSetMember fsm : trackedFields) {

        String fieldName  = fsm.getFieldPath();
        String fieldLabel = fsm.getLabel();
     System.debug( fieldname + fieldLabel );

        if (newAccount.get(fieldName) == oldAccount.get(fieldName))

        String oldValue = String.valueOf(oldAccount.get(fieldName));
        String newValue = String.valueOf(newAccount.get(fieldName));

        if (oldValue != null && oldValue.length()>255) 
            oldValue = oldValue.substring(0,255);

        if (newValue != null && newValue.length()>255) 
            newValue = newValue.substring(0,255); 

        final AccountHistoryTracking__c accountHistory = 
            new AccountHistoryTracking__c();

        accountHistory.name         = fieldLabel;
        accountHistory.apiName__c   = fieldName;
        accountHistory.Account__c      = newAccount.id;
        accountHistory.OldValue__c  = oldValue;
        accountHistory.NewValue__c  = newValue;


if (!fieldChanges.isEmpty()) {
    insert fieldChanges;

2 Answers 2


You are using history tracking table in your trigger and in test class history tracking table record is not created may be because in test class records are not created in actual so history is not tracked. you can cover that part by creating a fake history tracking record in your main class. It will works for you.

if (Test.isRunningTest()) {
        accHistory.add(new AccountHistory(...));
        accHistory.add(new AccountHistory(...));

Your trigger has some problems that will prevent 100% coverage. Let's fix that.

trigger AccountHistoryTracker on Account (after update) {
    AccountHistoryTracking__c[] fieldChanges = new AccountHistoryTracking__c[0];
    for(Schema.FieldSetMember fsm: SObjectType.Account.FieldSets.HistoryTracking.getFields()) {
        for(Integer index = 0, size = Trigger.new.size(); index < size; index++) {
            Account oldRecord = Trigger.old[index], newRecord = Trigger.new[index];
            String fieldName = fsm.getFieldPath(), fieldLabel = fsm.getLabel(),
                oldValue = (String)oldRecord.get(fieldName), newValue = (String)newRecord.get(fieldName);
            if(oldValue != newValue) {
                if(oldValue != null) {
                    oldValue = oldValue.abbreviate(255);
                if(newValue != null) {
                    newValue = newValue.abbreviate(255);
                fieldChanges.add(new AccountHistoryTracking__c(
                    Name = fieldLabel,
                    ApiName__c = fieldName,
                    Account__c = newRecord.Id,
                    OldValue__c = oldValue,
                    newValue__c = newValue
    insert fieldChanges;

Now that we're all optimized for coverage, we still need to make sure we perform a qualifying update:

@isTest static void testChanges() {
     Account record = new Account(Name='Test');
     insert record;
     record.Name = 'Test 2';
     update record;
     System.assertEquals(1, [SELECT COUNT() FROM AccountHistoryTracking__c], 'Expected a new history tracking item.');

(Note: You'll want to update this code to include any required fields, and of course depending on which field(s) you're tracking.)

(Note 2: You will not get code coverage if you run the tests synchronously. This is a rather well-known bug by now. You'll need to go to the Test menu and check "Always Run Asynchronously" to see your actual coverage).

  • Thanks for that, i tried the new trigger and test class but still 0% coverage.
    – SfdcDev14
    Commented Jan 5, 2016 at 22:02
  • @SfdcDev14 Again, make sure you're running the tests correctly. Assuming your unit test is not failing, you must have more than 0% coverage.
    – sfdcfox
    Commented Jan 5, 2016 at 22:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .