Search code examples
dynamics-crmdynamics-crm-2011

Locking records returned by context? Or perhaps a change to my approach


I'm not sure whether I need a way to lock records returned by the context or simply need a new approach.

Here's the story. We currently have a small number of apps that integrate with our CRM. Some of them open a XrmServiceContext and return a few thousand record to perform updates. These scripts are calling SaveChanges along the way but there will still be accounts near the end that will be saved a couple of minutes after the context return them. If a user updates the record during this time, their changes are overwritten by the script.

Is there a way of locking the records until the context has saved the update back or is there a better approach I should be taking?

Kit


Solution

  • In my opinion, this type of database transaction issue is what CRM is currently lacking the most. There is no way to ensure that someone else doesn't monkey with your data, it's always a last-one-in-wins world in CRM.

    With that being said, my suggestion would be to only update the attributes you care about. If you're returning all columns for an entity, when you update that entity, you're possibly going to update all the attributes of the entity, even if you only updated one of them.

    If you're dealing with a system were you can't tolerate the last-one-in-wins mentality, then you're probably better off not using CRM.

    Update 1

    CRM 2015 SP1 and above supports Optimistic Updates. Which allows the use of a version number to ensure that no one has updated the record since you retrieved it.