Blog, Development

How to Retrieve Duplicates using WebApi

In this post I provide 2 ways to retrieve duplicates – using plain JS way and with usage of Xrm.WebApi namespace that was introduced as a part of 9.0 release.

So here is plain JS:

And here is Xrm.WebApi version:

 

8 Comments

  1. Hi Andrew,

    I have used same webApi method (just Copy-Pasted) to retrieve duplicates in online trial environment. I’m getting error like “Resource not found for the segment ‘Retrieve Duplicates'”. Do you have any Idea ?

    1. Hello,
      I checked and it works fine for me. What exactly did you do with code and where you tried it?
      Thanks,
      Andrew

  2. Hi…I have not modified anything, just used your code as it is in my custom Js file. my online trial version 9.1

    Here is the method I’m calling on form load and getting error “Resource not found for the segment ‘Retrieve Duplicates’”

    this.retrieveDuplicatesWebApi = function () {
    var contactRecord = {
    “@odata.type”: “Microsoft.Dynamics.CRM.contact”,
    “firstname”: “Andrew”,
    “lastname”: “Butenko”
    };

    var pagingInfo = {
    “PageNumber”: 1,
    “Count”: 10
    };

    var retrieveDuplicatesRequest = {
    BusinessEntity: contactRecord,
    MatchingEntityName: “contact”,
    PagingInfo: pagingInfo,
    getMetadata: function () {
    return {
    boundParameter: null,
    parameterTypes: {
    “BusinessEntity”: {
    “typeName”: “mscrm.crmbaseentity”,
    “structuralProperty”: 5
    },
    “MatchingEntityName”: {
    “typeName”: “Edm.String”,
    “structuralProperty”: 1
    },
    “PagingInfo”: {
    “typeName”: “mscrm.PagingInfo”,
    “structuralProperty”: 5
    }
    },
    operationType: 1,
    operationName: “RetrieveDuplicates”
    };
    }
    };

    Xrm.WebApi.online.execute(retrieveDuplicatesRequest).then(
    function success(result) {
    if (result.ok) {
    var results = JSON.parse(result.responseText);
    //you’ve got results to results variable
    }
    },
    function (error) {
    var errorText = error.message;
    //handle error here
    }
    );
    }

  3. Hello, i have the same issue as Praveen. “Resource not found for the segment ‘Retrieve Duplicates”
    So strange, i tried almost everything and didn’t managed to solve the problem. Andrew is there any chance you expirienced and solved this issue somehow?

    Спасибо,
    Юрий

    1. Юрий,
      I tried the “Xrm.WebApi way” and it doesn’t work anymore for some reason but the old-fashioned way with the use of XmlHttpRequest still works.
      Андрій

  4. Hey Andrew,

    I tried using the article and had the same issues as you did with the Xrm.WebApi version not work. Microsoft has posted some SDK code on GitHub that should allow you to fix it.

    The big key takeaway here is that they are creating the request as a function and they are then adding the getMetadata to the prototype of that function. Adding a getMetadata function directly to an object doesn’t work.

    https://github.com/MicrosoftDocs/powerapps-docs/blob/live/powerapps-docs/developer/model-driven-apps/clientapi/reference/Xrm-WebApi/online/execute.md

    var Sdk = window.Sdk || {};

    Sdk.RetrieveDuplicatesRequest = function(businessEntity, matchingEntityName, pagingInfo) {
    this.BusinessEntity = businessEntity;
    this.MatchingEntityName = matchingEntityName;
    this.PagingInfo = pagingInfo;

    };

    Sdk.RetrieveDuplicatesRequest.prototype.getMetadata = function() {
    return {
    boundParameter: null,
    parameterTypes: {
    “BusinessEntity”: {
    “typeName”: “mscrm.crmbaseentity”,
    “structuralProperty”: 5 // Entity Type
    },
    “MatchingEntityName”: {
    “typeName”: “Edm.String”,
    “structuralProperty”: 1 // Primitive Type
    },
    “PagingInfo”: {
    “typeName:”: “mscrm.PagingInfo”, // Complex Type
    “structuralProperty”: 5
    }
    },
    operationType: 1, // This is a function. Use ‘0’ for actions and ‘2’ for CRUD
    operationName: “RetrieveDuplicates”,
    };
    };

    // Create a variable to point to a contact record and with specific data in the needed fields
    var contactRecord = {
    “@odata.type”: “Microsoft.Dynamics.CRM.contact”,
    “firstname”: “Test”,
    “lastname”: “Account”
    };

    // Create a paging object to keep track of the current page and how many records we get per page
    var pagingInfo = {
    “PageNumber”: 1,
    “Count”: 10
    };

    // Create the variable retrieveDuplicatesRequest to build the request
    var retrieveDuplicatesRequest = new Sdk.RetrieveDuplicatesRequest(contactRecord, “contact”, pagingInfo);

    // Use the request object to execute the function
    Xrm.WebApi.online.execute(retrieveDuplicatesRequest).then(
    function(result) {
    if (result.ok) {
    result.json().then(
    function(response) {
    console.log(“The response is: %s”, response);
    });
    }
    },
    function(error) {
    console.log(error.message);
    // handle error conditions
    });

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.