In Microsoft Dynamics 365 Finance and Operations (D365 FO), you can use the below x++ codes for getting on hand inventory (quantity) of an item. Here you need to pass the two arguments ItemId and InventDimId. You can copy and paste the below codes and change them as per your requirement. Here's an example of how you can achieve this:
public InventQtyAvailPhysical onHandInventory(ItemId
_itemId, InventDimId _inventDimId)
{
InventOnhand inventOnhand;
InventDim inventDim =
inventDim::find(_inventDimId);
InventDimParm inventDimParm;
inventDimParm.initFromInventDim(inventDim);
inventOnhand =
InventOnhand::newParameters(_itemId, inventDim, inventDimParm);
return
inventOnhand.availPhysical();
}