Skip to content

Class Name: ProductTriggerHandler

Last Updated: 2025-10-22 Source Code: https://github.com/AANP-IT/I2C.Salesforce.Metadata/blob/STAGING/force-app/main/default/classes/ProductTriggerHandler.cls

API Name: ProductTriggerHandler Type: Trigger Handler Test Coverage: To be determined

Business Purpose

The ProductTriggerHandler manages Product2 object trigger logic, handling two key workflows: 1. Promotion Sync: Updates promotions when product details change 2. SEO Processing: Generates SEO-friendly URLs when product names or descriptions change

This handler maintains data consistency for product catalog and improves product discoverability.

Class Overview

  • Author: Ryan O'Sullivan (original), Antoneac Victor (SEO updates)
  • Created: 2025-01-08
  • Last Modified: 2025-04-24
  • Test Class: PromotionSyncLogicTest
  • Scope/Sharing: with sharing - Respects record-level security
  • Extends: TriggerHandler
  • Supported Events: afterUpdate, afterInsert

Public Methods

afterUpdate

override protected void afterUpdate(Map<Id, SObject> updatedRecordsMap, Map<Id, SObject> oldRecordsMap)

Purpose: Processes product updates for promotion sync and SEO URL generation.

Business Logic:

  1. Promotion Sync (line 14):
    PromotionSyncLogic.execute(updatedRecordsMap,oldRecordsMap);
    
  2. Delegates to PromotionSyncLogic for promotion updates

  3. SEO Change Detection (lines 17-28):

  4. Detects Name or Description changes
  5. Filters products requiring SEO processing
  6. Checks for actual changes (not just null comparisons)

  7. SEO Processing (lines 30-32):

    if (!productsToProcess.isEmpty()) {
        ProductSEOHelper.processProducts(productsToProcess, 'Update');
    }
    

  8. Generates SEO-friendly URLs for changed products

Issues/Concerns: - ⚠️ Redundant Null Check (lines 23-24): Checks newProd.Name != oldProd.Name then checks newProd.Name == null - Second check is unnecessary - ✅ Change Detection: Properly detects field changes - ✅ Conditional Processing: Only processes products with relevant changes

afterInsert

override protected void afterInsert(Map<Id, SObject> newRecordsMap)

Purpose: Processes newly created products for SEO URL generation.

Business Logic: 1. Converts SObject map to Product2 list 2. Calls ProductSEOHelper for all new products 3. Uses 'Insert' operation type

Issues/Concerns: - ✅ Simple Implementation: Straightforward new product handling - ✅ Bulk Processing: Handles all new products in single call

Dependencies

Salesforce Objects

  • Product2 (Standard Object)
  • Fields: Id, Name, Description
  • Trigger: This handler

Other Classes

  • TriggerHandler: Base trigger framework
  • PromotionSyncLogic: Handles promotion updates
  • ProductSEOHelper: Generates SEO URLs

Design Patterns

  1. Trigger Handler Pattern: Extends TriggerHandler base class
  2. Separation of Concerns: Delegates to specialized logic classes
  3. Change Detection: Only processes records with relevant field changes

Governor Limits Considerations

Current Impact

  • SOQL Queries: 0 (delegated to helper classes)
  • DML Statements: 0 (delegated to helper classes)
  • CPU Time: Minimal (field comparison only)

Scalability

  • Bulk Processing: Processes all records in single calls
  • Conditional Logic: Only processes changed products

Test Class Requirements

See PromotionSyncLogicTest (test class exists)

Pre-Go-Live Concerns

MEDIUM

  • Redundant Null Check (lines 23-24): Simplify condition logic

LOW

  • No Error Handling: Delegates error handling to helper classes

Changes & History

Date Author Description
2025-01-08 Ryan O'Sullivan Initial implementation with promotion sync
2025-04-24 Antoneac Victor Added SEO processing logic

Documentation Status: ✅ Complete Code Review Status: ✅ Approved Test Coverage: PromotionSyncLogicTest exists