11

We are able to create new entities without any issues, but updating an existing entity in a plugin this does not appear to be working. This is for CRM 2011.

var crmContext = new CustomCrmContext(service);

var contact = crmContext.Contact.FirstOrDefault(c=>c.Id == targetEntity.Id);

contact.new_CustomField = "Updated";

crmContext.SaveChanges();
Guido Preite
  • 14,905
  • 4
  • 36
  • 65
Chad
  • 489
  • 1
  • 6
  • 16

3 Answers3

15

No need to download the whole Contact record if you already have the Id and you just need to update a field or two. You also don't need the OrganizationServiceContext - just the Service. Try something like:

var c = new contact() {
  Id = targetEntity.Id,
  new_CustomField = "Updated"
}

service.Update(c);

This will save the roundtrip of querying for the contact first.

Guido Preite
  • 14,905
  • 4
  • 36
  • 65
Josh Painter
  • 4,071
  • 21
  • 26
  • If you have previously fetched the entity from a OrganizationServiceContext, and you then query for it again. Won't it be already be cached in the context, i.e. no roundtrip will be performed? – Cartaya Jun 09 '16 at 14:13
14

You have to mark the object as modified in order to get it submitted to the server. See OrganizationServiceContext.UpdateObject (Entity)

You should add crmContext.UpdateObject(contact); before crmContext.SaveChanges();

ccellar
  • 10,326
  • 2
  • 38
  • 56
  • Unexpected exception from plug-in (Execute): Microsoft.Xrm.Sdk.SaveChangesException: An error occured while processing this request. – Chad Feb 22 '11 at 15:00
  • 1
    Above is what I get when I do an UpdateObject. Again, I can add a new record without any issue. It is just updating an existing object that doesn't seem to be working. Any ideas? – Chad Feb 22 '11 at 15:01
  • Can you please describe how you registered your plugin? Is it registered for update of contact? The SaveChangesException has a property "Results" - which items are included? – ccellar Mar 30 '11 at 15:36
  • 1
    I had the same problem. After I added context.UpdateObject(entity) before context.SaveChanges() it worked. – nang May 19 '11 at 09:59
  • I have the same results that @Chad, any suggestions? – Marco Medrano Nov 28 '12 at 00:48
  • @MarcoMedrano check the Exception. Is there any other plugin which is triggered or is the same plugin triggered through the update – ccellar Dec 06 '12 at 10:57
  • Hi @ccellar, In my case it does not do anything, I mean I call UpdateObject and also SaveChanges, but plainly does not do anything. – Marco Medrano Dec 07 '12 at 00:17
  • @MarcoMedrano maybe it would be better if you could create a new question and post some code – ccellar Dec 11 '12 at 11:08
  • @ccellar you right, I would include even the Plugin messages that I am using to replicate the issue. Thanks :) – Marco Medrano Dec 11 '12 at 14:52
1

LINQ is fine, just create the new object or list and loop the list in the linq and update:

using (var crm = new XrmServiceContext(service)){
var foo = crm.nmipcs_productpriceitemSet
    .Where(ppis => ppis.nmipcs_Account.Id == account.Id).ToList();

foreach (var nmipcsProductpriceitem in foo){
    var f = new nmipcs_productpriceitem
    {
    Id = nmipcsProductpriceitem.Id                 
    ,
    nmipcs_PriceSalesChannel = (decimal) 9.99
    };

    service.Update(f);
}
    }
Guido Preite
  • 14,905
  • 4
  • 36
  • 65
psven
  • 11
  • 2