Search code examples
sapb1sap-business-one-di-api

SAP B1: Getting an error when trying to create an AP Credit Memo - 540020003 - Missing document type


I am trying to create an A/P Credit Memo as a copy of an A/P Invoice. I am doing it manually, but copying values, instead of point the Credit Memo back to the base document.

The pertinent bits are all in this method:

private int CreateTarget(IDocuments sourceDoc, IDocuments targetDoc, int docEntry)
{
    targetDoc.DocDueDate = DateTime.Today;
    targetDoc.DocDate = DateTime.Today;
    targetDoc.DocType = sourceDoc.DocType;
    targetDoc.CardCode = sourceDoc.CardCode;
    targetDoc.ContactPersonCode = sourceDoc.ContactPersonCode;
    targetDoc.NumAtCard = sourceDoc.NumAtCard;
    targetDoc.SalesPersonCode = sourceDoc.SalesPersonCode;
    targetDoc.Comments = sourceDoc.Comments;
    targetDoc.ShipToCode = sourceDoc.ShipToCode;
    targetDoc.PayToCode = sourceDoc.PayToCode;
    targetDoc.Address = sourceDoc.Address;
    targetDoc.Address2 = sourceDoc.Address2;

    using (var sLines = sourceDoc.Lines.WithComCleanup())
    {
        using (var tLines = targetDoc.Lines.WithComCleanup())
        {
            var sourceLines = sLines.Resource;
            var targetLines = tLines.Resource;

            for (var idx = 0; idx < sourceLines.Count; idx++)
            {
                if (idx != 0)
                {
                    targetLines.Add();
                }

                targetLines.ItemCode = sourceLines.ItemCode;
                targetLines.Quantity = sourceLines.Quantity;
                targetLines.UnitPrice = sourceLines.UnitPrice;
                targetLines.DiscountPercent = sourceLines.DiscountPercent;
            }
        }
    }

    using (var docRefCom = targetDoc.DocumentReferences.WithComCleanup())
    {
        var docRef = docRefCom.Resource;
        docRef.Add();
        docRef.ReferencedObjectType = GetReferencedObjectType(false);
        docRef.ReferencedDocEntry = docEntry;
    }

    if (targetDoc.Add() != 0)
    {
        var (code, message) = _sboWrapper.GetLastError();
        throw new SapB1Exception(code, message);
    }

    return _sboWrapper.GetNewObjectKey();
}

When I reach the targetDoc.Add() call I get the following error from the DI API: Code: -10 Message: 540020003 - Missing document type

I cannot figure out, what this means. I have tried adding various properties without much luck.

My source document is declared like this: (SAPbobsCOM.Documents)Company.GetBusinessObject(BoObjectTypes.oPurchaseInvoices).GetByKey(docEntry);

And my target document like this: (SAPbobsCOM.Documents)Company.GetBusinessObject(BoObjectTypes.oPurchaseCreditNotes);


Solution

  • Turned out it was the docRef.Add() that was the culprit. In retrospect it makes perfect sense, but the Documents_DocumentReferences object does not have a Count property, and next to no documentation, so I have the feeling it is fairly new in the DI Api.

    Since it lacked the usual trappings of a collection object, likes Documents_Lines, I convinced myself that I needed to call Add() to do anything.

    Anyway, problem solved :)