Skip to content
Darin Larimore
All posts

Pansy Patch: how we used Salesforce data to create a dynamic donations landing page

5 min read
On this page
  1. The data is already in Salesforce
  2. Where it runs
  3. Never make a visitor wait on Salesforce
  4. The honor lists and the 2,000 record wall
  5. What I would change
  6. The short version

Pansy Patch is the annual giving campaign for a national sorority's foundation. Every gift is counted in pansies as well as dollars, donors give in honor of a sister or a chapter, and the chapters compete. In January 2020 I started on a page for their Craft site called Watch Our Garden Grow: the running dollar total, the number of pansies given, a slider of the top chapters, and the names of donors and the people they honored. All of that data already existed. It lives in Salesforce, where the foundation's team records every gift against a sub-campaign code, and nobody wanted to type it into a CMS a second time.

The data is already in Salesforce

The foundation runs the Nonprofit Success Pack, so a donation is an Opportunity with an Amount, a custom Number_of_Pansies__c, a PP_Chapter__r lookup and a Primary_Source_Sub_Campaign__r whose name encodes the campaign and the fiscal year. The totals the page needed are one aggregate query each:

SELECT sum(Amount)
FROM Opportunity
WHERE Primary_Source_Sub_Campaign__r.Name IN (
    'PPHOPE1500-Pansies in Hope FY24',
    'PPLOVE15000-Pansies in Love FY24',
    'PPFAITH150-Pansies in Faith FY24',
    'PPCCHCLUB24-College Chapter Giving Club Pansies FY24',
    'PPACHCLUB24-Alumnae Giving Club Pansies FY24',
    'PPGEN24-General Pansies FY24',
    'PPACH24-Alumnae Chapter Pansies FY24',
    'PPCCH24-College Chapter Pansies FY24'
)

The chapter standings are the same query with sum(Number_of_Pansies__c), grouped by PP_Chapter__r.Name, ordered descending, LIMIT 10. Each chapter in that result is matched to its chapter entry in Craft by Salesforce ID so the slide gets the chapter's photo from the CMS.

Where it runs

The queries live in a Craft module. The first version was in a plugin a colleague had written for staff and officer listings on the previous major of Craft; when the site moved up a major in the summer of 2020 the plugin became a module, and it has survived the major upgrade since. The module's controller exposes each query as a JSON action: get-chapter-total-all, get-total-pansies-given, get-number-of-pansies-for-each-chapter, get-individual-donors. The page is built from four Neo block types, and each block's Twig is one line:

<section id="totals-banner" data-pansy-patch-totals-banner></section>

A small React app mounts on that element, calls the two totals actions with axios, and counts the numbers up when they arrive. Editors control the intro copy from the control panel. The numbers are never typed by anyone.

Never make a visitor wait on Salesforce

Two decisions keep the page fast. First, the landing page URL is in Blitz's include list, so the HTML is a static file and Twig never calls Salesforce. The data comes in after the page is up. Second, every action caches its result in Craft's cache with a TTL matched to how often the number can change:

Craft::$app->cache->set("getTotalPansiesGiven", $totalsData, 60);
Craft::$app->cache->set("getPansyPatchChapterTotalAll", $totalsData, 3600);
Craft::$app->cache->set("getNumberOfPansiesForEachChapter", $opportunities, 3600);

The pansy count is the number people watch during the campaign, so it refreshes every minute. The dollar total and the chapter standings are an hour. On a cache miss the module gets a token with the OAuth password grant, runs the query over cURL with a 10 second timeout, and emails the team on a cURL error instead of failing silently.

The honor lists and the 2,000 record wall

In 2022 the page grew lists for the giving clubs, Pansies in Love, Hope and Faith. Those come from the In_Honor_Memoriam__c object joined to the opportunity and to both contacts, honoree and donor, with LIMIT and OFFSET behind a load-more button and an hour in the cache per page.

For the 2024 campaign the foundation wanted search: type a sister's name and find every gift made in her honor. Every field I needed was already aliased and cleaned in that query, so instead of a second Salesforce search I pulled the set and searched it in PHP with Fuse (loilo/fuse, a port of the JavaScript library), threshold 0.3, over the honoree and donor name, maiden name, chapter and school fields.

The first version ran the query per search and cached per query. It only ever searched the first batch, because the query API hands back at most 2,000 records per call. In June I replaced it with one recursive fetch that pages through the whole set and caches the merged result:

private function getSearchRecords($offset = 0, $records = [])
{
    if (Craft::$app->cache->exists("getSearchRecords")) {
        return Craft::$app->cache->get("getSearchRecords");
    }

    // ... the SOQL above, with LIMIT 2000 OFFSET $offset

    if (isset($response['records'])) {
        $records = array_merge($response['records'], $records);
    }

    if (isset($response['totalSize']) && $response['totalSize'] == 2000) {
        return $this->getSearchRecords($offset + 2000, $records);
    }

    Craft::$app->cache->set("getSearchRecords", $records, 3600);

    return $records;
}

Now a search is one cache read and one in-memory fuzzy match, and Salesforce sees one set of requests an hour no matter how many people are typing.

What I would change

The campaign codes carry the fiscal year and they are hard-coded in the queries. Each campaign the foundation creates new sub-campaigns, and I open the module and swap FY20 for FY24 by hand across half a dozen queries. They belong in a control panel field or an environment variable. The queries being string-built is also why the search input is stripped to letters and numbers before it gets anywhere near SOQL, which is the right guard but not a substitute for parameters.

The short version

Do not copy data out of the system of record. Query it, cache it with a TTL per number, keep the page static, and let the browser fetch the numbers after the page is up. The foundation's team keeps working in Salesforce, the campaign page updates itself, and the only thing I touch each year is the list of campaign codes, which is the next thing to fix.