Intermediate
30 min read
Track Inventory Levels
By the end of this lesson, you'll be able to:
- Write an InventoryService class with a method to record initial stock
- Query total stock for a product summed across every warehouse
- Explain how a relationship query reaches from product to its stock records
Prerequisites: "Design the Stock Model"
Recording initial stock
public class InventoryService {
public Warehouse_Stock__c receiveStock(Id productId, String warehouseName, Decimal quantity) {
Warehouse_Stock__c stock = new Warehouse_Stock__c(
Product__c = productId,
Warehouse_Name__c = warehouseName,
Quantity_On_Hand__c = quantity
);
insert stock;
return stock;
}
}
public class InventoryService {— this is Module 22's service-class pattern, applied to a new object.public Warehouse_Stock__c receiveStock(Id productId, String warehouseName, Decimal quantity) {— the single entry point for recording initial stock.Warehouse_Stock__c stock = new Warehouse_Stock__c(— builds the record.Product__c = productId,— links to the product.Warehouse_Name__c = warehouseName,— names the warehouse.Quantity_On_Hand__c = quantity— the starting quantity.);— closes the constructor call.insert stock;— saves it —InventoryServiceis the single entry point for every inventory operation, exactly likeLibraryServicewas for library operations.return stock;— hands back the saved record.}— closes the method.}— closes the class.
Summing stock across every warehouse
public Decimal getTotalStock(Id productId) {
AggregateResult result = [
SELECT SUM(Quantity_On_Hand__c) totalQty
FROM Warehouse_Stock__c
WHERE Product__c = :productId
];
return (Decimal) result.get('totalQty');
}
public Decimal getTotalStock(Id productId) {— answers "how much of this product do we have, total?"AggregateResult result = [— opens the query.SELECT SUM(Quantity_On_Hand__c) totalQty— this is Module 20'sSUM()aggregate function, put to real use — the database computes the total directly.FROM Warehouse_Stock__c— the object.WHERE Product__c = :productId— scoped to one product.];— closes the query.return (Decimal) result.get('totalQty');— reads the computed value via its alias, instead of querying everyWarehouse_Stock__crecord and adding them up in an Apex loop.}— closes the method.
Listing stock by warehouse
public List<Warehouse_Stock__c> getStockByWarehouse(Id productId) {
return [
SELECT Id, Warehouse_Name__c, Quantity_On_Hand__c
FROM Warehouse_Stock__c
WHERE Product__c = :productId
ORDER BY Warehouse_Name__c
];
}
public List<Warehouse_Stock__c> getStockByWarehouse(Id productId) {— a different, complementary question fromgetTotalStock— not "how much in total?" but "how much where?"return [— opens the query.SELECT Id, Warehouse_Name__c, Quantity_On_Hand__c— the fields needed.FROM Warehouse_Stock__c— the object.WHERE Product__c = :productId— scoped to one product.ORDER BY Warehouse_Name__c— sorted alphabetically by warehouse.];— closes the query — both useful views over the exact same underlyingWarehouse_Stock__crecords.}— closes the method.
Exercise
Add a getWarehouseCount(Id productId) method returning how many distinct warehouses currently stock a product, using COUNT().
Show hint
SELECT COUNT() FROM Warehouse_Stock__c WHERE Product__c = :productId
Track Inventory Levels 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
This lesson builds InventoryService's foundation — recording initial stock, and answering "how much of this product do we have, total, across every warehouse?"