Skip to content

Instantly share code, notes, and snippets.

@forcethesales
Last active January 19, 2023 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forcethesales/6a8cd89e93cd4e99e36537bdada7d5e6 to your computer and use it in GitHub Desktop.
Save forcethesales/6a8cd89e93cd4e99e36537bdada7d5e6 to your computer and use it in GitHub Desktop.
Nonprofit Year End Tax Deductible Letter
//Instructions
//In your developer console in your sandbox, find the class called yearEndGiftBatchTest
//and replace all the code with this code.
//You don't need to modify this code at all.
//Code by Jessie Rymph
//January 12, 2023
//Tests the Year End Gift Batch process using the YearEndtestDataFactory class
//https://wp.me/p9qPLI-1q8
@isTest
public class yearEndGiftBatchTest {
@isTest static void positiveTest() {
// Test data setup
// test for Gifts Last Year
// Create contacts with opps through test utility
Integer testNumC= 2;
Integer testNumO = 2;
contact[] cons = YearEndTestDataFactory.giftsLastYear(testNumC,testNumO);
Test.startTest();
yearEndTaxBatch yEGB = new yearEndTaxBatch();
Id batchId = Database.executeBatch(yEGB);
Test.stopTest();
List<Contact> contacts = new List<Contact>();
for(Contact person : [SELECT Id, Gifts_Last_Year__c FROM Contact]) {
if(person.Gifts_Last_Year__c.contains('Date')) {
contacts.add(person);
}
}
System.assertEquals(testNumC,contacts.size(),testNumC +' ');
}
@isTest static void negativeTest() {
// Test data setup
// Test that this years gifts do not go into Gifts Last Year
// Create contacts with opps through test utility
Integer testNumC=10;
Integer testNumO = 12;
contact[] cons = YearEndTestDataFactory.GiftsThisYear(testNumC,testNumO);
Test.startTest();
yearEndTaxBatch yEGB = new yearEndTaxBatch();
Id batchId = Database.executeBatch(yEGB);
Test.stopTest();
List<Contact> contacts = new List<Contact>();
for(Contact person : [SELECT Id, Gifts_Last_Year__c FROM Contact]) {
IF(person.Gifts_Last_Year__c != null) {
contacts.add(person);
}
}
System.assertEquals(0,contacts.size(),'Expected none, got' + contacts.size());
}
@isTest static void notTaxDeductibleLastYear() {
// Test gifts last year that are not tax deductible are not added
// Create contacts with opps through test utility
Integer testNumC=10;
Integer testNumO = 12;
contact[] cons = YearEndTestDataFactory.notTaxDeductibleGiftsLastYear(testNumC,testNumO);
Test.startTest();
yearEndTaxBatch yEGB = new yearEndTaxBatch();
Id batchId = Database.executeBatch(yEGB);
Test.stopTest();
List<Contact> contacts = new List<Contact>();
for(Contact person : [SELECT Id, Gifts_Last_Year__c FROM Contact]) {
IF(person.Gifts_Last_Year__c != null) {
contacts.add(person);
}
}
System.assertEquals(0,contacts.size(),'Expected none, got' + contacts.size());
}
}
//INSTRUCTIONS. In your dev console in your sandbox, open YearEndTestDataFactory. Replace it with this code below.
// This class is making sample data to test our apex. In line 26 we're giving an example of a contact who WOULD get a year end tax letter.
//Go to Line 27 and find Gifts_Last_Year_Tax_Deductible__c=60.
//Replace that field with whatever your criteria/example would be. If your field has a different name, use that instead. If it's a checkbox = true, put that in instead or add it in with a comma.
//if you have criteria for which opportunities should be included, go to Line 51 and change that to the right field name, or add it in with a comma.
//Code by Jessie Rymph
//January 17, 2023
//https://wp.me/p9qPLI-1q8
@isTest
public class yearEndtestDataFactory {
public static Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Donation').getRecordTypeId();
//gifts last year
public static List<Contact> GiftsLastYear(Integer numCts, Integer numOppsPerCt){
//create Test Data
Campaign camp = new Campaign (Name = 'Annual Fund');
insert camp;
List<Contact> cons = new List<Contact>();
for(Integer i=0;i<numCts;i++) {
Contact a = new Contact(LastName='Test'+i,Email='basket@gmail.com',Gifts_Last_Year_Tax_Deductible__c=60);
Cons.add(a);
}
insert Cons;
system.debug('insert' + cons);
List<Opportunity> opps = new List<Opportunity>();
for (Integer j=0;j<numCts;j++) {
Contact connie = Cons[j];
//get the year of last year. start by getting today's date.
Date myDate = system.today();
//get the year from the date
Integer thisYear = myDate.year();
//subtract one to make it last year
Integer lastYear = thisYear - 1;
//set a date variable for January 1 of last year. This will be the first gift date.
Date janDate = Date.newInstance(lastyear, 1, 1);
// For each contact just inserted, add opportunities
for (Integer k=0;k<numOppsPerCt;k++) {
opps.add(new Opportunity(Name=connie.Name + ' Opportunity ' + k,
recordTypeid= devRecordTypeId,
AccountId=connie.AccountId,
StageName='Closed Won',
tax_deductible__c=TRUE,
CampaignId = camp.Id,
//for each opp created use the JanDate variable, one month later
CloseDate=janDate.addmonths(k),
Amount=k+990,
npsp__Primary_Contact__c=connie.Id));
}
}
// Insert all opportunities for all accounts.
insert opps;
system.debug('all Test Opps created');
return cons;
}
public static List<Contact> giftsThisYear(Integer numCts, Integer numOppsPerCt){
//create Test Data with gifts that are this year
Campaign camp = new Campaign (Name = 'Annual Fund');
insert camp;
List<Contact> cons = new List<Contact>();
for(Integer i=0;i<numCts;i++) {
Contact a = new Contact(LastName='Test'+i,Email='basket@gmail.com',npo02__OppAmountLastYear__c=40, npo02__OppAmountThisYear__c=60);
Cons.add(a);
}
insert Cons;
system.debug('insert' + cons);
List<Opportunity> opps = new List<Opportunity>();
for (Integer j=0;j<numCts;j++) {
Contact connie = Cons[j];
//get today's date
Date myDate = system.today();
//get the year from the date
Integer thisYear = myDate.year();
//set a date variable for January 1 of this year. This will be the first gift date.
Date janDate = Date.newInstance(thisyear, 1, 1);
// For each contact just inserted, add opportunities
for (Integer k=0;k<numOppsPerCt;k++) {
opps.add(new Opportunity(Name=connie.Name + ' Opportunity ' + k,
recordTypeid=devRecordTypeId,
AccountId=connie.AccountId,
StageName='Closed Won',
CampaignId = camp.id,
//for each opp created use the JanDate variable and add one year
CloseDate=janDate.addyears(k),
Amount=k+990,
npsp__Primary_Contact__c=connie.Id));
}
}
// Insert all opportunities for all accounts.
insert opps;
system.debug('all Test Opps created');
return cons;
}
//gifts two years ago
public static List<Contact> gifts2YearsAgo(Integer numCts, Integer numOppsPerCt){
//create Test Data
Campaign camp = new Campaign (Name = 'Annual Fund');
insert camp;
List<Contact> cons = new List<Contact>();
for(Integer i=0;i<numCts;i++) {
Contact a = new Contact(LastName='Test'+i,Email='basket@gmail.com',npo02__OppAmountLastYear__c=40, npo02__OppAmountThisYear__c=60);
Cons.add(a);
}
insert Cons;
system.debug('insert' + cons);
List<Opportunity> opps = new List<Opportunity>();
for (Integer j=0;j<numCts;j++) {
Contact connie = Cons[j];
//get the year of last year. start by getting today's date.
Date myDate = system.today();
//get the year from the date
Integer thisYear = myDate.year();
//subtract one to make it last year
Integer twoYears = thisYear - 2;
//set a date variable for January 1 of last year. This will be the first gift date.
Date janDate = Date.newInstance(twoYears, 1, 1);
// For each contact just inserted, add opportunities
for (Integer k=0;k<numOppsPerCt;k++) {
opps.add(new Opportunity(Name=connie.Name + ' Opportunity ' + k,
recordTypeid= devRecordTypeId,
AccountId=connie.AccountId,
StageName='Closed Won',
CampaignId = camp.Id,
//for each opp created use the JanDate variable, one month later
CloseDate=janDate.addmonths(k),
Amount=k+990,
npsp__Primary_Contact__c=connie.Id));
}
}
// Insert all opportunities for all accounts.
insert opps;
system.debug('all Test Opps created');
return cons;
}
public static List<Contact> notTaxDeductibleGiftsLastYear(Integer numCts, Integer numOppsPerCt){
//create Test Data
Campaign camp = new Campaign (Name = 'Annual Fund');
insert camp;
List<Contact> cons = new List<Contact>();
for(Integer i=0;i<numCts;i++) {
Contact a = new Contact(LastName='Test'+i,Email='basket@gmail.com',npo02__OppAmountLastYear__c=40);
Cons.add(a);
}
insert Cons;
system.debug('insert' + cons);
List<Opportunity> opps = new List<Opportunity>();
for (Integer j=0;j<numCts;j++) {
Contact connie = Cons[j];
//get the year of last year. start by getting today's date.
Date myDate = system.today();
//get the year from the date
Integer thisYear = myDate.year();
//subtract one to make it last year
Integer lastYear = thisYear - 1;
//set a date variable for January 1 of last year. This will be the first gift date.
Date janDate = Date.newInstance(lastyear, 1, 1);
// For each contact just inserted, add opportunities
for (Integer k=0;k<numOppsPerCt;k++) {
opps.add(new Opportunity(Name=connie.Name + ' Opportunity ' + k,
recordTypeid= devRecordTypeId,
AccountId=connie.AccountId,
StageName='Closed Won',
tax_deductible__c=TRUE,
CampaignId = camp.Id,
//for each opp created use the JanDate variable, one month later
CloseDate=janDate.addmonths(k),
Amount=k+990,
npsp__Primary_Contact__c=connie.Id));
}
}
// Insert all opportunities for all accounts.
insert opps;
system.debug('all Test Opps created');
return cons;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment