Skip to content

Trigger Name: PricebookEntryTrigger

Last Updated: 2025-10-22 (Created: 2024-12-20) Source Code: https://github.com/AANP-IT/I2C.Salesforce.Metadata/blob/STAGING/force-app/main/default/triggers/PricebookEntryTrigger.trigger

API Name: PricebookEntryTrigger Object: PricebookEntryChangeEvent (Change Data Capture Event) Pattern: Framework Pattern (TriggerHandler)

Business Purpose

This trigger uses Change Data Capture to monitor PricebookEntry price changes and automatically update renewal subscription pricing. Since PricebookEntry does not support direct triggers, CDC events are used to detect UnitPrice changes and propagate those changes to existing auto-renewal subscriptions, ensuring subscription pricing stays current with catalog prices.

Trigger Events

This trigger fires on the following events: - ☐ Before Insert - ☐ Before Update - ☐ Before Delete - ☑️ After Insert (CDC events only support after insert) - ☐ After Update - ☐ After Delete - ☐ After Undelete

Trigger Handler

Handler Class: PricebookEntryTriggerHandler.cls Pattern: Framework Pattern - Extends TriggerHandler base class Entry Point: Handler instantiation with execute() method

trigger PricebookEntryTrigger on PricebookEntryChangeEvent (after insert) {
    new PricebookEntryTriggerHandler().execute();
}

Process Flow by Event

After Insert

Purpose: Capture PricebookEntry price changes and update renewal subscriptions

Business Logic: 1. Extract PricebookEntry IDs and new prices from change events 2. Query full PricebookEntry records with UnitPrice and Previous_Price__c 3. Pass to RenewalPriceUpdate.run() to update subscription pricing 4. Update PricebookEntry.Previous_Price__c field with new UnitPrice

Methods Called: - PricebookEntryTriggerHandler.afterInsert(Map<Id, SObject> newRecordsMap) - PricebookEntryTriggerHandler.adjustPriceBookEntries(List<PriceBookEntryChangeEvent> changeEvents) - RenewalPriceUpdate.run(List<PricebookEntry> associatedPriceBookEntries) - PricebookEntryTriggerHandler.updatePriceBookEntries(List<PricebookEntry> pricebookEntries)

Field Updates: - PricebookEntry.Previous_Price__c: Updated to match current UnitPrice

Related Records Updated: - Subscription renewal pricing (via RenewalPriceUpdate)

Key Business Rules

Validation Rules

  • Only processes PricebookEntry CDC events
  • Updates Previous_Price__c only if different from UnitPrice

Field Updates

  • Previous_Price__c: Stores prior price for reference
  • Subscription records updated with new renewal prices
  • Auto-renewal subscriptions synchronized with current catalog pricing

Bulkification & Governor Limits

Bulkified: Yes (CDC) Max Records Handled: CDC event batching supported SOQL Queries: 1 (query PricebookEntry records from event IDs) DML Operations: 1 (update PricebookEntry.Previous_Price__c)

Bulkification Strategy

  • CDC events batched automatically
  • Single SOQL query for all PricebookEntry records in events
  • Single DML operation to update Previous_Price__c
  • RenewalPriceUpdate handles subscription updates

Governor Limit Considerations

  • CDC Limits: Subject to org CDC allocations
  • SOQL: 1 query per trigger execution
  • DML: 1 DML in trigger + DML in RenewalPriceUpdate
  • Async: RenewalPriceUpdate may use async processing

Recursion Prevention

Strategy: CDC events cannot trigger themselves

Implementation: - CDC events fire after committed transactions - Events do not re-trigger on DML operations - Natural recursion protection via CDC architecture

Scenarios: - No recursion risk with CDC events - Updating Previous_Price__c does not create new CDC events in same transaction

Execution Order & Dependencies

Order of Execution Impact

  • CDC events fire after source transaction commits
  • Processing asynchronous from price change
  • Event triggers run in separate transaction

Dependent Triggers

  • None directly

Dependent Processes

  • RenewalPriceUpdate: Updates subscription renewal prices
  • Subscription lifecycle management
  • Auto-renewal processing

Error Handling

Strategy: CDC error handling with retry

User Experience: - Success: Price change captured, subscriptions updated - Event Delivery Failure: Salesforce automatically retries - Processing Error: Error in handler/renewal logic, event acknowledged

Logging: - CDC built-in monitoring - Application logging in RenewalPriceUpdate - Event Monitor in Setup

Rollback Behavior: - Source price change separate from processing - Processing errors don't rollback price changes - Failed updates should be logged

Dependencies

Apex Classes

  • PricebookEntryTriggerHandler: Handler extending TriggerHandler
  • TriggerHandler: Base framework class
  • RenewalPriceUpdate: Subscription price update logic

Salesforce Objects

  • PricebookEntryChangeEvent: CDC event object
  • PricebookEntry: Standard Pricebook2 object
  • Subscription__c: Custom subscription object (likely)

Custom Settings/Metadata

  • CDC entity settings (Salesforce managed)

External Systems

  • None directly

Testing

Test Class: RenewalPriceUpdateTest.cls Coverage: Unknown

Test Scenarios: - Single PricebookEntry price change - Bulk price changes - Price increases and decreases - Previous_Price__c update logic - Integration with RenewalPriceUpdate - CDC event delivery testing

Performance Considerations

Average Execution Time: Fast (CDC routing) Max Records Processed: CDC batch sizes Async Processing: Yes - CDC inherently async

Optimization Opportunities: - Already optimized with CDC - Monitor RenewalPriceUpdate performance - Consider async subscription updates if high volume

Changes & History

  • 2024-12-20: Created by Ryan O'Sullivan
  • Test Class: RenewalPriceUpdateTest
  • Pattern: CDC with TriggerHandler framework
  • Rationale: PricebookEntry doesn't support triggers, CDC required

Pre-Go-Live Concerns

CRITICAL - Fix Before Go-Live

  • CDC Configuration: Verify PricebookEntry CDC properly enabled in production
  • Field Tracking: Confirm UnitPrice field selected for CDC
  • Recursion Risk: Updating PricebookEntry.Previous_Price__c could create new CDC events

HIGH - Address Soon After Go-Live

  • Error Handling: Add try-catch wrapper in handler
  • Event Monitoring: Implement monitoring for CDC processing
  • Subscription Update Failures: Handle scenarios where renewal updates fail
  • Event Replay: Document CDC replay procedures

MEDIUM - Future Enhancement

  • Field Optimization: Only track UnitPrice in CDC
  • Async Subscription Updates: Use Queueable for large subscription bases
  • Event Archiving: Long-term CDC audit strategy
  • Performance Monitoring: Track subscription update latency

LOW - Monitor

  • CDC Limits: Monitor org-wide CDC allocations
  • Event Volume: Track price change frequency
  • Subscription Impact: Monitor number of subscriptions per price change

Maintenance Notes

Complexity: Medium Recommended Review Schedule: Quarterly

Key Maintainer Notes: - Uses CDC because PricebookEntry doesn't support direct triggers - Price changes affect auto-renewal subscriptions - Previous_Price__c update may generate new CDC events - monitor for loops - RenewalPriceUpdate class contains core subscription update logic - Always test price increases, decreases, and no-change scenarios - CDC events async - timing considerations for subscription updates - Use CDC Event Monitor to troubleshoot delivery issues

Deactivation Instructions: Add custom metadata check:

trigger PricebookEntryTrigger on PricebookEntryChangeEvent (after insert) {
    if (TriggerSettings__mdt.getInstance('PricebookEntry')?.Disabled__c == true) {
        return;
    }
    new PricebookEntryTriggerHandler().execute();
}

CDC Considerations: - PricebookEntry CDC must be enabled in Setup - Select UnitPrice field for CDC tracking - CDC events have 3-day retention for replay - Monitor CDC allocations (varies by Salesforce edition) - Test price change scenarios thoroughly in sandbox