omkar patade
omkar patade

Reputation: 1591

C# - Insert or update if exists in CRM

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?

Upvotes: 1

Views: 2035

Answers (1)

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.

Upvotes: 6

Related Questions