Upsert in MS Dynamics CRM

If we want to update a record in any entity, we should require GUID. If we know the GUID, we can do it easily, if not, we should retrieve the GUID of that record first, then we can update the record. Here, two database calls (Retrieve and Update) we are making which impacts performance of an application.


In these scenarios, Upsert will be very useful. Upsert request will check in database whether record exists in database, if exists it will update the record or else it will create the record.






So, how do we know that whether Upsert create or update record in each call?


This information we can get from UpsertResponse which contains a Boolean property RecordCreated. If RecordCreated contains “true” it indicates Upsert created the record else updated the record.


Sample Code:

               Entity testEntity = new Entity("new_test");
                testEntity["new_test1"] = "test1";
                testEntity["new_test2"] = "test2";
                testEntity["new_test3"] = "test3";

                UpsertRequest request = new UpsertRequest()
                {
                    Target = testEntity
                };
                try
                {
                    UpsertResponse response = (UpsertResponse)_serviceProxy.Execute(request);
                }
                catch (Exception ex)
                {
                  }


Note: For Microsoft Dynamics CRM Online organizations, this feature is available only if your organization has updated to Dynamics CRM Online 2015 Update 1. This feature is not available for Dynamics CRM (on-premises).

Comments

Popular posts from this blog

Workflow Activity Input and Output Parameters and Data Types in MS CRM 2013/15/16

Get Workflow Guid in Javascript using OData query

Convert Plugin to Sandbox mode,