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

I do some consulting work for non-profit organizations that use Salesforce.com.  There are so many non-profits that use Salesforce but many often lack the internal resources necessary to truly realize the benefits of adopting it.  It is highly rewarding work and I would encourage you to check out the Salesforce Foundation as one way of getting involved with non-profits who use Salesforce.

The Salesforce Foundation has a very successful program that gives non-profits the ability to get 10 donated (free) Enterprise Edition user licenses. This is a great program that offers a huge benefit to non-profits.  If you're a non-profit you can learn more about how to apply for your licenses here.  If you have experience in configuring, customizing, administering, consulting, and/or developing on the Force.com platform you can get involved by emailing foundation@salesforce.com and letting them know you're interested in volunteering your time and expertise.  This is a great first step towards getting involved.

With that being said I'd like to share a specific real world example for an Apex Trigger that might be helpful.

Use Case
Here's the scenario.

We use Linvio's PaymentConnect app to process online donations that come through our website.  When someone pays online via a credit card a Payment object record is created.  Payment is a custom object included in the PaymentConnect package.  The Payment record captures information about the transaction including credit card info, payment gateway, donation amount, etc.  It also captures the name, email, and contact info of the donor who initiates the transaction.  With this contact info a Contact record is either created (if no match is found) or the Payment is related to an existing Contact (if a match is found).

The challenge we faced is that we track donations through Opportunities.  While the Payment record does capture the amount of the donation and relates it to a Contact, we really wanted an Opportunity to be created that reflects each payment because that is how we report on donation income, do forecasting, etc.  We also wanted to relate the Contact to this Opportunity through a Contact Role.

One option could have been to create a workflow rule,that fires everytime a payment is successfully processed, and sets a Task for someone to manually create a new opportunity and relate it to the Contact.  But we wanted to automate this process as much as possible so we decided to write a trigger.

Below is the code for the trigger and the Apex Class that handles it.  Keep in mind that we are using the NonProfitForce configuration.  You can read more about NonProfitForce here.

The main point of the trigger is to create a Closed Opportunity (Donation) each time a successful Payment is processed.  Then, the corresponding Contact is related to the Opportunity through an OpportunityContactRole.  This trigger is bulkified to handle multiple Payments without easily tripping governor limits.  Read up on trigger governor limits here.

I'd also highly recommend considering Linvio's PaymentConnect app for your ecommerce and payment processing needs.  Ron Wild and his team are highly competent and helpful.

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.