Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Nonprofit Year End Donation Receipt
public class yearEndCampaignTable implements
Database.Batchable<sObject>,Database.Stateful {
//instance member to retain state across transactions
public static Integer emailLimits;
public Integer recordsProcessed =0;
public Database.QueryLocator start(Database.BatchableContext bc) {
Integer year = Date.Today().year()-1;
return Database.getQueryLocator([SELECT LastName, id,Gifts_Last_Year__c, (SELECT Id, CloseDate, Amount, Campaign.Name FROM Opportunities
WHERE CALENDAR_YEAR(CloseDate) =:year AND IsWon = True ORDER
BY CloseDate) FROM Contact WHERE npo02__OppAmountLastYear__c > 0 WITH SECURITY_ENFORCED]);
}
public void execute(Database.BatchableContext bc, List<Contact> scope) {
//process each batch of records
List<Contact> contactsForUpdate = new List<Contact>();
String loopString;
String longestString = '<table style="width:100%; border: 1px solid black;"><tr><th style="border: 1px solid black; padding: 15px;">Date</th> <th style="border: 1px solid black;">Amount</th><th style="border: 1px solid black;">Campaign Name</th> </tr>';
String dateFormatString = 'MMMM d, yyyy';
String finalString;
for (Contact con : scope) {
for (Opportunity opp : con.opportunities) {
Date d = opp.CloseDate;
Datetime dt = Datetime.newInstance(d.year(), d.month(),d.day());
String dateString = dt.format(dateFormatString);
String cleanAmt = String.valueOf(opp.Amount);
loopString =
'<tr> <td style="border: 1px solid black; padding: 15px;">' + dateString +
'</th> <td style="border: 1px solid black; padding: 15px;"> $' + cleanAmt +
'</th> <td style="border: 1px solid black; padding: 15px;">' + opp.Campaign.Name +
'</th> </tr>';
longestString = longestString + loopString;
}
finalstring = longestString + '</table>';
con.Gifts_Last_Year__c = String.escapeSingleQuotes(finalstring);
contactsForUpdate.add(con);
longestString = '<table style="width:100%; border: 1px solid black;"><tr><th style="border: 1px solid black; padding: 15px;"> Date</th> <th style="border: 1px solid black;">Amount</th> <th style="border: 1px solid black;">Campaign</th></tr>';
recordsProcessed = recordsProcessed + 1;
system.debug('Final String' + finalstring);
}
update contactsForUpdate;
}
public void finish(Database.BatchableContext bc){
AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
String subject = 'Year End Gift Batches complete';
String body = 'records processed ' + recordsProcessed.format() + '. The batch apex processed '+ job.TotalJobItems;
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Now sets the below paramaters of the email object
email.setSubject( subject );
// Here I am accessing current user email id to whom we are sending email
email.setToAddresses( new String[] {UserInfo.getUserEmail()} );
email.setPlainTextBody( body );
// Sends the email
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
system.debug('records processed: ' + recordsProcessed);
emailLimits = Limits.getEmailInvocations();
system.debug ('emaillimits:' + emailLimits);
if (r[0].success) {
System.debug('The email was sent successfully to ' + UserInfo.getUserEmail());
} else {
System.debug('The email failed to send: '+ r[0].errors[0].message);
FeedItem post = new FeedItem();
post.ParentId = UserInfo.getUserId();
post.Body = body;
List<FeedItem> postList= new List<FeedItem>();
postList.add(post);
Insert postList;
}
}
}
public class yearEndGiftBatch implements
Database.Batchable<sObject>,Database.Stateful {
//instance member to retain state across transactions
public static Integer emailLimits;
public Integer recordsProcessed =0;
public Database.QueryLocator start(Database.BatchableContext bc) {
Integer year = Date.Today().year()-1;
return Database.getQueryLocator([SELECT LastName, id,Gifts_Last_Year__c, (SELECT Id, CloseDate, Amount FROM Opportunities
WHERE CALENDAR_YEAR(CloseDate) =:year AND IsWon = True ORDER
BY CloseDate) FROM Contact WHERE npo02__OppAmountLastYear__c > 0 WITH SECURITY_ENFORCED]);
}
public void execute(Database.BatchableContext bc, List<Contact> scope) {
//process each batch of records
List<Contact> contactsForUpdate = new List<Contact>();
String loopString;
String longestString = '<table style="width:100%; border: 1px solid black;"><tr><th style="border: 1px solid black; padding: 15px;"> Date</th> <th style="border: 1px solid black;">Amount</th></tr>';
String dateFormatString = 'MMMM d, yyyy';
String finalString;
for (Contact con : scope) {
for (Opportunity opp : con.opportunities) {
Date d = opp.CloseDate;
Datetime dt = Datetime.newInstance(d.year(), d.month(),d.day());
String dateString = dt.format(dateFormatString);
String cleanAmt = String.valueOf(opp.Amount);
loopString =
'<tr> <td style="border: 1px solid black; padding: 15px;">' + dateString +
'</th> <td style="border: 1px solid black; padding: 15px;"> $' + cleanAmt + '</th> </tr>';
longestString = longestString + loopString;
}
finalstring = longestString + '</table>';
con.Gifts_Last_Year__c = String.escapeSingleQuotes(finalstring);
contactsForUpdate.add(con);
longestString = '<table style="width:100%; border: 1px solid black;"><tr><th style="border: 1px solid black; padding: 15px;"> Date</th> <th style="border: 1px solid black;">Amount</th></tr>';
recordsProcessed = recordsProcessed + 1;
system.debug('Final String' + finalstring);
}
update contactsForUpdate;
}
public void finish(Database.BatchableContext bc){
AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
String subject = 'Year End Gift Batches complete';
String body = 'records processed ' + recordsProcessed.format() + '. The batch apex processed '+ job.TotalJobItems;
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Now sets the below paramaters of the email object
email.setSubject( subject );
// Here I am accessing current user email id to whom we are sending email
email.setToAddresses( new String[] {UserInfo.getUserEmail()} );
email.setPlainTextBody( body );
// Sends the email
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
system.debug('records processed: ' + recordsProcessed);
emailLimits = Limits.getEmailInvocations();
system.debug ('emaillimits:' + emailLimits);
if (r[0].success) {
System.debug('The email was sent successfully to ' + UserInfo.getUserEmail());
} else {
System.debug('The email failed to send: '+ r[0].errors[0].message);
FeedItem post = new FeedItem();
post.ParentId = UserInfo.getUserId();
post.Body = body;
List<FeedItem> postList= new List<FeedItem>();
postList.add(post);
Insert postList;
}
}
}
//run this class to implement for Fiscal Years instead of calendar year
//Change your NPSP roll-up for Total Gifts Last Year to use Fiscal Year instead of calendar
//launch in execute anonymous by pasting in the next line without the two //
//Id batchJobId = Database.executeBatch(new fiscalYearEndGiftBatch());
public class fiscalYearEndGiftBatch implements
Database.Batchable<sObject>,Database.Stateful {
//instance member to retain state across transactions
public static Integer emailLimits;
public Integer recordsProcessed =0;
public Database.QueryLocator start(Database.BatchableContext bc) {
String currentFiscalYear = [SELECT FiscalYearSettings.Name FROM Period WHERE Type = 'Year' AND StartDate <= TODAY AND EndDate >= TODAY].FiscalYearSettings.Name;
Integer i = Integer.valueOf(currentFiscalYear);
Integer year = i - 1;
return Database.getQueryLocator([SELECT LastName, id,Gifts_Last_Year__c, (SELECT Id, CloseDate, Amount FROM Opportunities
WHERE FISCAL_YEAR(CloseDate) =:year AND IsWon = True ORDER
BY CloseDate) FROM Contact WHERE npo02__OppAmountLastYear__c > 0 WITH SECURITY_ENFORCED]);
}
public void execute(Database.BatchableContext bc, List<Contact> scope) {
//process each batch of records
List<Contact> contactsForUpdate = new List<Contact>();
String loopString;
String longestString = '<table style="width:100%; border: 1px solid black;"><tr><th style="border: 1px solid black; padding: 15px;"> Date</th> <th style="border: 1px solid black;">Amount</th></tr>';
String dateFormatString = 'MMMM d, yyyy';
String finalString;
for (Contact con : scope) {
for (Opportunity opp : con.opportunities) {
Date d = opp.CloseDate;
Datetime dt = Datetime.newInstance(d.year(), d.month(),d.day());
String dateString = dt.format(dateFormatString);
String cleanAmt = String.valueOf(opp.Amount);
loopString =
'<tr> <td style="border: 1px solid black; padding: 15px;">' + dateString +
'</th> <td style="border: 1px solid black; padding: 15px;"> $' + cleanAmt + '</th> </tr>';
longestString = longestString + loopString;
}
finalstring = longestString + '</table>';
con.Gifts_Last_Year__c = String.escapeSingleQuotes(finalstring);
contactsForUpdate.add(con);
longestString = '<table style="width:100%; border: 1px solid black;"><tr><th style="border: 1px solid black; padding: 15px;"> Date</th> <th style="border: 1px solid black;">Amount</th></tr>';
recordsProcessed = recordsProcessed + 1;
system.debug('Final String' + finalstring);
}
update contactsForUpdate;
}
public void finish(Database.BatchableContext bc){
AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
String subject = 'Year End Gift Batches complete';
String body = 'records processed ' + recordsProcessed.format() + '. The batch apex processed '+ job.TotalJobItems;
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Now sets the below paramaters of the email object
email.setSubject( subject );
// Here I am accessing current user email id to whom we are sending email
email.setToAddresses( new String[] {UserInfo.getUserEmail()} );
email.setPlainTextBody( body );
// Sends the email
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
system.debug('records processed: ' + recordsProcessed);
emailLimits = Limits.getEmailInvocations();
system.debug ('emaillimits:' + emailLimits);
if (r[0].success) {
System.debug('The email was sent successfully to ' + UserInfo.getUserEmail());
} else {
System.debug('The email failed to send: '+ r[0].errors[0].message);
FeedItem post = new FeedItem();
post.ParentId = UserInfo.getUserId();
post.Body = body;
List<FeedItem> postList= new List<FeedItem>();
postList.add(post);
Insert postList;
}
}
}
//Code by Jessie Rymph
//December 30, 2021
//launch in execute anonymous by pasting in the next line without the two //
//Id batchJobId = Database.executeBatch(new yearEndCampaignTable());
//https://wp.me/p9qPLI-1q8
//Instructions on how to add additional fields are found in rows 15, 16, 26, 41, 42.
//Compare this class with YearEndGiftBatch to see how I added the Campaign Name field to this version.
public class yearEndCampaignTable implements
Database.Batchable<sObject>,Database.Stateful {
//instance member to retain state across transactions
public static Integer emailLimits;
public Integer recordsProcessed =0;
public Database.QueryLocator start(Database.BatchableContext bc) {
Integer year = Date.Today().year()-1;
//******To add in additional fields from the Contact add them in the list with LastName, id, Gifts_Last_Year__c
//******To add in additional fields from the Opportunity add them in the list with id, CloseDate, Amount, Campaign.Name
return Database.getQueryLocator([SELECT LastName, id,Gifts_Last_Year__c, (SELECT Id, CloseDate, Amount, Campaign.Name FROM Opportunities
WHERE CALENDAR_YEAR(CloseDate) =:year AND IsWon = True ORDER
BY CloseDate) FROM Contact WHERE npo02__OppAmountLastYear__c > 0 WITH SECURITY_ENFORCED]);
}
public void execute(Database.BatchableContext bc, List<Contact> scope) {
//process each batch of records
List<Contact> contactsForUpdate = new List<Contact>();
String loopString;
//******To add another column to the table, add another section to longestString. <th style="border: 1px solid black;">Name of Column </th>
String longestString = '<table style="width:100%; border: 1px solid black;"><tr><th style="border: 1px solid black; padding: 15px;">Date</th> <th style="border: 1px solid black;">Amount</th><th style="border: 1px solid black;">Campaign Name</th> </tr>';
String dateFormatString = 'MMMM d, yyyy';
String finalString;
for (Contact con : scope) {
for (Opportunity opp : con.opportunities) {
//system.debug(con.LastName);
Date d = opp.CloseDate;
Datetime dt = Datetime.newInstance(d.year(), d.month(),d.day());
String dateString = dt.format(dateFormatString);
String cleanAmt = String.valueOf(opp.Amount);
loopString =
'<tr> <td style="border: 1px solid black; padding: 15px;">' + dateString +
'</th> <td style="border: 1px solid black; padding: 15px;"> $' + cleanAmt +
//******To add another field to the table, from the opportunity, follow the pattern of row 41, and replace "Campaign.Name" with the field name.
//******To add the ID for example, '</th> <td style="border: 1px solid black; padding: 15px;">' + opp.Id +
'</th> <td style="border: 1px solid black; padding: 15px;">' + opp.Campaign.Name +
'</th> </tr>';
longestString = longestString + loopString;
}
finalstring = longestString + '</table>';
con.Gifts_Last_Year__c = String.escapeSingleQuotes(finalstring);
contactsForUpdate.add(con);
longestString = '<table style="width:100%; border: 1px solid black;"><tr><th style="border: 1px solid black; padding: 15px;"> Date</th> <th style="border: 1px solid black;">Amount</th> <th style="border: 1px solid black;">Campaign</th></tr>';
recordsProcessed = recordsProcessed + 1;
system.debug('Final String' + finalstring);
}
update contactsForUpdate;
}
public void finish(Database.BatchableContext bc){
AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
String subject = 'Year End Gift Batches complete';
String body = 'records processed ' + recordsProcessed.format() + '. The batch apex processed '+ job.TotalJobItems;
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Now sets the below paramaters of the email object
email.setSubject( subject );
// Here I am accessing current user email id to whom we are sending email
email.setToAddresses( new String[] {UserInfo.getUserEmail()} );
email.setPlainTextBody( body );
// Sends the email
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
system.debug('records processed: ' + recordsProcessed);
emailLimits = Limits.getEmailInvocations();
system.debug ('emaillimits:' + emailLimits);
if (r[0].success) {
System.debug('The email was sent successfully to ' + UserInfo.getUserEmail());
} else {
System.debug('The email failed to send: '+ r[0].errors[0].message);
FeedItem post = new FeedItem();
post.ParentId = UserInfo.getUserId();
post.Body = body;
List<FeedItem> postList= new List<FeedItem>();
postList.add(post);
Insert postList;
}
}
}
//Code by Jessie Rymph
//December 30, 2021
//Tests the Year End Gift Campaign process using the YearEndtestDataFactory class
//https://wp.me/p9qPLI-1q8
@isTest
public class yearEndCampaignTest {
@isTest static void positiveTest() {
// Test data setup
// Create contacts with opps through test utility
Integer testNumC= 20;
Integer testNumO = 20;
contact[] cons = YearEndTestDataFactory.createContactsWithOpps(testNumC,testNumO);
Test.startTest();
yearEndCampaignTable yEGB = new yearEndCampaignTable();
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('Campaign')) {
contacts.add(person);
}
}
System.assertEquals(testNumC,contacts.size(),testNumC +' ');
}
@isTest static void negativeTest() {
// Test data setup
// Create contacts with opps through test utility
Integer testNumC=10;
Integer testNumO = 5;
contact[] cons = YearEndTestDataFactory.wrongYearOpps(testNumC,testNumO);
Test.startTest();
yearEndCampaignTable yEGB = new yearEndCampaignTable();
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 testThatEmailWasSent(){
Integer testNumC= 20;
Integer testNumO = 20;
contact[] cons = YearEndTestDataFactory.createContactsWithOpps(testNumC,testNumO);
Test.startTest();
//System.assertEquals(0, Limits.getEmailInvocations(), 'No emails should be sent');
yearEndCampaignTable yEGB = new yearEndCampaignTable();
Id batchId = Database.executeBatch(yEGB);
Test.stopTest();
System.assertEquals(1,yearendCampaignTable.emaillimits);
}
}
//Code by Jessie Rymph
//December 30, 2021
//
//launch in execute anonymous by pasting in the next line without the two //
//Id batchJobId = Database.executeBatch(new yearEndGiftBatch());
//https://wp.me/p9qPLI-1q8
public class yearEndGiftBatch implements Database.Batchable<sObject>,Database.Stateful {
//instance member to retain state across transactions
public static Integer emailLimits;
public Integer recordsProcessed =0;
public Database.QueryLocator start(Database.BatchableContext bc) {
Integer year = Date.Today().year()-1;
return Database.getQueryLocator([SELECT LastName, id,Gifts_Last_Year__c, (SELECT Id, CloseDate, Amount FROM Opportunities
WHERE CALENDAR_YEAR(CloseDate) =:year AND IsWon = True ORDER
BY CloseDate) FROM Contact WHERE npo02__OppAmountLastYear__c > 0 WITH SECURITY_ENFORCED]);
}
public void execute(Database.BatchableContext bc, List<Contact> scope) {
//process each batch of records
List<Contact> contactsForUpdate = new List<Contact>();
String loopString;
String longestString = '<table style="width:100%; border: 1px solid black;"><tr><th style="border: 1px solid black; padding: 15px;"> Date</th> <th style="border: 1px solid black;">Amount</th></tr>';
String dateFormatString = 'MMMM d, yyyy';
String finalString;
for (Contact con : scope) {
for (Opportunity opp : con.opportunities) {
Date d = opp.CloseDate;
Datetime dt = Datetime.newInstance(d.year(), d.month(),d.day());
String dateString = dt.format(dateFormatString);
String cleanAmt = String.valueOf(opp.Amount);
loopString =
'<tr> <td style="border: 1px solid black; padding: 15px;">' + dateString +
'</th> <td style="border: 1px solid black; padding: 15px;"> $' + cleanAmt + '</th> </tr>'
longestString = longestString + loopString;
}
finalstring = longestString + '</table>';
con.Gifts_Last_Year__c = String.escapeSingleQuotes(finalstring);
contactsForUpdate.add(con);
//reset the value of longestString to the start. otherwise it's going to add everyone's gifts to the list and keep growing.
longestString = '<table style="width:100%; border: 1px solid black;"><tr><th style="border: 1px solid black; padding: 15px;"> Date</th> <th style="border: 1px solid black;">Amount</th></tr>';
recordsProcessed = recordsProcessed + 1;
system.debug('Final String' + finalstring);
}
update contactsForUpdate;
}
public void finish(Database.BatchableContext bc){
AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
String subject = 'Year End Gift Batches complete';
String body = 'records processed ' + recordsProcessed.format() + '. The batch apex processed '+ job.TotalJobItems;
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Now sets the below paramaters of the email object
email.setSubject( subject );
// Here I am accessing current user email id to whom we are sending email
email.setToAddresses( new String[] {UserInfo.getUserEmail()} );
email.setPlainTextBody( body );
// Sends the email
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
system.debug('records processed: ' + recordsProcessed);
emailLimits = Limits.getEmailInvocations();
system.debug ('emaillimits:' + emailLimits);
if (r[0].success) {
System.debug('The email was sent successfully to ' + UserInfo.getUserEmail());
} else {
System.debug('The email failed to send: '+ r[0].errors[0].message);
FeedItem post = new FeedItem();
post.ParentId = UserInfo.getUserId();
post.Body = body;
List<FeedItem> postList= new List<FeedItem>();
postList.add(post);
Insert postList;
}
}
}
//Code by Jessie Rymph
//December 30, 2021
//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
// Create contacts with opps through test utility
Integer testNumC= 20;
Integer testNumO = 20;
contact[] cons = YearEndTestDataFactory.createContactsWithOpps(testNumC,testNumO);
Test.startTest();
yearEndGiftBatch yEGB = new yearEndGiftBatch();
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
// Create contacts with opps through test utility
Integer testNumC=10;
Integer testNumO = 12;
contact[] cons = YearEndTestDataFactory.wrongYearOpps(testNumC,testNumO);
Test.startTest();
yearEndGiftBatch yEGB = new yearEndGiftBatch();
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 testThatEmailWasSent(){
Integer testNumC= 20;
Integer testNumO = 20;
contact[] cons = YearEndTestDataFactory.createContactsWithOpps(testNumC,testNumO);
Test.startTest();
yearEndGiftBatch yEGB = new yearEndGiftBatch();
Id batchId = Database.executeBatch(yEGB);
Test.stopTest();
System.assertEquals(1,yearendGiftbatch.emaillimits);
}
}
//Code by Jessie Rymph
//December 30, 2021
//https://wp.me/p9qPLI-1q8
@isTest
public class yearEndtestDataFactory {
public static Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Donation').getRecordTypeId();
//positive test
public static List<Contact> createContactsWithOpps(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',
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> wrongYearOpps(Integer numCts, Integer numOppsPerCt){
//Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Donation').getRecordTypeId();
//create Test Data with gifts that are not last 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);
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;
}
}
@forcethesales
Copy link
Author

forcethesales commented Jan 27, 2022

This code goes with the AppExchange package for Nonprofit Donation Receipt. https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3u00000Pu8bOEAR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment