web
You’re offline. This is a read only version of the page.
close
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

I have the same question (0)
  • Koen VdV Profile Picture
    1,219 on at

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

  • Community Member Profile Picture
    on at

    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).

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    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.

  • Suggested answer
    JayeshAC Profile Picture
    1,157 on at

    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

  • Community Member Profile Picture
    on at

    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
    Iswarya Profile Picture
    1,347 on at

    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();

                       }

                   }

               }

           }

       }

    }

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans