Skip to content

Instantly share code, notes, and snippets.

@jamesasu
Created October 2, 2020 06:31
Show Gist options
  • Save jamesasu/b50eee214dc44145171a8b80ed257689 to your computer and use it in GitHub Desktop.
Save jamesasu/b50eee214dc44145171a8b80ed257689 to your computer and use it in GitHub Desktop.
global class AFFL_AffiliationsStartDate_TDTM extends npsp.TDTM_Runnable {
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 = new npsp.TDTM_Runnable.DmlWrapper();
List<npe5__Affiliation__c> newAffils = (List<npe5__Affiliation__c>) newlist;
List<npe5__Affiliation__c> oldAffils = (List<npe5__Affiliation__c>) oldlist;
if (triggerAction == npsp.TDTM_Runnable.Action.BeforeInsert) {
for(npe5__Affiliation__c affil : newAffils) {
if (affil.npe5__StartDate__c == null) {
affil.npe5__StartDate__c = Date.today();
}
}
}
return dmlWrapper;
}
}
@isTest
private class AFFL_AffiliationsStartDate_TEST {
@isTest
static void testTrigger() {
// Retrieve default NPSP Trigger Handlers
List<npsp__Trigger_Handler__c> triggerHandlers = npsp.TDTM_Config_API.getCachedRecords();
// Add our Trigger Handler to cached Trigger Handlers
npsp__Trigger_Handler__c th = new npsp__Trigger_Handler__c();
th.Name = 'AffiliationsStartDate';
th.npsp__Class__c = 'AFFL_AffiliationsStartDate_TDTM';
th.npsp__Object__c = 'npe5__Affiliation__c';
th.npsp__Trigger_Action__c = 'BeforeInsert';
th.npsp__Active__c = true;
th.npsp__Load_Order__c = 1;
th.npsp__Asynchronous__c = false;
triggerHandlers.add(th);
Account acc1 = new Account(
Name = 'My Test Account 1'
);
Database.SaveResult acc1r = Database.insert(acc1, false);
System.assertEquals(true, acc1r.isSuccess());
if (!acc1r.isSuccess()) {
System.debug(acc1r.getErrors());
}
Contact con1 = new Contact(
LastName = 'MyTestContact'
);
Database.SaveResult con1r = Database.insert(con1, false);
System.assertEquals(true, con1r.isSuccess());
if (!con1r.isSuccess()) {
System.debug(con1r.getErrors());
}
Test.startTest();
npe5__Affiliation__c aff1 = new npe5__Affiliation__c(
npe5__Organization__c = acc1r.getId(),
npe5__Contact__c = con1r.getId(),
npe5__Status__c = 'Current'
);
Database.SaveResult aff1r = Database.insert(aff1, false);
System.assertEquals(true, aff1r.isSuccess());
if (!aff1r.isSuccess()) {
System.debug(aff1r.getErrors());
}
Test.stopTest();
npe5__Affiliation__c aff1c = [SELECT Id, npe5__StartDate__c FROM npe5__Affiliation__c WHERE Id = :aff1r.getId() LIMIT 1];
System.assertNotEquals(null, aff1c);
System.assertEquals(Date.today(), aff1c.npe5__StartDate__c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment