Trigger Name: ProductTrigger¶
Last Updated: 2025-10-22 (Created: 2025-01-08) Source Code: https://github.com/AANP-IT/I2C.Salesforce.Metadata/blob/STAGING/force-app/main/default/triggers/ProductTrigger.trigger
API Name: ProductTrigger Object: Product2 (Standard Salesforce Object) Pattern: Framework Pattern (TriggerHandler)
Business Purpose¶
This trigger manages Product2 lifecycle events including promotion synchronization and SEO optimization. When products are created or updated, it automatically syncs promotion data and generates/updates SEO-friendly URLs and metadata for e-commerce catalog pages, ensuring products are properly indexed and discoverable.
Trigger Events¶
This trigger fires on the following events: - ☐ Before Insert - ☐ Before Update - ☐ Before Delete - ☑️ After Insert - ☑️ After Update - ☐ After Delete - ☐ After Undelete
Trigger Handler¶
Handler Class: ProductTriggerHandler.cls Pattern: Framework Pattern - Extends TriggerHandler base class Entry Point: Handler instantiation with execute() method
trigger ProductTrigger on Product2 (after insert, after update) {
new ProductTriggerHandler().execute();
}
Process Flow by Event¶
After Insert¶
Purpose: Generate SEO metadata for new products
Business Logic: 1. Collect all newly inserted Product2 records 2. Pass to ProductSEOHelper.processProducts() with 'Insert' mode 3. Generate SEO-friendly URLs, meta descriptions, keywords 4. Update product records with SEO metadata
Methods Called:
- ProductTriggerHandler.afterInsert(Map<Id, SObject> newRecordsMap)
- ProductSEOHelper.processProducts(List<Product2> products, String mode)
Field Updates: - SEO-related fields (URL slugs, meta descriptions, keywords, etc.)
After Update¶
Purpose: Sync promotions and regenerate SEO metadata if Name or Description changed
Conditional Logic: - Fires When: Name or Description fields change - Example: If Product Name changes from 'Widget A' to 'Widget A Pro'
Business Logic: 1. Execute PromotionSyncLogic for promotion synchronization 2. Compare old and new records to detect Name or Description changes 3. Collect products with Name/Description changes 4. Pass to ProductSEOHelper.processProducts() with 'Update' mode 5. Regenerate SEO metadata based on new Name/Description
Methods Called:
- ProductTriggerHandler.afterUpdate(Map<Id, SObject> updatedRecordsMap, Map<Id, SObject> oldRecordsMap)
- PromotionSyncLogic.execute(Map<Id, SObject> updatedRecordsMap, Map<Id, SObject> oldRecordsMap)
- ProductSEOHelper.processProducts(List<Product2> products, String mode)
Field Updates: - SEO fields regenerated when Name/Description changes - Promotion-related fields synchronized
Key Business Rules¶
Validation Rules¶
- SEO processing applies to all new products
- SEO regeneration only when Name or Description changes
Field Updates¶
- SEO Fields: URL slugs, meta descriptions, keywords generated/updated
- Promotion Fields: Synchronized via PromotionSyncLogic
Related Record Operations¶
- Promotions synchronized (details in PromotionSyncLogic)
- May update related catalog/storefront records
Bulkification & Governor Limits¶
Bulkified: Yes Max Records Handled: 200 Product2 records per transaction SOQL Queries: Depends on PromotionSyncLogic and ProductSEOHelper DML Operations: Depends on helper classes
Bulkification Strategy¶
- All products passed to helper classes as lists
- Field change detection done in bulk
- Helper classes handle bulk SOQL/DML
Governor Limit Considerations¶
- SOQL: Queries in PromotionSyncLogic and ProductSEOHelper
- DML: DML in helper classes
- CPU: String manipulation for SEO could be intensive
Recursion Prevention¶
Strategy: None explicitly implemented
Implementation: - No static variables or recursion flags - TriggerHandler framework may provide protection - Helper classes may implement controls
Scenarios: - Product update -> SEO update -> potential Product update (recursion risk) - Monitor for circular updates if SEO helper updates Product2
Execution Order & Dependencies¶
Order of Execution Impact¶
- After Insert/Update: Runs after validation rules
- Standard Salesforce order of execution applies
Dependent Triggers¶
- BuyerGroupPricebookTrigger: May update Product2.VisibleToBuyerGroups__c
- Risk: Cascading updates between triggers
Dependent Processes¶
- PromotionSyncLogic: Promotion synchronization
- ProductSEOHelper: SEO metadata generation
- E-commerce catalog indexing
- Search engine optimization
Error Handling¶
Strategy: Relies on TriggerHandler framework
User Experience: - Success: Product saves, SEO and promotions updated - Validation Error: Standard Salesforce errors - System Error: Full transaction rollback
Logging: - Logging in helper classes - No explicit logging in trigger/handler
Rollback Behavior: - Full transaction rollback on errors
Dependencies¶
Apex Classes¶
ProductTriggerHandler: Handler extending TriggerHandlerTriggerHandler: Base framework classPromotionSyncLogic: Promotion synchronization logicProductSEOHelper: SEO metadata generation
Salesforce Objects¶
Product2: Standard object (primary)- Promotion-related objects
- Catalog/storefront objects
Custom Settings/Metadata¶
- None visible (should add for trigger settings)
External Systems¶
- E-commerce storefront (consumes SEO metadata)
- Search engines (index SEO metadata)
Testing¶
Test Class: PromotionSyncLogicTest.cls (specified in trigger comments) Coverage: Unknown
Test Scenarios: - Single product insert with SEO generation - Bulk product insert (200 records) - Product Name change with SEO regeneration - Product Description change with SEO regeneration - Promotion synchronization - Products without Name/Description changes
Performance Considerations¶
Average Execution Time: Unknown Max Records Processed: 200 per transaction Async Processing: May be used in helper classes
Optimization Opportunities: - Consider async SEO processing for large batches - Cache SEO templates/patterns - Optimize string manipulation for SEO generation - Add field-level tracking to reduce processing
Changes & History¶
- 2025-01-08: Created by Ryan O'Sullivan
- 2025-04-24: Updated by Antoneac Victor (added SEO logic)
- Test Class: PromotionSyncLogicTest
- Pattern: TriggerHandler framework with helper classes
Pre-Go-Live Concerns¶
CRITICAL - Fix Before Go-Live¶
- No Recursion Prevention: SEO updates could trigger recursion
- Field Updates in After Trigger: SEO helper may update Product2 in after context, requiring additional DML
- Performance: String manipulation for SEO on 200 products could timeout
HIGH - Address Soon After Go-Live¶
- No Error Handling: No explicit error handling in trigger/handler
- SEO Logic Coupling: ProductSEOHelper tightly coupled to trigger
- No Field Change Optimization: Processes all updates regardless of field changes
- Missing Logging: No audit trail of SEO generation
MEDIUM - Future Enhancement¶
- Async SEO Processing: Move to Queueable/Batch for large volumes
- Trigger Settings: Custom metadata for enable/disable
- SEO Template Configuration: Move SEO patterns to Custom Metadata
- Better Documentation: Document SEO field usage
LOW - Monitor¶
- CPU Time: Monitor SEO string manipulation performance
- Test Coverage: Verify PromotionSyncLogicTest covers all scenarios
- SEO Field Length: Monitor generated URLs/descriptions don't exceed limits
Maintenance Notes¶
Complexity: Medium-High Recommended Review Schedule: Quarterly
Key Maintainer Notes: - Two distinct pieces of logic: Promotion sync AND SEO generation - SEO logic added after initial creation (2025-04-24 update) - ProductSEOHelper may perform DML in after context (additional update) - Always test with bulk operations (200 products) - SEO regenerates ONLY when Name or Description changes - PromotionSyncLogic runs on all updates (no field filtering) - Test both promotion and SEO scenarios together
Deactivation Instructions: Add custom metadata check:
trigger ProductTrigger on Product2 (after insert, after update) {
if (TriggerSettings__mdt.getInstance('ProductTrigger')?.Disabled__c == true) {
return;
}
new ProductTriggerHandler().execute();
}
Testing Considerations: - Test SEO URL generation with special characters - Verify promotion sync with various product configurations - Test bulk scenarios (200+ products) - Validate SEO metadata within field length limits - Test Name and Description change detection accuracy