Inventory API: Code Cookbook

Retrieve the history of inventory changes for an item

Before you start
Permalink Get a link to this section

Retrieve history of inventory changes
Permalink Get a link to this section

To retrieve the history of inventory adjustments and physical counts for a specific item variation (for example, to create an audit log), call the RetrieveInventoryChanges endpoint with the Catalog object ID of the item variation you care about. You can also use a location ID to filter the search against specific locations. See the RetrieveInventoryChanges endpoint in the Connect API Technical Reference for more information on the response fields.

String cursor = null; // Fetch initial page of results
while (true) {
    RetrieveInventoryChangesResponse changeResponse =
        inventoryApi.retrieveInventoryChanges(variation1Id, locationId1, null);

    for (InventoryChange change: changeResponse.getChanges()) {
        // Process change (physical count or adjustment)
        switch (change.getType()) {
        case ADJUSTMENT:
            InventoryAdjustment adjustment = change.getAdjustment();
            System.out.println("Adjustment for item variation " +
                adjustment.getCatalogObjectId() + ":");
            System.out.println("  From state: " + adjustment.getFromState() +
                " at location " + adjustment.getLocationId());
            System.out.println("    To state: " + adjustment.getToState() +
                " at location " + adjustment.getLocationId());
            System.out.println("     Quantity: " + adjustment.getQuantity());
            break;

        case PHYSICAL_COUNT:
            InventoryPhysicalCount count = change.getPhysicalCount();
            System.out.println("Physical count for item variation " +
                count.getCatalogObjectId() + ":");
            System.out.println("     Counted at: " + count.getOccurredAt());
            System.out.println("       Location: " + count.getLocationId());
            System.out.println("       Quantity: " + count.getQuantity());
            break;
        }

        case TRANSFER:
          InventoryTransfer transfer = change.getTransfer();
            System.out.println("Transfer for item variation " +
                transfer.getCatalogObjectId() + ":");
            System.out.println("  From location: " + transfer.getFromLocationId() +
                " at state " + transfer.getState());
            System.out.println("    To location: " + transfer.getToLocationId() +
                " at state " + transfer.getState());
            System.out.println("     Quantity: " + transfer.getQuantity());
            break;
    }

    // Use cursor from response to fetch additional pages.
    cursor = changeResponse.getCursor();
    if (cursor == null || cursor.length() == 0) {
        break;
    }
}