1

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 = 
    SObjectType.Account.FieldSets.HistoryTracking.getFields();

if (trackedFields.isEmpty()) return;

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

if(!trigger.isUpdate)
    return;

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))
            continue;

        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;

        fieldChanges.add(accountHistory);
    }
}

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

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(...));
}  
1

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).

2
  • Thanks for that, i tried the new trigger and test class but still 0% coverage. – SfdcDev14 Jan 5 '16 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 Jan 5 '16 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.