Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a CRM 2011 Entity using LINQ in a Plugin?

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();
like image 292
Chad Avatar asked Feb 22 '11 06:02

Chad


1 Answers

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.

like image 198
Josh Painter Avatar answered Oct 27 '22 05:10

Josh Painter