Flow Name: Update Buyer Group Member On Delete¶
Last Updated: 2025-10-22 Source Code: https://github.com/AANP-IT/I2C.Salesforce.Metadata/blob/STAGING/force-app/main/default/flows/Update_Buyer_Group_Member_On_Delete.flow-meta.xml
API Name: Update_Buyer_Group_Member_On_Delete Status: Active Type: Custom Trigger: Before BuyerGroupMember record is deleted
Business Purpose¶
This flow maintains the consolidated BuyerGroupIds__c field on Account records by removing deleted buyer group memberships from the comma-separated ID list. It ensures the denormalized buyer group data remains accurate when memberships are removed.
Process Flow¶
- BuyerGroupMember Deleted: User initiates deletion of buyer group membership (before delete timing)
- Lookup Account: Retrieves the related Account record using BuyerId
- Validate Account Found: Checks if account exists before proceeding
- Query All Memberships: Retrieves all existing BuyerGroupMember records for this account
- Clear Existing List: Resets the BuyerGroupIds__c field to empty
- Loop Through Memberships: Iterates through all memberships, excluding the one being deleted
- Build Updated List: Concatenates remaining buyer group IDs with commas
- Update Account: Based on results:
- If IDs remain: Updates account with remaining IDs
- If no IDs remain: Clears the field entirely
- Complete Deletion: BuyerGroupMember deletion proceeds
📊 Click to view Process Flow Diagram
flowchart TD
Start([Start: BuyerGroupMember Delete]) --> Lookup[Lookup Related Account]
Lookup --> Found{Account<br/>Found?}
Found -->|No| End([End])
Found -->|Yes| QueryAll[Query All BuyerGroupMembers<br/>for Account]
QueryAll --> Clear[Clear BuyerGroupIds Field]
Clear --> Loop[Loop Through Memberships<br/>Exclude Deleted One]
Loop --> HasRemaining{Any IDs<br/>Remaining?}
HasRemaining -->|Yes| UpdateWithIds[Update Account<br/>with Remaining IDs]
HasRemaining -->|No| ClearField[Update Account<br/>Clear Field]
UpdateWithIds --> End
ClearField --> End
style Start fill:#e1f5ff
style Lookup fill:#f0e1ff
style Found fill:#fff4e1
style QueryAll fill:#f0e1ff
style Clear fill:#e1ffe1
style Loop fill:#e1ffe1
style HasRemaining fill:#fff4e1
style UpdateWithIds fill:#ffe1e1
style ClearField fill:#ffe1e1
style End fill:#e1f5ff
Key Business Rules¶
- Rebuilds entire buyer group ID list excluding the deleted membership (not incremental removal)
- Uses before-delete timing to access the deleted record's BuyerGroupId
- Handles two scenarios:
- If other memberships remain: updates BuyerGroupIds__c with remaining IDs
- If no memberships remain: clears BuyerGroupIds__c field entirely
- Maintains same comma-separated format as creation flow
- Ensures data consistency by complete rebuild
Dependencies¶
- Objects: BuyerGroupMember, Account
- Fields:
- BuyerGroupMember: BuyerId, BuyerGroupId
- Account: BuyerGroupIds__c (text field for comma-separated list)
- Relationships: BuyerGroupMember.BuyerId → Account.Id
Changes¶
- PR-32376: Product Detail Pages that require user permissions should force login (noted in description)
⚠️ Pre-Go-Live Concerns¶
CRITICAL - Fix Before Go-Live¶
None identified.
HIGH - Address Soon After Go-Live¶
- Before-delete timing risks: Limited error recovery if update fails during deletion transaction
- No error handling: If account lookup or update fails, deletion may proceed with stale data on account
- Performance concern: Full list rebuild on every deletion is inefficient
MEDIUM - Future Enhancement¶
- Consider after-delete timing with stored values for better error recovery
- Add error handling and logging for troubleshooting
- Monitor performance with accounts having many buyer group memberships
- Consider incremental removal (string manipulation) instead of full rebuild
LOW - Monitor¶
- Test with accounts going from multiple memberships to zero memberships
- Verify proper handling when last buyer group membership is removed
- Ensure consistency between create and delete flows
Maintenance Notes¶
- Complexity: Medium-High - uses queries, loops, filtering, and dual update paths
- Review Schedule: When buyer group processes change or performance issues arise
- Testing Focus: Test deletion scenarios with 1, 2, 5, and 10+ remaining memberships, plus scenario where last membership is deleted
- Companion Flow: Works in tandem with Update_Buyer_Group_Member_On_Create flow