locked
How to avoid duplicates when creating records using Web API (C#) ? RRS feed

  • Question

  • When I try to create a duplicate in the CRM web application I am getting a pop-up indicating that I am trying to create a duplicate record. However, if the create using Web API (C#) from a .Net application then by default, the duplicate record is getting created without any warning.

    Here is the sample code we are using to using to create a record using Web API (MSDN Source

                JObject accountRecord = new JObject();
                accountRecord.Add("name", "JKC");
                accountRecord.Add("accountnumber", "00112233");
    
                HttpRequestMessage createAccountReq =
                   new HttpRequestMessage(HttpMethod.Post, getVersionedWebAPIPath() + "accounts");
    
                createAccountReq.Content = new StringContent(accountRecord.ToString(),
                    Encoding.UTF8, "application/json");
    
                HttpResponseMessage createAccountRes =
                    await httpClient.SendAsync(createAccountReq);


    Thanks and Regards.

    Sunday, May 7, 2017 9:25 AM

Answers

  • Hello,
    To turn on duplicates validation using webapi you should add new header "MSCRM.SuppressDuplicateDetection" to request with value "false".
    That should resolve your ussue.

    Dynamics CRM/Business Solutions MVP
    Read My blog

    • Marked as answer by Ravitheja J Monday, May 8, 2017 2:37 AM
    Monday, May 8, 2017 1:54 AM
    Moderator

All replies

  • Hi,

    The pop up dialog normally is a UI feature. If you want your code to do the duplicate check as well, you will have to employ that in your code.

    https://msdn.microsoft.com/en-us/library/hh210213.aspx

    I hope it helps.

    Thanks.

    Sunday, May 7, 2017 5:27 PM
  • I'm not sure if you can apply Duplicate Detection currently if using the Web API. As per the previous post, you can use a CreateRequest with the SuppressDuplicateDetection parameter if using the OrganizationService, but I've not seen an equivalent in the Web API, as Create is not exposed as an Action in the Web API

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Sunday, May 7, 2017 6:01 PM
    Moderator
  • Hello,
    To turn on duplicates validation using webapi you should add new header "MSCRM.SuppressDuplicateDetection" to request with value "false".
    That should resolve your ussue.

    Dynamics CRM/Business Solutions MVP
    Read My blog

    • Marked as answer by Ravitheja J Monday, May 8, 2017 2:37 AM
    Monday, May 8, 2017 1:54 AM
    Moderator