1

What is the best way to perform upsert operation in CRM from c#.

Do I need to fetch the record everytime to check if it exists before inserting new one or is there a better way to do it? And performance effects?

If anyone got any example of it?

1 Answer 1

6

Boom..Boom. UpsertRequest

    //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);

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).

If you are in on-premise or older versions, Retrieve has to be called to verify if record exists before Create/Update call.

2
  • I think you can even configure custom keys for Upserts (so you don't have to rely on GUIDs) Commented Aug 24, 2017 at 3:42
  • Correct. Alternate key will be used to identify if it's already available. Commented Aug 24, 2017 at 10:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.