New CRM SDK Feature - Optimistic Concurrency

19 April 2015
Daniel Cai

In your Microsoft Dynamics CRM environment, data changes could happen to the same record simultaneously due to the operations initiated by different system users or applications at the same time. Such concurrency situation could sometimes result in data loss. With the introduction of Optimistic Concurrency feature in CRM Online 2015 Update 1 release, we now have a way to detect whether the record has been changed since the last time it was retrieved, which can help mitigate such potential data loss.

The Optimistic Concurrency feature is made possible by the ConcurrencyBehavior option of UpdateRequest and DeleteRequest message.

The following snippet shows how the behavior is used in the scenario when you need to perform an update of CRM record.

using (var service = new OrganizationService(crmConnection))
{
    var account = service.Retrieve("account", accountId, new ColumnSet("name", "revenue", "creditlimit"));

    // Here you might be doing something that takes a lot time in which case that the CRM record could be modified by other application or system user.

    object revenue;
    if (account != null && account.Attributes.TryGetValue("revenue", out revenue) && ((Money)revenue).Value > 50000000)
    {
        var updatedAccount = new Entity()
        {
            LogicalName = account.LogicalName,
            Id = account.Id,
            RowVersion = account.RowVersion
        };

        updatedAccount["creditlimit"] = new Money(5000);

        // Define the update behavior so it only happens when RowVersion matches
        var accountUpdate = new UpdateRequest()
        {
            Target = updatedAccount,
            ConcurrencyBehavior = ConcurrencyBehavior.IfRowVersionMatches
        };

        // Make update service call
        try
        {
            var accountUpdateResponse = (UpdateResponse)service.Execute(accountUpdate);
        }
        catch (FaultException<OrganizationServiceFault> ex)
        {
            if (ex.Code == OPTIMISTIC_CONCURRENCY_VIOLATION)
            {
                // TODO: Handle the exception
            }
        }
    }
}

ConcurrencyBehavior option is also available for DeleteRequest message, which provides a similar logic. Delete will fail if the record has been changed by another thread since it was retrieved. The following snippet demonstrates the code pattern that you can use.

using (var service = new OrganizationService(crmConnection))
{
    var account = service.Retrieve("account", accountId, new ColumnSet("name", "revenue", "creditlimit"));

    // Here you might be doing something that takes a lot time in which case that the CRM record could be modified by other application or system user.

    var accountToDelete = new EntityReference()
    {
        LogicalName = account.LogicalName,
        Id = account.Id,
        RowVersion = account.RowVersion
    };
 
    var accountDelete = new DeleteRequest()
    {
        Target = accountToDelete,
        ConcurrencyBehavior = ConcurrencyBehavior.IfRowVersionMatches
    };
 
    try
    {
        var accountDeleteResponse = service.Execute<DeleteResponse>(accountDelete);
    }
    catch (FaultException<OrganizationServiceFault> ex)
    {
        if (ex.Code == OPTIMISTIC_CONCURRENCY_VIOLATION)
        {
            // TODO: Handle the exception
        }
    }
}

For more details about this feature, please check out CRM SDK online documentation page: Reduce potential data loss using optimistic concurrency.

Hope this has helped.

Cheers,
Daniel Cai | KingswaySoft

Read the full New SDK Capabilities Blog Series:

Archive

November 2024 3 October 2024 1 September 2024 1 August 2024 2 July 2024 1 June 2024 1 May 2024 1 April 2024 2 March 2024 2 February 2024 2 January 2024 2 December 2023 1 November 2023 1 October 2023 2 August 2023 1 July 2023 2 June 2023 1 May 2023 2 April 2023 1 March 2023 1 February 2023 1 January 2023 2 December 2022 1 November 2022 2 October 2022 2 September 2022 2 August 2022 2 July 2022 3 June 2022 2 May 2022 2 April 2022 3 March 2022 2 February 2022 1 January 2022 2 December 2021 1 October 2021 1 September 2021 2 August 2021 2 July 2021 2 June 2021 1 May 2021 1 April 2021 2 March 2021 2 February 2021 2 January 2021 2 December 2020 2 November 2020 4 October 2020 1 September 2020 3 August 2020 2 July 2020 1 June 2020 2 May 2020 1 April 2020 1 March 2020 1 February 2020 1 January 2020 1 December 2019 1 November 2019 1 October 2019 1 May 2019 1 February 2019 1 December 2018 2 November 2018 1 October 2018 4 September 2018 1 August 2018 1 July 2018 1 June 2018 3 April 2018 3 March 2018 3 February 2018 3 January 2018 2 December 2017 1 April 2017 1 March 2017 7 December 2016 1 November 2016 2 October 2016 1 September 2016 4 August 2016 1 June 2016 1 May 2016 3 April 2016 1 August 2015 1 April 2015 10 August 2014 1 July 2014 1 June 2014 2 May 2014 2 February 2014 1 January 2014 2 October 2013 1 September 2013 2 August 2013 2 June 2013 5 May 2013 2 March 2013 1 February 2013 1 January 2013 1 December 2012 2 November 2012 2 September 2012 2 July 2012 1 May 2012 3 April 2012 2 March 2012 2 January 2012 1

Tags