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;
}
public void receiveShipment(Id warehouseStockId, Decimal quantityReceived) {— the method.Warehouse_Stock__c stock = [— reading the record first, exactly Module 19's "Update" lesson pattern.SELECT Id, Quantity_On_Hand__c FROM Warehouse_Stock__c WHERE Id = :warehouseStockId— queries the current value.];— closes the query.stock.Quantity_On_Hand__c += quantityReceived;—+=is Module 4's compound assignment operator, doing real inventory work, adding to the current value.update stock;— saves the change.}— 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;
}
public Boolean shipOrder(Id warehouseStockId, Decimal quantityShipped) {— returns whether the shipment succeeded.Warehouse_Stock__c stock = [— queries the current stock.SELECT Id, Quantity_On_Hand__c FROM Warehouse_Stock__c WHERE Id = :warehouseStockId— the current value.];— closes the query.- (blank line)
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.System.debug('Not enough stock to ship this quantity.');— logs a clear reason.return false;— rather than silently creating an impossible negative quantity.}— closes theif.- (blank line)
stock.Quantity_On_Hand__c -= quantityShipped;— only reached if there was enough stock.update stock;— saves the change.return true;— reports success.}— 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
// DON'T DO THIS— the label says it all.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
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.