title: Create a delivery via SAP DI API
body: <p>I am using SAP B1 9.1 HANA and programmatically creating a delivery upon successful shipment of a product. When the delivery is created, I get <strong>Quantity falls into negative inventory [DLN1.ItemCode][line: 1]</strong> for a product with positive on-hand inventory. Has anyone seen this error?</p>
  
  <p>Thanks.</p>
  
  <p>Here is the code.  </p>
  
  <pre><code>Private Function createDelivery(SalesOrderKey As Integer, MyConn As OdbcConnection)
  
          Dim myString As String = ""
          Dim ivar As Integer
          Dim icnt As Integer
          Dim oSalesOrder As SAPbobsCOM.Documents
      Dim oDeliveryNote As SAPbobsCOM.Documents
      Dim oDeliveryLines As SAPbobsCOM.Documents
      Dim lRetCode As Integer
      Dim PostSuccess As Boolean
      Dim DeliveryLineAdded As Boolean
      oSalesOrder = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders)
      If oSalesOrder.GetByKey(SalesOrderKey) Then
          oDeliveryNote = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDeliveryNotes)
          oDeliveryNote.CardCode = oSalesOrder.CardCode
          oDeliveryNote.SalesPersonCode = oSalesOrder.SalesPersonCode
          oDeliveryNote.DocType = SAPbobsCOM.BoDocumentTypes.dDocument_Items
          oDeliveryNote.DocumentSubType = SAPbobsCOM.BoDocumentSubType.bod_None
          oDeliveryNote.Address = oSalesOrder.Address
          oDeliveryNote.ShipToCode = oSalesOrder.ShipToCode
          oDeliveryNote.Address2 = oSalesOrder.Address2
          oDeliveryNote.HandWritten = SAPbobsCOM.BoYesNoEnum.tNO
          oDeliveryNote.JournalMemo = "Delivery - " &amp; oSalesOrder.CardCode
          oDeliveryNote.Comments = "Based on SO " &amp; Convert.ToString(SalesOrderKey)
          oDeliveryNote.PaymentGroupCode = oSalesOrder.PaymentGroupCode
          oDeliveryNote.DocObjectCode = SAPbobsCOM.BoObjectTypes.oDeliveryNotes
          oDeliveryNote.DocObjectCodeEx = SAPbobsCOM.BoObjectTypes.oDeliveryNotes
          oDeliveryNote.DocDueDate = Now.Month &amp; "/" &amp; Now.Day &amp; "/" &amp; Now.Year
          oDeliveryNote.TrackingNumber = ""
  
          For icnt = 0 To oSalesOrder.Lines.Count - 1
              oSalesOrder.Lines.SetCurrentLine(icnt)
              oDeliveryNote.Lines.ItemCode = oSalesOrder.Lines.ItemCode
              oDeliveryNote.Lines.Quantity = oSalesOrder.Lines.Quantity
  
  
              UpdateSystemLog(0, 0, "Insert Delivery SKU: " + oSalesOrder.Lines.ItemCode, "IME-D0V", MyConn)
              UpdateSystemLog(0, 0, "Insert Delivery Quantity: " + Convert.ToString(oSalesOrder.Lines.Quantity), "IME-D0V", MyConn)
  
  
              oDeliveryNote.Lines.DiscountPercent = oSalesOrder.Lines.DiscountPercent
              oDeliveryNote.Lines.TaxCode = oSalesOrder.Lines.TaxCode
              oDeliveryNote.Lines.LineTotal = oSalesOrder.Lines.LineTotal
              oDeliveryNote.Lines.COGSCostingCode = oSalesOrder.Lines.COGSCostingCode
              oDeliveryNote.Lines.COGSCostingCode2 = oSalesOrder.Lines.COGSCostingCode2
              oDeliveryNote.Lines.COGSCostingCode3 = oSalesOrder.Lines.COGSCostingCode3
              oDeliveryNote.Lines.COGSCostingCode4 = oSalesOrder.Lines.COGSCostingCode4
              oDeliveryNote.Lines.COGSCostingCode5 = oSalesOrder.Lines.COGSCostingCode5
              oDeliveryNote.Lines.Rate = oSalesOrder.Lines.Rate
  
              oDeliveryNote.Lines.Add()
          Next
          lRetCode = oDeliveryNote.Add()
  
          If lRetCode &lt;&gt; 0 Then
              Dim sErr As String
              oCompany.GetLastError(lRetCode, sErr)
              UpdateSystemLog(lRetCode, 0, "Insert Delivery Error --&gt; " + sErr, "IME-D0A", MyConn)
          Else
              Dim sErr As String
              oCompany.GetLastError(lRetCode, sErr)
              UpdateSystemLog(lRetCode, 0, "Insert Delivery --&gt; " + sErr, "IME-D0B", MyConn)
          End If
  
  
      End If
  End Function
  </code></pre>