• Jennifer Chance
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Working in NPSP, so I have a concurrent post in the Hub, but I'm thinking this may be more test class related so I'm posting here as well.

I have a simple service class that modifies a couple dates
public class VIR_ContactService {

    public static void UpdateTACertDates(List<Contact> newCons){
        for (Contact c : newCons) {
            if (c.TA_Join_Date__c != null) {
                if (c.TA_Date_Recertified__c == null || c.TA_Certification_Expiration__c == null) {
                    c.TA_Date_Recertified__c = c.TA_Join_Date__c;
                    c.TA_Certification_Expiration__c = c.TA_Join_Date__c.addYears(3);
                }
                if (c.TA_Certification_Expiration__c <= System.Date.today()) {
                    c.TA_Certification_Expiration__c = c.TA_Certification_Expiration__c.addYears(3);
                    c.TA_Date_Recertified__c = c.TA_Certification_Expiration__c.addYears(-3);
                }
            }
        }
    }

}

This is called by a TDTM trigger handler
global without sharing class VIR_CallTAUpdateCertDates_TDTM extends npsp.TDTM_Runnable {
  
  // the main entry point for TDTM to invoke our trigger handlers.
  global override npsp.TDTM_Runnable.DmlWrapper run(List<SObject> newlist, List<SObject> oldlist, 
             npsp.TDTM_Runnable.Action triggerAction, Schema.DescribeSObjectResult objResult) {
  
  	npsp.TDTM_Runnable.DmlWrapper dmlWrapper = null;

      if (triggerAction == npsp.TDTM_Runnable.Action.BeforeInsert ||
         triggerAction == npsp.TDTM_Runnable.Action.BeforeUpdate ) {
        List<Contact> newConList = (List<Contact>)newlist;
        VIR_ContactService.UpdateTACertDates(newConList);
      }
  	return dmlWrapper;
  	}
}
This works fine from the UI. I can update TA_Certification_Expiration__c on a Contact and the values will be modified as expected. The problem occurred when I created a test class.
 
@isTest
class VIR_ContactService_test {

    @isTest
    static void UpdateTACertDates_test () {
        Account a = new Account(Name='Test Vendor');
        insert a;
        
        List<Contact> conList = new List<Contact>();
        for(Integer i=0;i<3;i++){
            Contact c = new Contact(LastName='Tester'+i,Account=a);
            conList.add(c);
        }
        Date now = System.Date.today();
        conList[1].TA_Join_Date__c = now.addYears(-1);
        System.debug('Test Contacts for insert: ' + conList);
        Test.startTest();
        insert conList;

		conList = [SELECT Id,FirstName,LastName,TA_Join_Date__c,
                   TA_Date_Recertified__c,TA_Certification_Expiration__c
                   FROM Contact
                   ORDER BY LastName];
      
        conList[0].FirstName = 'Fatima';
        conList[1].FirstName = 'Frank';
        conList[1].TA_Certification_Expiration__c = now.addYears(-2);
        conList[2].FirstName = 'Fiora';
        conList[2].TA_Certification_Expiration__c = now.addYears(-1);
        System.debug('Test Contacts for update: ' + conList);
        update conList;
        
		conList = [SELECT Id,FirstName,LastName,TA_Join_Date__c,
                   TA_Date_Recertified__c,TA_Certification_Expiration__c
                   FROM Contact];

        system.assertEquals(null, conList[0].TA_Certification_Expiration__c);
        system.assertEquals(now.addYears(1), conList[1].TA_Certification_Expiration__c);
        system.assertEquals(now.addYears(2), conList[2].TA_Certification_Expiration__c);
        Test.stopTest)();
    }
}
The insert triggers and runs the service class, but the update does not. What am I missing? Is there some aspect of a test class update that is significantly different here that updates work from the UI but not in the test run?