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;
    }
}
  1. public class InventoryService { — this is Module 22's service-class pattern, applied to a new object.
  2. public Warehouse_Stock__c receiveStock(Id productId, String warehouseName, Decimal quantity) { — the single entry point for recording initial stock.
  3. Warehouse_Stock__c stock = new Warehouse_Stock__c( — builds the record.
  4. Product__c = productId, — links to the product.
  5. Warehouse_Name__c = warehouseName, — names the warehouse.
  6. Quantity_On_Hand__c = quantity — the starting quantity.
  7. ); — closes the constructor call.
  8. insert stock; — saves it — InventoryService is the single entry point for every inventory operation, exactly like LibraryService was for library operations.
  9. return stock; — hands back the saved record.
  10. } — closes the method.
  11. } — 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');
}
  1. public Decimal getTotalStock(Id productId) { — answers "how much of this product do we have, total?"
  2. AggregateResult result = [ — opens the query.
  3. SELECT SUM(Quantity_On_Hand__c) totalQty — this is Module 20's SUM() aggregate function, put to real use — the database computes the total directly.
  4. FROM Warehouse_Stock__c — the object.
  5. WHERE Product__c = :productId — scoped to one product.
  6. ]; — closes the query.
  7. return (Decimal) result.get('totalQty'); — reads the computed value via its alias, instead of querying every Warehouse_Stock__c record and adding them up in an Apex loop.
  8. } — 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
    ];
}
  1. public List<Warehouse_Stock__c> getStockByWarehouse(Id productId) { — a different, complementary question from getTotalStock — not "how much in total?" but "how much where?"
  2. return [ — opens the query.
  3. SELECT Id, Warehouse_Name__c, Quantity_On_Hand__c — the fields needed.
  4. FROM Warehouse_Stock__c — the object.
  5. WHERE Product__c = :productId — scoped to one product.
  6. ORDER BY Warehouse_Name__c — sorted alphabetically by warehouse.
  7. ]; — closes the query — both useful views over the exact same underlying Warehouse_Stock__c records.
  8. } — 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

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Track Inventory Levels Quiz

1. Why use SUM(Quantity_On_Hand__c) in SOQL rather than looping over records and adding in Apex?

Log in to submit the quiz and save your score.

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?"