Search This Blog

Monday, May 1, 2023

UPSERT - Update or Insert in Dynamics CRM

 When you configure an Alternate key for an entity then you are getting this most powerful feature - UPSERT


What does that mean? Back in old days before CRM online 2015 update 1 - we have to make two calls ie one for checking the record availability using service.Retrieve and another one for either service.Create or service.Update. Yes, this is really painful.

But now we can do all this in single call. Platform will take care of everything. Refer the below snippet:

    //use alternate key for product
Entity productToCreate = new Entity("sample_product", "sample_productcode", productCode);

productToCreate
["sample_name"] = productName;
productToCreate
["sample_category"] = productCategory;
productToCreate
["sample_make"] = productMake;
UpsertRequest request = new UpsertRequest()
{
Target = productToCreate
};

// Execute UpsertRequest and obtain UpsertResponse.
UpsertResponse response = (UpsertResponse)_serviceProxy.Execute(request);
if (response.RecordCreated)
Console.WriteLine("New record {0} is created!", productName);
else
Console.WriteLine("Existing record {0} is updated!", productName);

No comments:

Post a Comment