Email Address should be unique in contact form by JS RRS feed

  • Question

  • Hi Everyone,

    I Have Stuck in a Query. My requirement is simply that email address should be unique for a contact record. One record has a unique email address and if user creates another record with earlier existing email address, then form should not save and shows a alert message.

    I can do this thing by plugin. But I want to do by JS. I have searched a lot on google and find odata JS Query, Xrm toolkit.

    But unfortunately, My requirement is not fulfill. I have used below Odataquery code and read about a retrievemultiplerecords function in XrmServicetoolkit.


    function Account_onchange() {
    //var accountId = Xrm.Page.getAttribute('parentcustomerid').getValue();

    var accountId = Xrm.Page.getAttribute('emailaddress1'); 

        if (accountId != null) {
            var contacts = GetContactsByAccount(accountId);
            if (contacts != null && contacts[0].results.length > 0) {
                for (var count = 0; count < contacts[0].results.length; count++) {
    alert("There is no data");


    function GetContactsByAccount(accountId) {

       var serverUrl = "http://" + window.location.host + "/" + Xrm.Page.context.getOrgUniqueName();

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

    var oDataUri = oDataPath + "/ContactSet?$select=ContactId,LastName&$filter=EMailAddress1 eq '" + 

    accountId +"'";

        var jSonArray = new Array();

            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: oDataUri,
            async: false,
            beforeSend: function (XMLHttpRequest) {
                //Specifying this header ensures that the results will be returned as JSON.            
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            success: function (data, textStatus, XmlHttpRequest) {
                if (data && data.d != null) {
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert("Error :  has occured during retrieval of the contacts");

        return jSonArray;

    Please help on this.

    I will appreciate your view.

    Thanks in Advance

    Vivek Gupta

    Wednesday, January 22, 2014 2:16 PM

All replies

  • If you are using jQuery (like in your sample) the request should look something like this:

        type: 'GET',
        contentType: 'application/json; charset=utf-8',
        datatype: 'json',
        url: Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/ContactSet?$select=ContactId,EMailAddress1&$filter=EMailAddress1 eq 'test@test.com'",
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader('Accept', 'application/json');
        async: true,
        success: function (data, textStatus, xhr) {
            var results = data.d.results;
            if (results.length > 0) {
                alert("Email address already exists");
        error: function (xhr, textStatus, errorThrown) {
            alert(textStatus + ' ' + errorThrown);

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Thursday, January 23, 2014 2:37 AM
  • Hi Jason,

    Thanks for your reply.

    I have used the code that you have provided. But the same thing happens.

    Error Shows : Microsoft JScript runtime error: '0.results' is null or not an object

    Whereas there are several same emailaddress in the contact records.

    Please let me know is there any other alternative through JS.

    Thanks & Regards,

    Vivek Gupta

    Vivek gupta

    Thursday, January 23, 2014 7:46 AM
  • Hi Jason,

    Any Update !!!

    I am looking forward for it.

    Please help me on this.

    I will appreciate your view.

    Thanks & Regards,

    Vivek Gupta

    Vivek gupta

    Monday, January 27, 2014 10:26 AM