locked
Adding salesorderdetail using silverlight RRS feed

  • Question

  • I am trying to add salesorderdetails to an existing sales order using silver light and the following code
    Context.SalesOrderDetail detal = new Context.SalesOrderDetail();
                detal.ProductId = new Context.EntityReference();
                detal.ProductId.Id = product.ProductId;
                detal.PricePerUnit = new Context.Money();
                detal.PricePerUnit.Value = product.Price_Base.Value;
                           detal.Quantity = 1;
                detal.SalesOrderId = new Context.EntityReference();
                detal.SalesOrderId.Id = salesorder;
                detal.Tax = new Context.Money();
                detal.Tax.Value = 0;
                if (user != null)
                {
                    detal.SalesRepId = new Context.EntityReference();
                    detal.SalesRepId.Id = user;
                }
                detal.ExtendedAmount = new Context.Money();
                detal.ExtendedAmount.Value = product.Price_Base.Value;
                detal.UoMId = new Context.EntityReference();
                detal.UoMId.Name = "default";
                
                context.AddToSalesOrderDetailSet(detal);
                context.BeginSaveChanges(Submitcallback, detal);

    however this does not work and gives me a "missing parent id" error. I am sure there is some property that I am not setting but I cannot figure out what it can be. Any help would be appreciated.
    Wednesday, June 26, 2013 2:55 PM

Answers

  • I checked the code again and I noticed this part:

    detal.UoMId = new Context.EntityReference();
    detal.UoMId.Name = "default";

    also for UoMId you must to set the Id property of the EntityReference, Name is not enough (and you can also skip to assign)


    My blog: www.crmanswers.net

    • Proposed as answer by Guido PreiteMVP Wednesday, June 26, 2013 4:27 PM
    • Unproposed as answer by Thomastx06 Wednesday, June 26, 2013 5:01 PM
    • Marked as answer by Thomastx06 Thursday, June 27, 2013 2:56 AM
    Wednesday, June 26, 2013 4:27 PM

All replies

  • the variable salesorder contains the Guid of the SalesOrder?

    My blog: www.crmanswers.net

    Wednesday, June 26, 2013 3:54 PM
  • Yes Guido, SalesOrder does contain that Guid of the parent sales order.
    Wednesday, June 26, 2013 4:18 PM
  • I checked the code again and I noticed this part:

    detal.UoMId = new Context.EntityReference();
    detal.UoMId.Name = "default";

    also for UoMId you must to set the Id property of the EntityReference, Name is not enough (and you can also skip to assign)


    My blog: www.crmanswers.net

    • Proposed as answer by Guido PreiteMVP Wednesday, June 26, 2013 4:27 PM
    • Unproposed as answer by Thomastx06 Wednesday, June 26, 2013 5:01 PM
    • Marked as answer by Thomastx06 Thursday, June 27, 2013 2:56 AM
    Wednesday, June 26, 2013 4:27 PM
  • Made then change but still no dice, I get the same error.

    It seems that UoMId would need to be there sice it is a required field on the form. I will try adding that ones id and see if I get the same error.

    Wednesday, June 26, 2013 4:36 PM
  • I have added the correct UomId now and still getting the "missing parent id " error. I thought I might post the async callback as well to see if that may help

      private void Submitcallback(IAsyncResult result)
            {
                try
                {
                    
                    context.EndSaveChanges(result);
                    Context.SalesOrderDetail det = result.AsyncState as Context.SalesOrderDetail;
                    getproducts();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.InnerException.Message);
                }
            }

    .

    Wednesday, June 26, 2013 4:45 PM
  • Ok so here is the odd part.... It is actually adding the salesorderdetail but it is still giving that error on the callback.
    Wednesday, June 26, 2013 5:01 PM
  • Alright so Guido was right on the money the reason this was not working is that I had a control bound to a dataservicecollection which by default it tracked. So when I called the savechanges that collection was posting as well and that is where the parenteid was missing. Also for any one else reading this for me the Uom was required to post.
    Thursday, June 27, 2013 2:56 AM