Search code examples
c#arraysweb-servicesdynamics-crm-2011dynamics-gp

Passing multiple line items with GP webservice


Below is the code I'm working with to pass multiple line items to create sales order through GP Web service. I can pass single Line Item without any problem , but when I pass multiple Items it is only taking the last one. The array has around 5 Item ID and I'm passing fixed Quantity as 15, Need to make both dynamic. But for the testing purpose I'm trying like this. I know the problem with the creation/initialization of some web service objects. As novice to the entire set of things I couldn't find the exact problem.

C# Code

            CompanyKey companyKey;
            Context context;
            SalesOrder salesOrder;
            SalesDocumentTypeKey salesOrderType;
            CustomerKey customerKey;
            BatchKey batchKey;
           // SalesOrderLine salesOrderLine;
            ItemKey orderedItem;
            Quantity orderedAmount;
            Policy salesOrderCreatePolicy;
            DynamicsGPClient wsDynamicsGP = new DynamicsGPClient();
            wsDynamicsGP.ClientCredentials.Windows.ClientCredential.UserName = "Admin";
            wsDynamicsGP.ClientCredentials.Windows.ClientCredential.Password = "pass";
            wsDynamicsGP.ClientCredentials.Windows.ClientCredential.Domain = "Gp";
            System.ServiceModel.WSHttpBinding binding;
            binding = new System.ServiceModel.WSHttpBinding(System.ServiceModel.SecurityMode.None);
            context = new Context();
            companyKey = new CompanyKey();
            companyKey.Id = (1);
            context.OrganizationKey = (OrganizationKey)companyKey;
            salesOrder = new SalesOrder();
            salesOrderType = new SalesDocumentTypeKey();
            salesOrderType.Type = SalesDocumentType.Order;
            salesOrder.DocumentTypeKey = salesOrderType;
            customerKey = new CustomerKey();
            customerKey.Id = "121001";
            salesOrder.CustomerKey = customerKey;
            batchKey = new BatchKey();
            batchKey.Id = "RMS";
            salesOrder.BatchKey = batchKey;
           // SalesOrderLine[] orders = new SalesOrderLine[6];

            SalesOrderLine[] lines = { };

            for (int i = 1; i < 5; i++)
            {
                SalesOrderLine salesOrderLine = new SalesOrderLine();
                orderedItem = new ItemKey();
                orderedItem.Id = Arr[i].ToString();
                salesOrderLine.ItemKey = orderedItem;
                orderedAmount = new Quantity();
                orderedAmount.Value = 15;
                salesOrderLine.Quantity = orderedAmount;
                lines = new SalesOrderLine[] { salesOrderLine };
                MessageBox.Show(lines.Count().ToString());
            }
            salesOrder.Lines = lines;
            //salesOrder.Lines = orders;
            salesOrderCreatePolicy = wsDynamicsGP.GetPolicyByOperation("CreateSalesOrder", context);
            wsDynamicsGP.CreateSalesOrder(salesOrder, context, salesOrderCreatePolicy);
            if (wsDynamicsGP.State != CommunicationState.Faulted)
            {
                wsDynamicsGP.Close();
            }

            MessageBox.Show("Success");

Solution

  • lines = new SalesOrderLine[] { salesOrderLine }; will recreate the lines array object each time meaning you loose any previously added objects. Effectively only the final object in the loop is actually added.

    Try using a List<T> like so:

    SalesOrderLine[] lines = { }; Becomes List<SalesOrderLine> lines = new List<SalesOrderLine>();

    lines = new SalesOrderLine[] { salesOrderLine }; Becomes: lines.Add(salesOrderLine);

    If its important you end up with an array as the input:

    salesOrder.Lines = lines; Becomes: salesOrder.Lines = lines.ToArray();