Wednesday 8 June 2011

c# plugin update error The product and the unit cannot be updated while the entity is locked.

It appears that if you have an order(sales order) that has its prices locked (done by clicking ‘Lock Pricing’ or converting the order to an invoice), you CANT update any field on it through a plugin. You can through the form though, but with the plugin you get an error message:

The product and the unit cannot be updated while the entity is locked.

The solution here would be to remove the references to those fields before doing the update. e.g.


bool check = (bool)job.Attributes["salesorderispricelocked"];
                                    if (check)
                                    {
                                        if (job.Attributes.Contains("productid"))
                                        {
                                            job.Attributes.Remove("productid");
                                        }
                                        if (job.Attributes.Contains("uomid"))
                                        {
                                            job.Attributes.Remove("uomid");
                                        }
                                        if (job.Attributes.Contains("priceperunit_base"))
                                        {
                                            job.Attributes.Remove("priceperunit_base");
                                        }
                                        if (job.Attributes.Contains("priceperunit"))
                                        {
                                            job.Attributes.Remove("priceperunit");
                                        }
                                        service.Update(job);
                                    }
                                    else
                                    {
                                        service.Update(job);
                                    }


This would be the same for the invoice entity too