Skip to Content
0
Former Member
Sep 22, 2008 at 01:40 AM

Unable to add order to B1. Please help.

57 Views

Hi,

I am trying to create DIAPI code to add an order to the system, and I keep running into an error,

which is error code 1, and error string 'Unable to commit transaction'.

When I try to use the sample code from SAP (project called "order" in C:\Program Files\SAP\SAP

Business One SDK\Samples\COM DI\CSharp\05.order), it fails with the same response.

This is the code I am using (although please remember that SAP code returns the same error).

    public class oOrder
    {
        public int DocNum { get; set; }
        public string CardCode { get; set; }
        public string CardName { get; set; }
        public DateTime DocDate { get; set; }
        public DateTime DocDueDate { get; set; }
        public ArrayList DocumentLines { get; set; }

        public oOrder()
        {
            this.DocumentLines = new ArrayList();
        }
    }

    public class oOrderLineItem
    {
        public string ItemCode { get; set; }
        public string ItemDescription { get; set; }
        public double Quantity { get; set; }
    }

From the calling code:

            // Create an order object
            oOrder oOrder = new oOrder();

            // Populate order object with values
            oOrder.DocNum = 79054;
            oOrder.CardCode = "SAF";
            oOrder.CardName = "SAFEWAY";
            oOrder.DocDate = DateTime.Now;
            oOrder.DocDueDate = DateTime.Now.AddDays(4);

            // Create line item 1, populate with values
            oOrderLineItem oOrderLineItem1 = new oOrderLineItem();
            oOrderLineItem1.ItemCode = "HOB";
            oOrderLineItem1.ItemDescription = "HONEY BUNCH GOLDEN GRAPE 12 PINT";
            oOrderLineItem1.Quantity = 1;

            // Create line item 2, populate with values
            oOrderLineItem oOrderLineItem2 = new oOrderLineItem();
            oOrderLineItem2.ItemCode = "YPR";
            oOrderLineItem2.ItemDescription = "YELLOW PEAR 12 HALF PINT";
            oOrderLineItem2.Quantity = 2;

            // Add line items to the order object
            oOrder.DocumentLines.Add(oOrderLineItem1);
            oOrder.DocumentLines.Add(oOrderLineItem2);

            int lRetCode = di.AddOrderToSAP(oOrder);

The code that attempts to add the order to B1"

        public int  AddOrderToSAP(oOrder oOrder)
        {
            sapCompany = new Company();

            //  Set connection properties
            sapCompany.Server = databaseServer;
            sapCompany.CompanyDB = databaseName;
            sapCompany.LicenseServer = diServerLicenseServer;
            sapCompany.DbUserName = databaseUserName;
            sapCompany.DbPassword = databasePassword;
            sapCompany.language = SAPbobsCOM.BoSuppLangs.ln_English;
            sapCompany.UserName = companyUserName;
            sapCompany.Password = companyPassword;
            sapCompany.UseTrusted = true;

            // Try to connect
            lRetCode = sapCompany.Connect();


            sapOrder = ((SAPbobsCOM.Documents)(sapCompany.GetBusinessObject
(SAPbobsCOM.BoObjectTypes.oOrders))); 

            sapOrder.DocNum = oOrder.DocNum;
            sapOrder.CardCode = oOrder.CardCode;
            sapOrder.CardName = oOrder.CardName;
            sapOrder.DocDate = oOrder.DocDate;
            sapOrder.DocDueDate = oOrder.DocDueDate;

            bool firstLineItemAdded = false;
            
            foreach (oOrderLineItem oOrderLineItem in oOrder.DocumentLines)
            {
                if (firstLineItemAdded)
                {
                    sapOrder.Lines.Add();
                }

                sapOrder.Lines.ItemCode = oOrderLineItem.ItemCode;
                sapOrder.Lines.ItemDescription = oOrderLineItem.ItemDescription;
                sapOrder.Lines.Quantity = oOrderLineItem.Quantity;

                if (!firstLineItemAdded)
                {
                    firstLineItemAdded = true;
                }
            }

            lRetCode = sapOrder.Add(); //  Try to add the orer to the database

            sapCompany.Disconnect();

            if (lRetCode != 0)
            {
                sapCompany.GetLastError(out lErrCode, out sErrMsg);
            }

            return lRetCode;
        }

If you have any ideas about why the system isn't accepting an order from this code, or from the

standard sample code provided by SAP, please let me know.

Thank you,

Mike