Design the Stock Model
By the end of this lesson, you'll be able to:
- Design a data model where one product can have stock in multiple warehouses
- Decide between storing a stock quantity on the product vs a separate related record
- Apply the Module 22 data-modeling process to a new, differently-shaped problem
Prerequisites: Module 22: "Project: Library Management"
What we're building
Over this module you'll build a small inventory system that:
- Tracks how much stock of each product exists, per warehouse.
- Lets a warehouse manager query which products are running low.
- Processes stock adjustments (receiving new stock, shipping stock out).
- Searches across warehouses for a product by name or SKU.
Same overall shape as Module 22's library project, but the underlying data relationship is genuinely different — worth designing carefully before writing any Apex.
Why a single quantity field on Product__c falls short
// A tempting but flawed first attempt
Product__c {
Name__c
SKU__c
Quantity_On_Hand__c // one number... but which warehouse?
}
If a product exists in three warehouses, one Quantity_On_Hand__c field can't represent "50 units in Cape Town, 30 in Durban, 0 in Johannesburg" — it can only hold one number. This is the same kind of modeling mistake Module 16 flagged when discussing parallel Lists that drift out of sync: cramming a genuinely multi-valued fact into a single field.
The fix: a separate stock record per product/warehouse pair
Product__c
- Name__c (Text)
- SKU__c (Text, External ID)
Warehouse_Stock__c
- Product__c (Lookup to Product__c)
- Warehouse_Name__c (Text)
- Quantity_On_Hand__c (Number)
- Reorder_Threshold__c (Number)
Warehouse_Stock__c is a separate record for each product/warehouse combination — a product in three warehouses means three Warehouse_Stock__c records, each with its own quantity. This is genuinely the same relationship shape as Module 16's "Map of Lists": one product, many stock records, one per warehouse.
Exercise
As a comment, explain why a single Quantity_On_Hand__c field directly on Product__c cannot correctly represent stock spread across three warehouses.
Show hint
One field can only hold one value at a time.
Design the Stock Model Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
A single product can sit in several warehouses at once with a different quantity in each — this lesson designs a data model that reflects that reality instead of flattening it away.