Salesforce D2C Commerce: Dynamic Buyer Groups

Learn how to automatically assign customers to Buyer/Customer Groups in Salesforce Commerce Cloud based on real-time order data. This guide walks through using Apex and the OrderSummaryCreatedEvent to personalize experiences for high-value customers without manual effort.
Summary
Introduction
In today’s competitive landscape, customer segmentation and personalized marketing are main drivers of business success. Salesforce Commerce Cloud empowers businesses to deliver tailored experiences through powerful tools like Buyer Groups, an essential feature that allows organizations to create unique journeys for specific customer segments.
By leveraging Buyer Groups, businesses can provide targeted incentives, offers, and experiences. For example, a Buyer Group can be created for high-value customers those who consistently make large purchases and offer them exclusive discounts or early access to promotions.
However, there’s one limitation: out-of-the-box Buyer Groups in Salesforce Commerce Cloud are static. That means customer assignments to these groups need to be done manually, which can be inefficient and error-prone especially as new order data comes in.
In this blog post, we’ll walk through how to dynamically assign commerce users to Buyer Groups in Salesforce Commerce Cloud based on real-time purchase behavior.
Note: If you are already using Salesforce Data Cloud than you should check out the Salesforce Data Cloud Buyer Groups feature.
The code for this blog post is available on GitHub.
The Business Need
Imagine a scenario where we want to automatically group our top-spending customers those who’ve spent more than $1,000 in total into a Buyer Group called “High Lifetime Sales Customers”. This group would receive personalized offers as a reward for their loyalty.
To make this work, we need a way to dynamically evaluate a customer’s order history and update their group membership accordingly, without manual intervention.
Solution
To automate Buyer Group assignments, we can leverage Salesforce’s standard platform events specifically, the OrderSummaryCreatedEvent. This event is triggered every time an order summary is created.
Our solution involves creating an Apex trigger that listens to this event, evaluates the buyer’s cumulative order value, and adds them to the appropriate Buyer Group if they meet the criteria.
The high level flow of the solution is as follows:
- Listen to the OrderSummaryCreatedEvent: Create an Apex trigger that listens for this standard platform event.
- Aggregate buyer sales history: Within the trigger, use an aggregate SOQL query to calculate the buyer’s total spend.
- Evaluate against threshold: If the total exceeds the defined threshold (e.g., $1,000), assign the buyer to the “High Lifetime Sales Customers” Buyer Group.
- Avoid redundant updates: If the buyer is already in the group, skip the update to prevent unnecessary processing.