locked
CRM 2011: Plugin and XRMServiceContext RRS feed

  • Question

  • I currently have a plugin that triggers anytime a user creates, updates, or deletes a record out of a custom entity named Conflict of Interest. The plugin will update a flag on the contact record that lets a user know if there is an active conflict of interest. When I try to run the linq update I get an error "The context is not currently tracking the 'contact' entity".

    This sets up the IOrganizationService:

    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
    This function creates the linq query to update the contact record
    
    public void SetActor_ConflictOfInterest(IOrganizationService service, Guid ActorGUID)
        {
          Utilities util = new Utilities();
          Guid BillingHeader = Guid.Empty;
          var xrm = new XrmServiceContext(service);
    
    
          var Actor = new Xrm.Contact
          {
            ContactId = ActorGUID,
            new_ConflictofInterest = true,
          };
          xrm.UpdateObject(Actor);
          xrm.SaveChanges();
        }
    

    I've tried updating the record with a sql query as well, but CRM is locking the contact record for some reason. Any ideas on the linq error and why CRM is locking the contact record?

     

    Wednesday, May 18, 2011 9:52 PM

Answers

  • The short answer is add the .Attach() method call to your code, thus:

    xrm.Attach(Actor);
    xrm.UpdateObject(Actor);
    xrm.SaveChanges(); 
    
    I'm planning on blogging shortly with the long version when I find the time.

     


    --pogo (pat) @ pogo69.wordpress.com
    • Marked as answer by Ben Cook Thursday, May 19, 2011 8:29 PM
    Thursday, May 19, 2011 12:42 AM

All replies