Skip to Content
0
Oct 14, 2016 at 11:58 AM

Adding Goods Receipt via DI API sometimes fails -- what am I doing wrong?

2801 Views

My item master contains a couple of items for which I cannot reliably add a Goods Receipt via DI API in SAP Business One 9.0 PL9. The resulting error message is cryptic and doesn't seem applicable. I think there must be some nuance of creating a Goods Receipt that I don't understand. Here is code showing all the steps I take. What am I missing?

    public void AddGoodsReceipt(
      SAPbobsCOM.Company company,
      string receivedItemCode,
      string warehouseCode,
      double standardCost,
      double receivedQuantity,
      string referenceDoc
    ) {
      var grh = company.GetBusinessObject( SAPbobsCOM.BoObjectTypes.oInventoryGenEntry ) as SAPbobsCOM.Documents;

      grh.DocDate = DateTime.Today;
      grh.Reference1 = referenceDoc;
      grh.JournalMemo = "GR:" + referenceDoc;
      grh.Comments = "Goods Receipt: " + referenceDoc;

      var grl = grh.Lines;

      grl.ItemCode = receivedItemCode;
      grl.UnitPrice = standardCost;
      grl.Quantity = receivedQuantity;
      grl.WarehouseCode = warehouseCode;

      grl.Add();

      if ( grh.Add() != 0 ) {
        // Here we fail.
        // The error code is: -5011
        // The error message is: [IGN1.WhsCode][line:1], 'Invalid Account Code'
        // The receivedItemCode refers to an item whose GLMethod is "Item Group" (not Warehouse)
        // The same group is used for other item-group-managed items for which this code works!
        // In other words, there doesn't seem to be a difference in the configuration of items
        // which do work versus items which don't. So it makes me think some step is missing
        // in the code above.
      }
    }