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:
var contactRecord = { "@odata.type": "Microsoft.Dynamics.CRM.contact", "firstname": "Andrew", "lastname": "Butenko" }; var pagingInfo = { "PageNumber": 1, "Count": 10 }; var context; //Use this code if you're on 8.x if (typeof GetGlobalContext === "function") { context = GetGlobalContext(); } else { context = Xrm.Page.context; } //Use this code if you're on 9.x context = Xrm.Utility.getGlobalContext(); var requestUrl = "/api/data/v8.2/RetrieveDuplicates(BusinessEntity=@p1,MatchingEntityName=@p2,PagingInfo=@p3)"; requestUrl += "?@p1=" + encodeURIComponent(JSON.stringify(contactRecord)); requestUrl += "&@p2='contact'";//put account, lead or other entity here requestUrl += "&@p3=" + encodeURIComponent(JSON.stringify(pagingInfo)); var req = new XMLHttpRequest(); req.open("GET", context.getClientUrl() + requestUrl, true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var results = JSON.parse(this.response); //you've got results to results variable } else { var errorText = this.responseText; //handle error here } } }; req.send();
And here is Xrm.WebApi version:
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 } );
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 ?
Hello,
I checked and it works fine for me. What exactly did you do with code and where you tried it?
Thanks,
Andrew
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
}
);
}
try with Hashtag:
“@odata.type”: “#Microsoft.Dynamics.CRM.contact”,
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?
Спасибо,
Юрий
Юрий,
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.
Андрій
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
});
Richard,
Thanks!
Andrew