Tuesday 4 September 2012

Dynamics CRM 2011 leverage duplicate detection with jquery

Have you come across a situation where users take a considerable amount of time filling in a form and at the end the CRM duplicate detection pops-up?

With Jquery we can pro-actively check for duplicate records performing a number of checks on the background; on a successful trigger alert the user.


The process is straight forward, however I have used jquery CSS UI libraries to give the solution a better look-and-feel. You can download the CRM unmanaged solution on the following link:
http://gallery.technet.microsoft.com/Dynamics-CRM-2011-3e9fdb10

If you would like to change the pop-up colour and layout, look at the available jquery UI libraries:
http://jqueryui.com/

When you download the solution and import it into CRM, load the following resources on the account form properties:


Below is the javascript code that performs the duplicate check.  On this example it checks for the account name, however you can extend this and perform multiple checks and combinations of data:

function idp() {

var isCreateForm = Xrm.Page.ui.getFormType() == 1;
if (isCreateForm) {

$.ajaxSetup({cache:false});
$(document.body).append("
A possible duplicate record was found, please review:
");

$('#name').focusout(function() {

var name = Xrm.Page.getAttribute('name').getValue();
var query = "/AccountSet?$filter=Name eq '"+name+"'";

var serverUrl = Xrm.Page.context.getServerUrl();

    if (serverUrl.match(/\/$/)) {
        serverUrl = serverUrl.substring(0, serverUrl.length - 1);
    }

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

    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: ODataURL,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, XmlHttpRequest) {
           var entities = data.d.results;
    $.fx.speeds._default = 1000;
    $(document).ready(function() {
        $("#dialog").dialog({
            autoOpen: false,
            show: "blind",
            hide: "explode",
            modal: true
        });

    });
if(entities.length > 0 ) {
  for( i=0; i< entities.length; i++)
  {
     var entity = entities[i];
}    

var entityUrl = "main.aspx?etn=account&pagetype=entityrecord&id=";
$(document).ready(function() {
                             $("#link").empty();
                             $("#link").append(document.createTextNode(entity.Name));
                             $("a#link").attr("href","/"+entityUrl+"%7B"+entity.AccountId+"%7D"+"");
                             $('#dialog').dialog("open");
});
          
}

        },
        error: function (XmlHttpRequest, textStatus, errorObject) {
            alert("OData Execution Error Occurred");
        }
    });


});

}

}

1 comment:

  1. This is fantastic, I'm thinking of implementing this for a charity project, but is it supported code?

    ReplyDelete