New CRM SDK Feature - No more Special Messages for Special Fields

17 April 2015
Daniel Cai

Microsoft Dynamics CRM uses a number of special fields for special purpose, those fields used to require using special SOAP messages in order to update them.

For instance, almost every CRM entity has statecode and statuscode fields which represent the status of CRM records. In order to update a CRM record's status, you would have to use SetStateRequest message. Another example is the ownerid field which is available to all entities that are setup as a user-owned entity. In order to update ownerid field, you would have to use AssignRequest. The list goes on including similar fields such as:

This has made writing data integration code unpleasantly difficult, your integration solution will break if you don't handle those fields specifically. This has also caused some performance issues. In a data migration/integration project, it is very common that you have to make three service calls to make an update to one CRM record, which include the first service call to update all regular fields of the CRM record, a second one to assign the ownership using AssignRequest, and a third one to update the record's status using SetStateRequest. The following snippet shows the typical code pattern that you would use when you need to update a CRM record.

using (var service = new OrganizationService(crmConnection))
{
    Entity account = new Entity("account");
    account["accountid"] = new Guid("0C2D5AC7-B7E4-E411-80E9-C4346BAC7DA8");
    account["name"] = "Adventure Works Inc.";
    account["creditlimit"] = new Money(100000);

    // Service call 1 - Update all regular fields
    var updateRequest = new UpdateRequest() { Target = account };
    var updateResponse = (UpdateResponse)service.Execute(updateRequest);

    // Service call 2 - Assign the record's ownership
    var assignRequest = new AssignRequest()
    {
        Assignee = new EntityReference
        {
            LogicalName = "team",
            Id = new Guid("042d5707-6fe5-e411-80e5-fc15b428fa14")
        },

        Target = new EntityReference
        {
            LogicalName = "account",
            Id = new Guid("0C2D5AC7-B7E4-E411-80E9-C4346BAC7DA8")
        },
    };
    var assignResponse = (AssignResponse)service.Execute(assignRequest);

    // Service call 3 - Update the record's status by setting its statecode, statuscode fields
    var setStateRequest = new SetStateRequest()
    {
        EntityMoniker = new EntityReference
        {
            LogicalName = "account",
            Id = new Guid("0C2D5AC7-B7E4-E411-80E9-C4346BAC7DA8")
        },
        State = new OptionSetValue(1), //inactive
        Status = new OptionSetValue(2) //inactive
    };
    var setStateResponse = (SetStateResponse)service.Execute(setStateRequest);
}

The Good News

With CRM online 2015 Update 1 release, you no longer have to make special service calls to update those fields, you can simply put all special fields in the same property bag and use one service call to update the CRM record. This makes it significantly easier to write integration code, and at the same time it now offers a much better performance due to the reduced service calls. The following is the code that you can use for the same purpose as above if you are on CRM Online 2015 Update 1.

using (var service = new OrganizationService(crmConnection))
{
    Entity account = new Entity("account");
    account["accountid"] = new Guid("0C2D5AC7-B7E4-E411-80E9-C4346BAC7DA8");
    account["name"] = "Adventure Works Inc.";
    account["creditlimit"] = new Money(100000);
    account["statecode"] = new OptionSetValue(1); //inactive
    account["statuscode"] = new OptionSetValue(2); //inactive
    account["ownerid"] = new EntityReference { LogicalName = "team", Id = new Guid("042d5707-6fe5-e411-80e5-fc15b428fa14") };

    var request = new UpdateRequest() { Target = account };
    var response = (UpdateResponse)service.Execute(request);
}

Hope you can see the benefits by comparing the above two code snippets that actually do the exactly same thing.

Some Final Thoughts

  • Unfortunately CreateRequest does not include this enhancement as of this release, which could benefit from this capability by saving a service call for statecode and statuscode fields. CreateRequest does support including ownerid field directly. However, if you need to work with statecode, statuscode fields when creating CRM records, you will have to send in an additional service call (SetStateRequest) if the statecode is not the default statecode (Active for most entities). I will create a feature request for this particular issue on Microsoft connect site. I will share the URL here once that becomes available, so you can help vote up. (Update - May 20, 2015, here is the link of the Connection item).
  • Consider that the above special messages are deprecated as per CRM online documentation page, which means that you should start moving away from using those messages. 
  • Watch those plugin pipeline changes mentioned in the CRM online documentation page: Perform specialized operations using Update.
  • Your CRM plugin registered under SetState message should be registered under Update message as well in order to capture the status change if you take the new Update approach.

The less special messages to deal with, the better. Do you agree?

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