Leverage Cloud-Computing, Saas, and Better Business Processes to Drive Change in Your Industry

Clint Lee

Subscribe to Clint Lee: eMailAlertsEmail Alerts
Get Clint Lee: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Related Topics: Cloud Computing, Salesforce.com Journal

Cloud Computing: Blog Post

Trigga What?

A simple non-profit Salesforce Trigger example



trigger NewDonationAfterPayment on pymt__PaymentX__c (after insert) {


Apex Class

public with sharing class PaymentManager {

public Class PaymentManagerException extends Exception {}

public static void handleNewPayment(List <pymt__PaymentX__c> newPayments) {
List <pymt__PaymentX__c> completedPayments = new List <pymt__PaymentX__c>();
List <Opportunity> donationList = new List <Opportunity>();
List <OpportunityContactRole> ocrList = new List <OpportunityContactRole>();

// Loop through all new Payment objects in the trigger and add the ones with a 'Completed' status and Type of 'Payment'.
for (pymt__PaymentX__c payment : newPayments) {
if (payment.pymt__Status__c == 'Completed' && payment.pymt__Transaction_Type__c =='Payment') {
} //close for-loop

// If there are new Payments that fit our criteria above then move forward.
if (completedPayments.size() > 0) {

// Loop through the Payments that fit our criteria and create a new Opportunity that mirrors certain information from the Payment.
for (pymt__PaymentX__c p : completedPayments) {
Opportunity donation = new Opportunity();
donation.Name = p.Name + ' - ' + p.pymt__Amount__c;
donation.CloseDate = date.Today();
donation.StageName = 'Posted';
donation.Amount = p.pymt__Amount__c;

// Insert these new Opportunities (called Donations in NonProfitForce)
try {
insert donationList;
} catch (Dmlexception e) {
System.debug('donationList not inserted: ' + e);

// Create two new Lists that will be used to relate the new Opportunities to the Payments in the trigger.
// Select all the opportunities that we just created and order them by Name.  Then select all the Payments that fit our criteria from the trigger
// and order them by Name.  Theoretically, these lists should be the same size and follow the same order since the Names mirror each other..
List <Opportunity> oppList = new List <Opportunity>([Select Id, Name FROM Opportunity WHERE Id IN :donationList ORDER BY Name]);
List <pymt__PaymentX__c> pymtList = new List <pymt__PaymentX__c> ([Select Id, Name FROM pymt__PaymentX__c WHERE Id IN :completedPayments ORDER BY Name]);

//  This loop will assign the opportunity id to the Payment object.
for (Integer i=0; i < pymtList.size(); i++) {
pymtList[i].pymt__Opportunity__c = oppList[i].Id;

// Update the list of Payments from above, this should save the Opportunity Id that we just added.
try {
update pymtList;
} catch (Dmlexception e) {
System.debug('pymtList not inserted: ' + e);

// Create a new list of Payment objects so we can relate the Contacts to the Opportunities that we just created.
//Since the Payment object is related to both the Contact and Opportunity we can query those Id's directly from the Payment object.
List <pymt__PaymentX__c> listForContRole = new List <pymt__PaymentX__c> ([Select Id, pymt__Contact__r.Id, pymt__Opportunity__r.Id FROM pymt__PaymentX__c WHERE ID IN :completedPayments]);
List <OpportunityContactRole> contRoles = new List<OpportunityContactRole>();

// Loop through this new list of Payment objects.  Create a new OpportunityContactRole and using the Opp and Contact Id's from the Payment object.
for (pymt__PaymentX__C p : listForContRole) {
OpportunityContactRole ocr = new OpportunityContactRole();
ocr.ContactId = p.pymt__Contact__r.Id;
ocr.OpportunityId = p.pymt__Opportunity__r.Id;

// Insert this new list of OpportunityContactRoles
try {
insert contRoles;
} catch (DmlException e) {
System.debug('ocrList not inserted: ' + e);

} //close if-statement

} //close handleNewPayment method

} //close PaymentManager Class


I hope this is helpful, and as always look forward to your feedback.

More Stories By Clint Lee

Clint is a Principal and Founder of The Flywheel Group, a premier provider of innovative solutions to the franchise industry. The FranchiseFlywheel™ application was developed out of the industry's need for a cutting-edge and affordable franchise management tool for franchisors. Spending several years in the franchise industry directing daily activities related to franchise and business development processes, marketing, lead-generation, and contract administration, and an understanding of the needs of peers and colleagues, led to the development of the application. The Flywheel Group is focused on improving best practices and driving change in the franchise industry by introducing industry-leading solutions and working with its clients to re-engineer business processes that will ultimately enhance revenue and improve bottom line efficiency. Clint actively writes and shares his thoughts on the Flywheel Blog.