New CRM SDK Feature - Upsert

16 April 2015
Daniel Cai

Upsert is a term that's often seen in a DBA's textbook, but not in a CRM developer's. Upsert stands for Update/Insert, it indicates that it could take either an Update or an Insert action depending on whether a match can be found when writing to the target system.

At KingswaySoft, we have offered Upsert functionality since our very first release of SSIS Integration Toolkit product, which has proved to be a very useful feature, as it significantly simplifies data integration development. Many of our clients rely on this robust feature to implement their ongoing data synchronization from external systems to CRM. Upsert was not a platform feature at the time, so we had to implement it using our own approach, which is to first make a CRM query service call to figure out if the incoming row is an existing record in CRM system or a new one before performing the write action (Create if not exists, otherwise update). This has worked really well, however it does come with an upfront cost, which is the query that involves a round-trip service call to the CRM server.

With the introduction of Upsert support to the new CRM release, it makes the platform even better.

Prerequisites

To use the Upsert capability, you need to:

  • Have a CRM 2015 Update 1 (v7.1) instance (CRM Online only at this moment) in order to utilize this new feature.
  • Use CRM SDK 7.1.0-preview to compile the code.
  • Create the Alternate Keys in CRM system using its customization tool or SDK (please refer to my other blog post for more detailed instructions on how to create Alternate Keys).

Code in Action

The following code snippet shows how to make a CRM Upsert service call using its UpsertRequest message.

using (var service = new OrganizationService(crmConnection))
{
    // Use alternate key (accountnumber) field to identify an account record
    var account = new Entity("account")
    {
        KeyAttributes = new KeyAttributeCollection
        {
            {"accountnumber", "ACT-12345" }
        }
    };

    account["name"] = "Microsoft";
    account["creditlimit"] = new Money(100000);

    var request = new UpsertRequest() { Target = account };
    var response = (UpsertResponse)service.Execute(request);
}

When you run the code, CRM server will first check if there is an account record that has an "ACT-12345" accountnumber. If it finds such a record, it will use the latest incoming values to update the record, or otherwise it will create a CRM record using the values provided.

Note that there is no Upsert plugin message for CRM Upsert requests, it will either be Create or Update message depending on which action is going to be taken.

References

For more details about CRM Upsert capability, please check the following links the CRM SDK documentation team has provided.

Hope this helps.

Read the full New SDK Capabilities Blog Series:

Update 1 release (v7.1)

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