Access CRM Fields in an Efficient Way

31 March 2017
Daniel Cai

As a professional Dynamics 365/CRM developer, you want your code to be as efficient as possible. In your CRM project, you might often run into a code structure like something below.

if (entity.Attributes.Contains("myorg_myfield"))
{
    var fieldValue = entity.Attributes["myorg_myfield"];
    // Handle it based on there is a value with the field.
}
else
{
    // Handle it based on there is no value with the field.
}

You can find this particular code pattern almost everywhere, even including the sample code that is shipped with Dynamics 365/CRM SDK. However, there is a problem with this code structure, it is simply not as efficient as it can be!

Let me explain to you why.

CRM entity's Attributes property is essentially a property bag which contains all field values using an internal .NET dictionary. The dictionary uses field name as the key. What the above code does is, it first performs a lookup to check if the entity record contains a value for the field called myorg_myfield. When it determines that there is a value with the concerned field, it will try to perform another lookup of the dictionary to actually get the value for the field. The problem is, the code structure is essentially doing two lookup operations against the same internal dictionary. This should not be the way that you access information in a .NET dictionary, as there is a better way to do it!

Let's have a close look of the option available. .NET dictionary object has a TryGetValue method, which can be used to get value from the dictionary using a key. If it finds the key in the dictionary, the method returns a value of true, at the same time it return the value for the key in its only "out" parameter. If doesn't find one, the method's return value will be false indicating no such key exists in the dictionary, and the "out" parameter variable will have a null value. Using the TryGetValue method, your code would be something like the following.

object fieldValue;
if (entity.Attributes.TryGetValue("myorg_myfield", out fieldValue))
{
    // Handle it based on there is a value with the field.
}
else
{
    // Handle it based on there is no value with the field.
}

Using the new code structure, we are now only performing one lookup, and we know whether the CRM entity property bag contains a value for a particular field, and we also have the value with one single method call. Doing so, you have a performance improvement of 100% (in theory though), your code runs two times faster.

This may not be such a big issue if you are only accessing just one or even a few fields. But if your code runs within a loop structure or if you try to access a lot more fields, the performance difference could add up quickly so it may become significant enough.

I shared this in a CRMUG summit session previously, this is the first time I put the idea down in writing. I hope this is helpful, happy CRM (or 365) coding!

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