i looked at get_stock_balance() in erpnext/erpnext/stock/utils.py at develop · frappe/erpnext · GitHub and saw that it has inventory dimension as input but the following two calls return the same value:
without inventory dimension
x = frappe.call('erpnext.stock.utils.get_stock_balance',
item_code='STO-ITEM-2025-00001',
warehouse='Finished Goods - T'
)
print(x)
output: 1471.0
with inventory dimension:
x = frappe.call('erpnext.stock.utils.get_stock_balance',
item_code='STO-ITEM-2025-00001',
warehouse='Finished Goods - T',
inventory_dimensions_dict={'customer': 'test'}
)
print(x)
output: 1471.0
i think i know its because erpnext.stock.utils.get_stock_balance returns last_entry.qty_after_transaction but how else do i get the balance value of 20 that i see in the stock balance report ? maybe i call the function in a wrong way ? is there another function for this that is whitelisted or another way to get this ? I would appreciate any help 
this is my solution for others also needing this until someone more knowledgable answers the question. I dont know for sure if this is how to actually calculate the correct stock balance but in my local testing it returns the correct qty.
I hope some day they will further enhance the inventory dimensions feature and add them to the bin doctype as well as making them selectable everywhere a warehouse is, because this feature already is very powerfull.
def get_dimensioned_stock_qty(item_code, warehouse, inventory_dimensions_dict=None):
filters = {"item_code": item_code, "warehouse": warehouse}
if inventory_dimensions_dict:
filters.update(inventory_dimensions_dict)
return sum(sle.actual_qty for sle in frappe.get_all("Stock Ledger Entry",filters=filters,fields=["actual_qty"]))
example call iterating over item child table and using inventory dimension of doctype customer:
for i, row in enumerate(doc.items):
dimensioned_qty = get_dimensioned_stock_qty(row.item_code,row.warehouse,{"customer": doc.customer})
1 Like
Update:
I tested the code again on another instance and it works as expected in server scripts:
x = frappe.call(
'erpnext.stock.utils.get_stock_balance',
item_code='STO-ITEM-2025-00001',
warehouse='Finished Goods - T',
inventory_dimensions_dict={'customer': 'test'}
)
print(x)
A few important notes:
- Use the code (not the name) of both the item and the inventory dimension Doctype.
- The
inventory_dimensions_dict
expects the fieldname that gets created in Stock Ledger Entry when you create the inventory dimension.
- This fieldname is based on the Doctype name in lowercase, not the display name you gave to the dimension.
For example, if you create an inventory dimension called “Customer Inventory Dimension”, the fieldname will simply be:
customer
and not:
custom_customer_inventory_dimension or
customer_inventory_dimension
as one might expect.
Thanks to no one for helping with this 
1 Like