Intermediate 30 min read

Process Stock Adjustments (DML)

By the end of this lesson, you'll be able to:

  • Increase stock when a shipment is received
  • Decrease stock when an order ships out, without allowing it to go negative
  • Explain why an adjustment reads the current value before writing a new one

Prerequisites: "Query Low-Stock Items"

Receiving a shipment (increasing stock)

public void receiveShipment(Id warehouseStockId, Decimal quantityReceived) {
    Warehouse_Stock__c stock = [
        SELECT Id, Quantity_On_Hand__c FROM Warehouse_Stock__c WHERE Id = :warehouseStockId
    ];
    stock.Quantity_On_Hand__c += quantityReceived;
    update stock;
}
  1. public void receiveShipment(Id warehouseStockId, Decimal quantityReceived) { — the method.
  2. Warehouse_Stock__c stock = [ — reading the record first, exactly Module 19's "Update" lesson pattern.
  3. SELECT Id, Quantity_On_Hand__c FROM Warehouse_Stock__c WHERE Id = :warehouseStockId — queries the current value.
  4. ]; — closes the query.
  5. stock.Quantity_On_Hand__c += quantityReceived; — += is Module 4's compound assignment operator, doing real inventory work, adding to the current value.
  6. update stock; — saves the change.
  7. } — closes the method.

Shipping an order out (decreasing stock)

public Boolean shipOrder(Id warehouseStockId, Decimal quantityShipped) {
    Warehouse_Stock__c stock = [
        SELECT Id, Quantity_On_Hand__c FROM Warehouse_Stock__c WHERE Id = :warehouseStockId
    ];

    if (stock.Quantity_On_Hand__c < quantityShipped) {
        System.debug('Not enough stock to ship this quantity.');
        return false;
    }

    stock.Quantity_On_Hand__c -= quantityShipped;
    update stock;
    return true;
}
  1. public Boolean shipOrder(Id warehouseStockId, Decimal quantityShipped) { — returns whether the shipment succeeded.
  2. Warehouse_Stock__c stock = [ — queries the current stock.
  3. SELECT Id, Quantity_On_Hand__c FROM Warehouse_Stock__c WHERE Id = :warehouseStockId — the current value.
  4. ]; — closes the query.
  5. (blank line)
  6. if (stock.Quantity_On_Hand__c < quantityShipped) { — checking before subtracting prevents stock from ever going negative — a warehouse can't ship out more than it actually has.
  7. System.debug('Not enough stock to ship this quantity.'); — logs a clear reason.
  8. return false; — rather than silently creating an impossible negative quantity.
  9. } — closes the if.
  10. (blank line)
  11. stock.Quantity_On_Hand__c -= quantityShipped; — only reached if there was enough stock.
  12. update stock; — saves the change.
  13. return true; — reports success.
  14. } — closes the method.

Why read-then-write, not a blind overwrite

// DON'T DO THIS
stock.Quantity_On_Hand__c = quantityReceived; // overwrites, doesn't add
  1. // DON'T DO THIS — the label says it all.
  2. stock.Quantity_On_Hand__c = quantityReceived; — setting the field directly to the shipment quantity would erase whatever stock was already there instead of adding to it — the read-then-modify-then-write pattern (query the current value, change it, update) is what makes an adjustment actually additive rather than destructive.

Exercise

Given a Warehouse_Stock__c already queried with Quantity_On_Hand__c, write code that adds 25 to its quantity and saves the change.

Show hint

stock.Quantity_On_Hand__c += 25; update stock;

APEX

Process Stock Adjustments (DML) Quiz

1. Why does shipOrder check the current quantity before subtracting?

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

A stock adjustment is never a blind overwrite — it always reads the current quantity first, then applies a change relative to it, guarding against an impossible negative result.