Javascript to Test if a Value is Unique Across All Entity Instances

One thing I have run into numerous times is a client requirement to be able to enter a value, usuall some kind of ID number, and have it be a unique value. Obviously CRM’s autonumbering system doesn’t work here, and you can’t just use Math.random() either. Niether of the two solutions above are really flexible enough if a client needs to have a certain numbering scheme to integrate into another system, wants the ability to enter an ID manually, et cetera.

Anyway, below is a little javascript snippit that will test if a value is unique across all instances of a particular entity. You could also use CRM’s duplicate detection settings, but I like this better since it allows you to provide instant feedback when the user tabs off the field rather than waiting for a save.


function isUniqueValue(value, fieldName, entity)


    var authenticationHeader = GenerateAuthenticationHeader();


    // Prepare the SOAP message.

    var xml = "<?xml version=’1.0′ encoding=’utf-8′?>" +

        "<soap:Envelope xmlns:soap=’’&quot; +

        " xmlns:xsi=’’&quot; +

        " xmlns:xsd=’’>&quot; +

        authenticationHeader +

        "<soap:Body>" +

        "<RetrieveMultiple xmlns=’’>&quot; +

        "<query xmlns:q1=’’&quot; +

        " xsi:type=’q1:QueryExpression’>" +


        "<q1:EntityName>"+entity+"</q1:EntityName>" +


        "<q1:ColumnSet xsi:type=’q1:ColumnSet’>" +

        "<q1:Attributes>" +

            "<q1:Attribute>"+fieldName+"</q1:Attribute>" +

        "</q1:Attributes>" +

        "</q1:ColumnSet>" +

        "<q1:Distinct>false</q1:Distinct>" +

        "<q1:Criteria>" +

        "<q1:FilterOperator>And</q1:FilterOperator>" +

        "<q1:Conditions>" +


        "<q1:Condition>" +

        "<q1:AttributeName>"+fieldName+"</q1:AttributeName>" +

        "<q1:Operator>Like</q1:Operator>" +

        "<q1:Values>" +

        "<q1:Value xsi:type=’xsd:string’>" + value + "</q1:Value>" +

        "</q1:Values>" +

        "</q1:Condition>" +


        "</q1:Conditions>" +

        "</q1:Criteria>" +

        "</query>" +

        "</RetrieveMultiple>" +

        "</soap:Body>" +


    // Prepare the xmlHttpObject and send the request.

    var xHReq = new ActiveXObject("Msxml2.XMLHTTP");

    xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);

    xHReq.setRequestHeader("SOAPAction", ";);

    xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

    xHReq.setRequestHeader("Content-Length", xml.length);


    // Capture the result.

    var resultXml = xHReq.responseXML;


    var beNodes = resultXml.getElementsByTagName("BusinessEntity");


    if (beNodes.length > 0)


        return false;




        return true;





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: