Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Duplicate detection using JavaScript

(0) ShareShare
ReportReport
Posted on by

Hello,
I am trying to write a function which throws an error message if one of the records has a few field records which are duplicate.
Name of entity - dev_employee
Name of the lookup field - dev_centerid
Name of the field which needs to be checked for duplicates - dev_employeeid

So I am using SDk.Rest method to retrieve all the dev_employee records in which the dev_centerid is equal to the current record on which it will be run.

function EmployeeDuplicateDetection() {
var ecid = Xrm.Page.data.entity.attributes.get("dev_centerid").getValue()[0].id;
var EmpID = Xrm.Page.getAttribute("dev_employeeid").getValue();
var containsDuplicates = false;
SDK.REST.retrieveMultipleRecords(
"dev_employee",
"?$select=dev_employeeid,dev_entertainmentcenterid&$filter=dev_entertainmentcenterid/Id eq guid'" + ecid,
function (results) {

if (results.length > 0)
{
int length = results.length;
for(i=0; i<length; i++)
{
var employeeID = results[i].dev_employeeid;
if(employeeID == EmpID)
{
alert("Record with same Employee ID already exists. Please use different ID.");
}
}
}
},
function (error) {
alert(error.message);
},
function () {
//On Complete - Do Something
}
);
}


I am thrown a EmployeeDuplicateDetection() cannot be run on Event handler error. I have loaded the Rest.SDK library to the form as well.

*This post is locked for comments

  • Suggested answer
    Iswarya Profile Picture
    1,345 on at
    RE: Duplicate detection using JavaScript

    Hi,

    the following script will populate alert if there is any duplicate record present, and prevent the current record to save.

    function GetData(executionObj)

    {

       debugger;

       var x= Xrm.Page.getAttribute("new_x").getValue();

       var y=Xrm.Page.getAttribute("new_y").getValue();

       if (x!= null && y!=null )

       {

           var xId = x[0].id;

           var yId = y[0].id;     

           var context = Xrm.Page.context;

           var serverUrl = context.getClientUrl();

           var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";

           var retrieveResult = new XMLHttpRequest();

           retrieveResult.open("GET", ODataPath +"/new_TestSet?$select=new_X,new_Y&$filter=new_X/Id eq  guid'"+xId+"' and new_Y/Id eq guid'"+yId+"'",false);

           retrieveResult.setRequestHeader("Accept", "application/json");

           retrieveResult.setRequestHeader("Content-Type", "application/json; charset=utf-8?");

           retrieveResult.send();

           if (retrieveResult.readyState == 4 /* complete */)

           {

               if (retrieveResult.status == 200)

               {

                   var retrieved = this.parent.JSON.parse(retrieveResult.responseText).d;

                   var Result = retrieved.results;

                   if (typeof Result !== "undefined")

                   {

                       var count = Result.length;

                       if (count>1)

                       {

                           alert("duplicate value not allowed");

                           executionObj.getEventArgs().preventDefault();

                       }

                   }

               }

           }

       }

    }

  • Community Member Profile Picture
    on at
    RE: Duplicate detection using JavaScript

    Hi Jim,

    You can use OOB Key feature available for each Entity in CRM 2016 to restrict the user from creating the duplicate record.

    This feature will also through an message for duplicate record.

  • Suggested answer
    JayeshAC Profile Picture
    1,157 on at
    RE: Duplicate detection using JavaScript

    Hi Jim,

    Please correct me if my understanding is wrong. You want to restrict an user to create an record if it is already present in the system with same dev_centerid and you are verifying the duplicate records by dev_centerid of the current record.

    If my understanding is correct then why don't you use sync plugin in Pre-validation stage?

    You can use Query Expression/Linq/FetchXml to retrieve the record whose dev_centerid is equal to current record's dev_centerid. If you found the result then simply throw an InvalidPluginExecutionException with error message. It will restrict the user to create a record with same dev_centerid.

    Check below link to know more abount InvalidPluginExecutionException.

    msdn.microsoft.com/.../microsoft.xrm.sdk.invalidpluginexecutionexception.aspx

    Hope this helps to resolve your issue.

    Regards,

    Jayesh

  • Aric Levin Profile Picture
    30,188 Moderator on at
    RE: Duplicate detection using JavaScript

    I am not sure if this is the case, but why are you not using the CRM 2016 Web API. It is possible that the Rest.SDK is not longer functioning (or functioning properly) in 2016.

    There are a few libraries that you can use for the CRM 2016 Web Api (which are somewhat similar to Rest SDK). Below are a couple:

    github.com/.../Xrm.Tools.CRMWebAPI

    sdkwebapi.codeplex.com

    Microsoft released webapi functionality as part of their Xrm namespace in v9.

    Hope this helps.

  • Community Member Profile Picture
    on at
    RE: Duplicate detection using JavaScript

    Hi Koen,

    Thanks for the response. Are you asking about the duplicate detection? If that's the case, the reason is I just want the user to be thrown an error message. I do not want the user to be shown all the records with the same field value existing in the system(Duplicate Detectiion wizard).

  • Koen VdV Profile Picture
    1,219 on at
    RE: Duplicate detection using JavaScript

    Probably you have a good reason for this, but why are you not using the OOB functionality?

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,892 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,772 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans