Another enterprise capability has been added to the Dynamics CRM 2015 Online Update 1. Kudo’s the Dynamics CRM PG team. This is going to ease the Optimistic concurrency enablement in the product layer.
Prior to CRM online 2015 update 1, There were no concurrency lock checks out of the box and CRM always saves the value on the field level. The latest transaction will win (Overwrite). Developers might build some customization code to ensure the data integrity. However it requires lot of customization effort.
Provided in the latest SDK release is the ability to detect whether an entity record has changed on the server in the time between when your application retrieved the record and when it tries to update or delete that record.
The ConcurrencyBehavior Property specifies the type of optimistic concurrency behavior that should be performed by the Web service when processing this request:
Member name | Description |
AlwaysOverwrite | Specifies the behavior where an update or delete operation is applied without regard to the version of the record in the database. Value = 2.If optimistic concurrency is not enabled for the target entity, the AlwaysOverwrite behavior is applied. |
Default | Specifies to use the default behavior. Value = 0. The default value is also used if no value is set in the ConcurrencyBehavior property of the request. The meaning of “default” is interpreted differently based on the calling context. |
IfRowVersionMatches | Specifies the behavior where an update or delete operation is only applied if the entity record in the database has the same row version as the entity or entity reference in the request. Value = 1.If no row version value is provided on the entity or entity references in the request, the request fails immediately. |
The following sample code from SDK shows how to use optimistic concurrency for update and delete operations. The complete sample can be downloaded from MSDN: https://code.msdn.microsoft.com/Use-optimistic-concurrency-e0b0440d.
using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,serverConfig.Credentials, serverConfig.DeviceCredentials))
{
CreateRequiredRecords();
// Retrieve an account.
var account = _serviceProxy.Retrieve(“account”, _accountId, new ColumnSet(“name”,”creditlimit”));
Console.WriteLine(“\tThe row version of the created account is {0}”, account.RowVersion);
if (account != null)
{
// Create an in-memory account object from the retrieved account.
Entity updatedAccount = new Entity()
{
LogicalName = account.LogicalName,
Id = account.Id,
RowVersion = account.RowVersion
};
// Update just the credit limit.
updatedAccount[“creditlimit”] = new Money(1000000);
// Set the request’s concurrency behavour to check for a row version match
UpdateRequest accountUpdate = new UpdateRequest()
{
Target = updatedAccount,
ConcurrencyBehavior = ConcurrencyBehavior.IfRowVersionMatches
};
// Do the update.
UpdateResponse accountUpdateResponse = (UpdateResponse) _serviceProxy.Execute(accountUpdate);
Console.WriteLine(“Account ‘{0}’ updated with a credit limit of {1}.”, account[“name”],((Money)updatedAccount[“creditlimit”]).Value);
account = _serviceProxy.Retrieve(“account”, updatedAccount.Id, new ColumnSet());
Console.WriteLine(“\tThe row version of the updated account is {0}”, account.RowVersion);
_accountRowVersion = account.RowVersion;
}
DeleteRequiredRecords(promptforDelete);
}
Hopefully you find it useful!
Thank you,
Zhe Chen
Pingback: Optimistic concurrency in CRM Online 2015 Update 1 | Dinesh Ram Kali.