Flow Name: Order - On Create - Before Save¶
Last Updated: 2025-10-22 Source Code: Order_On_Create_On_Update_Before_Save.flow-meta.xml
API Name: Order_On_Create_On_Update_Before_Save Status: Active Type: Custom Trigger: Record-Triggered Flow - Before Save on Order (Create only)
Business Purpose¶
This flow performs critical order setup tasks for manual orders before they are saved to the database. It handles billing contact assignment, address population, sales store association, and price book configuration to ensure orders are properly configured for processing.
Process Flow¶
1. Manual Order Validation¶
- Retrieves "Manual_Order" record type
- Validates if current order uses Manual_Order record type
- Retrieves B2C type WebStore for manual orders
- Exits if not a manual order
2. Account Type Determination¶
- Determines if account is a person account (IsPersonAccount)
- Person Account Path: Uses PersonContactId directly for contact assignment
- Business Account Path: Implements complex contact selection logic
3. Business Account Contact Assignment (Sequential Search Strategy)¶
Implements three-stage contact search for business accounts: - Stage 1: Search for contacts with "Billing Contact" role - Stage 2: Search for contacts with "Company Administrator" role - Stage 3: Search for any active AccountContactRelation
For contacts with dates (StartDate/EndDate): - Prioritizes most recent relationship based on date comparisons - Compares start and end dates to find current relationship
For contacts without dates: - Falls back to first available active relationship
4. Buyer Group and Price Book Setup¶
- Retrieves buyer group members for the account
- Gets associated price book from buyer group
- Assigns existing or buyer group price book to order
5. Order Field Population¶
Updates order fields before save: - Contact Information: BillToContactId, ShipToContactId, Bill_To__c, Ship_To__c - Address Information: Billing and shipping addresses using formula-based logic - Person accounts: Uses PersonMailing fields - Business accounts: Uses Business_Address__ fields - Order Configuration: EffectiveDate, Manual_Order__c, Pricebook2Id, SalesStoreId, Status
📊 Click to view Process Flow Diagram
flowchart TD
Start([Start: Order Created<br/>AccountId populated]) --> GetRT[Get Manual_Order<br/>Record Type]
GetRT --> CheckRT{Is Manual<br/>Order?}
CheckRT -->|No| End([End])
CheckRT -->|Yes| GetStore[Get B2C<br/>Sales Store]
GetStore --> CheckAcctType{Is Person<br/>Account?}
CheckAcctType -->|Yes| UsePersonContact[Use PersonContactId<br/>for Contact Assignment]
CheckAcctType -->|No| SearchBilling[Search for<br/>Billing Contact Role]
SearchBilling --> FoundBilling{Billing Contact<br/>Found?}
FoundBilling -->|Yes| SelectBilling[Select Most Recent<br/>by Date]
FoundBilling -->|No| SearchAdmin[Search for<br/>Company Administrator]
SearchAdmin --> FoundAdmin{Admin<br/>Found?}
FoundAdmin -->|Yes| SelectAdmin[Select Most Recent<br/>by Date]
FoundAdmin -->|No| SearchAny[Search Any Active<br/>Relationship]
SearchAny --> SelectAny[Select First<br/>Available Contact]
UsePersonContact --> GetBuyer[Get Buyer Group<br/>and Pricebook]
SelectBilling --> GetBuyer
SelectAdmin --> GetBuyer
SelectAny --> GetBuyer
GetBuyer --> PopulateFields[Populate Order Fields:<br/>Contact, Address, Status,<br/>Pricebook, Store]
PopulateFields --> End
style Start fill:#e1f5ff
style GetRT fill:#f0e1ff
style CheckRT fill:#fff4e1
style GetStore fill:#f0e1ff
style CheckAcctType fill:#fff4e1
style UsePersonContact fill:#e1ffe1
style SearchBilling fill:#f0e1ff
style FoundBilling fill:#fff4e1
style SelectBilling fill:#e1ffe1
style SearchAdmin fill:#f0e1ff
style FoundAdmin fill:#fff4e1
style SelectAdmin fill:#e1ffe1
style SearchAny fill:#f0e1ff
style SelectAny fill:#e1ffe1
style GetBuyer fill:#f0e1ff
style PopulateFields fill:#ffe1e1
style End fill:#e1f5ff
Key Business Rules¶
- Only processes orders with Manual_Order record type during creation
- Requires AccountId to be populated
- Person accounts use PersonContactId directly for contact assignment
- Business accounts implement three-stage contact search: Billing Contact → Company Administrator → Any Active Relationship
- Contact selection prioritizes most recent relationships when start/end dates exist
- Address population uses formula-based logic to handle person vs business accounts
- Sets Order Status to "Draft" initially
- Associates order with B2C sales store
- Populates EffectiveDate with current date
- Sets Manual_Order__c flag to true
Dependencies¶
- Order object with custom fields
- Record Type: Manual_Order
- Account object with IsPersonAccount, PersonContactId, PersonMailing fields, Business_Address__ fields
- AccountContactRelation object with Roles field and date tracking
- BuyerGroupMember and BuyerGroup objects
- BuyerGroupPricebook object for price book association
- WebStore object with Type field
Changes¶
No specific pull requests or changes noted in the flow metadata.
Pre-Go-Live Concerns¶
CRITICAL - Fix Before Go-Live¶
- Simplify complex three-stage contact selection logic to reduce edge case failures
- Add comprehensive null checking for all formula-based address population
- Implement validation that B2C WebStore exists before order creation
- Add error handling for missing required contact relationships
HIGH - Address Soon After Go-Live¶
- Add fault handling for all record type and store lookups
- Implement clear error messages for each failure scenario (missing contacts, missing stores, etc.)
- Add logging for contact selection decisions to aid troubleshooting
- Validate that buyer group pricebook associations are properly configured
MEDIUM - Future Enhancement¶
- Make record type name and store type configurable via Custom Metadata
- Reduce SOQL queries through more efficient contact selection logic
- Consider moving complex formula logic to Apex for better error handling
- Cache frequently accessed data (record types, sales stores)
- Add business rule documentation for contact priority logic
LOW - Monitor¶
- Track orders failing due to missing contact relationships
- Monitor for orders with incomplete address data
- Verify buyer group and pricebook associations are working correctly
- Track performance impact of before-save processing
Maintenance Notes¶
- Complexity: Very High - complex contact selection with three fallback stages and date comparisons
- Before-save trigger means flow must complete quickly to avoid transaction timeouts
- Heavy reliance on formula fields for address population creates tight coupling
- Contact selection logic includes multiple queries and complex date comparisons
- Variables used for contact tracking and relationship date comparisons
- Review quarterly to ensure contact role names remain consistent
- Test thoroughly when making changes to account structures or contact relationships
- Document any changes to contact selection priority clearly