Search code examples
dynamics-crm-onlinedynamics-crm-2015

Unable to update custom entity record


I've run into a problem with CRM Online and a custom entity where in I am unable to update any records in the custom entity. In addition to being unable to update records I am also unable to delete records, which is not a big deal, but still a bit strange.

When I try to update a record I get the following exception:

The entity cannot be updated because it is read-only.

Initially I thought this was being because I wasn't querying the correct entity, however was able to confirm that I'm editing the correct entity as I am doing the following:

var record = new jol_custom_orders();
record.jol_estimated_ship_date = DateTime.Parse("12/15/2015");
record.jol_purchase_order_id = new EntityReference(SalesOrder.EntityLogicalName, order.Id);
record.jol_account = new EntityReference(Account.EntityLogicalName, CustomerId);
var id = Service.Create(record); 

When I get the id directly from crm and I try to update the same record this is where I run into my problem.

I am attempting to update the record doing the following:

var record = (jol_custom_orders)Service.Retrieve(jol_custom_orders.EntityLogicalName, id, new ColumnSet(true));
record.jol_estimated_ship_date = shipDate;
record.jol_estimated_ship_date = estimatedShipDate;
Service.Update(record);

So what exactly am I missing here as I'm really lost as to why this record is now read-only.

edit

I'm uncertain as to the why, but I created another custom entity with all the same fields as the one that's causing me problems and I'm able to to do all my crud operations. I'm a bit baffled by this as it makes no sense at all.


Solution

  • It sounds like you are having a similar issue to this poster: "EntityState must be set to null, Created (for Create message) or Changed (for Update message)" when attempting to update an entity in CRM 2011

    I find the best answer to actually be the one by user764754 with 5 upvotes (currently). Try out that approach of setting up a helper object and you should get past this issue (aka use following code)

    var newRecord = new jol_custom_orders(){
      Id = id,
      jol_estimated_ship_date = estimatedShipDate
    };
    Service.Update(newRecord);