Marqueone
Marqueone

Reputation: 1213

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.

Upvotes: 0

Views: 425

Answers (2)

Daryl
Daryl

Reputation: 18895

Generally when you can't perform a standard CRM CRUD operation on an entity, the issue is caused by a synchronous plugin or synchronous workflow, causing an exception and causing the transaction to get rolled back. I'd double check any custom plugins you have registered, and and synchronous workflows you have as well, to see if anything triggers on your custom entity that could be causing an exception and rolling back the transaction.

Upvotes: 0

Joseph Duty
Joseph Duty

Reputation: 795

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);

Upvotes: 1

Related Questions