web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Suggested Answer

Duplicate Record Detection

(1) ShareShare
ReportReport
Posted on by 365

i m using this code but it's not working help to fix this code

function Duplicaterecord(Context){
debugger;
var formContext = Context.getFormContext();
var id = formContext.data.entity.getId();
var req = new XMLHttpRequest();
var results;
req.open("GET", formContext.getClientUrl() + "/api/data/v9.1/msdyn_customerassets?$select=new_preventivemaintenanceschedule&$filter=new_preventivemaintenanceschedule eq "+ id.replace("{", "").replace("}", "") ,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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
debugger;
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 200) {
results = JSON.parse(this.response);
if (results.length > 0)
{
alert("Duplicate Record Detected");
}


} else {
}
}
};
req.send();
}

  • Suggested answer
    necsa Profile Picture
    3,455 on at

    Hi, unbeatable,

    Please use as a reference following link:

    xrmdynamicscrm.wordpress.com/.../

  • Suggested answer
    LeoAlt Profile Picture
    16,341 Moderator on at

    Hi Partner,

    1.If you are calling this code in entity form, you need to pass "executionContext" in the function, not "context".

    2.You need to select the "" in form properties.

    3.The code you provided is only  used to retrieve a special msdyn_customerassets record with its id. 

    BTW, please share more information about the issue you mentioned, is there any error message when the code running?

    Regards,

    Leo

  • Suggested answer
    necsa Profile Picture
    3,455 on at

    Another two links for programmatically duplicate detection. Take as a reference and modified for your solution.

    community.dynamics.com/.../767772

    social.microsoft.com/.../how-can-i-do-a-duplicate-detection-using-javascript-and-odata-endpoints

  • Suggested answer
    Bipin D365 Profile Picture
    28,995 Moderator on at

    Hello,

    Why don't you use xrm.web api simple code to achieve your requirement.

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/retrievemultiplerecords

    Use Rest Builder to generate js code- github.com/.../releases

    Please mark my answer verified if i were helpful

  • unbeatable Profile Picture
    365 on at

    okay I will discuss what actually I m doing so there is one lookup field logical name is("new_preventivemaintenanceschedule ") and whenever its value changes it check if any other customer asset("msdyn_customerasset") using the same value if yes then give an alert msg, this is our entity where I m putting code, for onchange event of lookup is using that code

  • Suggested answer
    LeoAlt Profile Picture
    16,341 Moderator on at

    Hi Partner,

    Thanks for your details.

    As I mentioned before, please try to pass "executionContext" in your JS function first.

    And if it still doesn't work, please try the following code.

    function checkLookup(executionContext) {
        var formContext = executionContext.getFormContext();
        var id = formContext.data.entity.getId();
        id = id.substring(1, id.length - 1);
        //call web api
        Xrm.WebApi.retrieveMultipleRecords("msdyn_customerassets", "?$select=new_preventivemaintenanceschedule&$filter=new_preventivemaintenanceschedule  eq "   id).then(
            function success(result) {
                if (results.length > 0) {
                    alert("Duplicate Record Detected");
                }
            },
            function (error) {
                console.log(error.message);
            }
        )
    }

    Best Regards,

    Leo

  • unbeatable Profile Picture
    365 on at

    i had tried this and also tried my above one also but its not detecting duplicate record

  • LeoAlt Profile Picture
    16,341 Moderator on at

    Hi Partner,

    All the code above could not detect duplicate record, they could only retrieve the record with the special id you provided.

    And here is the code logic:

    When you update the "new_preventivemaintenanceschedule" field on your form, the code will be triggered. It will retrieve all the records with  its "new_preventivemaintenanceschedule" look up field value equals your current entity id from "msdyn_customerassets" entity, and if there is such record, then alert the message "Duplicate Record Detected".

    So is this your complete requirement? If not, please share more details about your requirement so that we could provide the correct code for you.

    Regards,

    Leo

  • unbeatable Profile Picture
    365 on at

    yes, that's all my requirement but no alert msg has been displayed when I change the"new_preventivemaintenanceschedule" field, no alerts message showing

  • LeoAlt Profile Picture
    16,341 Moderator on at

    Hi Partner,

    If there is no alert message, so the condition has not been triggered which means there is no such record with the same id as "new_preventivemaintenanceschedule" value used.

    Or you could also debug the code step by step in F12 developer tool for more details.

    Regards,

    Leo

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
11manish Profile Picture

11manish 48 Super User 2026 Season 2

#2
Travis South Profile Picture

Travis South 45

#3
Ayesha Wajahat Profile Picture

Ayesha Wajahat 18

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans