If a bundle is selected as an existing product, you can't change it to another value.

Question Status

Verified
slx asked a question on 4 Aug 2016 11:17 AM

Hi

I am trying to update a custom field on a bundle record for when order product is created (salesorderdetail).

Condition:

postImageEntity.GetAttributeValue<OptionSetValue>("producttypecode").Value == 2

{

//code

}

This condition should only allow update for bundle, I ignore the other values:

1 = product

3,4 = require and optional product bundle

Same code works for producttypecode = 1

I don't touch the product bundle items which are read only, why this error ???

Reply
slx responded on 5 Aug 2016 6:37 AM

I tried different events - create / update and got the same error

Any help ?

Reply
Verified Answer
slx responded on 5 Aug 2016 8:53 AM

I got the answer:

                       postImageEntity.Attributes.Remove("productid");

                       postImageEntity.Attributes.Remove("producttypecode");

Reply
Ruzel responded on 4 Jun 2017 10:51 PM

I'm having the same issue, how did you manage to make it work? Below is my sample code, where I only update the line item from quotedetail entity where that quotedetail is equivalent to the productbundle item itself.

 private void UpdateQuoteProducts()
        {
            var quote = XrmContext.CreateQuery("quote").First(q => q.GetAttributeValue<Guid>("quoteid") == Purchasable.Quote.Id);

            var quoteItems = XrmContext.CreateQuery("quotedetail")
                .Where(q => q.GetAttributeValue<EntityReference>("quoteid") == quote.ToEntityReference() && q["parentbundleid"] == null)
                .ToArray();
            decimal total = 0;
            decimal tax = 0;

            foreach (var qitem in quoteItems)
            {
                var pricePerUnit = qitem.GetAttributeValue<Money>("priceperunit").Value;
                var baseAmount = qitem.GetAttributeValue<Money>("baseamount").Value;
                qitem.SetAttributeValue("ispriceoverridden", true);
                qitem.SetAttributeValue("priceperunit", new Money(pricePerUnit));
                qitem.SetAttributeValue("baseamount", new Money(baseAmount));
                total = total + baseAmount;
                EntityReference qItemProduct = (EntityReference)qitem.Attributes["productid"];
                var actualProduct = ServiceContext.CreateQuery("product").FirstOrDefault(p => p.GetAttributeValue<Guid>("productid") == qItemProduct.Id);
                EntityReference qItemCurrency = (EntityReference)actualProduct.Attributes["transactioncurrencyid"];
              
                qitem.SetAttributeValue("tax", new Money(baseAmount * 0.10m));
                tax = tax + baseAmount * 0.10m;
              
                XrmContext.UpdateObject(qitem);
            }
            XrmContext.SaveChanges();
}


 

Reply
slx responded on 5 Jun 2017 3:30 AM

Can you send the error?

I remember, for some reason which I didn't investigate, update didn't work when productid and producttypecode were part of the query containing bundles.

1) On the code above I would just try to remove

- producttypecode

- productid

from quoteitem

before the update:

               XrmContext.UpdateObject(qitem);

2) Or make sure they are not taken into the object from initial query, select only the fields you need to make the update on CreateQuery

Hope that helps

Reply
Ruzel responded on 14 Jun 2017 12:14 AM

Hi slx, thanks for the reply.

It is now working, we just removed the attribute productid from the object before updating like you have suggested above.

Reply
Yasser Abu Bakr responded on 10 Jul 2017 5:43 AM

Thank you so much, that resolve my issue as well

Reply
Verified Answer
slx responded on 5 Aug 2016 8:53 AM

I got the answer:

                       postImageEntity.Attributes.Remove("productid");

                       postImageEntity.Attributes.Remove("producttypecode");

Reply