locked
CRM2011: Trouble creating related entities on pre-event plugin RRS feed

  • Question

  • Hi

    Because we have large volumes of sales orders that need to be created as quick as possible and we also have a small number of products, what i have done is the following:

    I have created some custom fields for the products on the salesorder entity so that the user simply puts the quanitity in that field when creating a salesorder - as opposed to first saving, then going to order products and adding them.. (long process).

    Anyways, i have tested my code in a standard app using web services and it works fine as per below:

    Dim neworder As New Entity(SalesOrder.EntityLogicalName)
            neworder.Attributes.Add("customerid", New EntityReference(Account.EntityLogicalName, CustomerGUID)))
    
            Dim ProductOne As New Entity(SalesOrderDetail.EntityLogicalName)
            ProductOne.Attributes.Add("ispriceoverridden", True)
            ProductOne.Attributes.Add("isproductoverridden", False)
            ProductOne.Attributes.Add("productid", New EntityReference(Product.EntityLogicalName, ProdyctGUID)))
            ProductOne.Attributes.Add("priceperunit", New Money(4.5D))
            ProductOne.Attributes.Add("quantity", 120D)
            ProductOne.Attributes.Add("uomid", New EntityReference(UoM.EntityLogicalName, UoMGUID)))
    
    
            Dim ProductTwo As New Entity(SalesOrderDetail.EntityLogicalName)
            ProductTwo.Attributes.Add("ispriceoverridden", True)
            ProductTwo.Attributes.Add("productid", New EntityReference(Product.EntityLogicalName, ProductGUID)))
            ProductTwo.Attributes.Add("priceperunit", New Money(1D))
            ProductTwo.Attributes.Add("quantity", 1D)
            ProductTwo.Attributes.Add("productdescription", "Delivery fee")
    
            Dim orderitemscol As New EntityCollection
            orderitemscol.EntityName = SalesOrderDetail.EntityLogicalName
            orderitemscol.Entities.Add(ProductOne)
            orderitemscol.Entities.Add(ProductTwo)
            neworder.Attributes.Add("pricelevelid", New EntityReference(PriceLevel.EntityLogicalName, PricelistGUID)))
    
            neworder.RelatedEntities.Item(New Relationship("order_details")) = orderitemscol
    
            Console.WriteLine(ICEProx.Create(neworder))
    

    However, when i use the code in a plugin and register it in a pre-event stage, I get no errors but the parent-child related entities do NOT get created.

    I then tried to take the entity collection and store it into the "SharedVariables" on the context and created a seperate plugin registered on the post-event, and tried to update the entity by attaching ordered items there. Still no luck.

    I guess i will have to perhaps try and update each order item with the parent order ID and update them seperately thru service.

    I was hoping to keep it clean and lean by allowing the platform to automatically link the child entities on the main Create event.  I guess this funtionality is not supported.


    Rish

    Saturday, June 22, 2013 11:44 AM

All replies

  • Using RelatedEntities would only wokr if your code were creating both the salesorder and the salesorderdetail records. However, the salesorder is created by the user, so this approach won't work.

    The simplest solution would be to use a post-event and create each salesorderdetail record in turn. You could bundle these up into an ExecuteMultiple request, but there's no real benefit to this.


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Tuesday, June 25, 2013 2:20 PM
    Moderator