locked
validate account number format show message not correct formate and if validated then check for Existing record if exist then show msg already exist RRS feed

  • Question

  • Hi there,

    I have requiremnt to validate account number fomat as123456d if not validated then msg not correct format and if validated then check for existance record if already exist then show msg already exist.

    I can validate through regex  on filed change event  how can iCheck for existance on the same field change event through OData query.

    Or do i need to write plugin to vaidate and then check for existance.

    please reply with some example code as i  m new to crm2011/2013


    • Edited by Crm_2013 Saturday, September 28, 2013 1:54 PM
    Saturday, September 28, 2013 1:53 PM

All replies

  • You can use something like this to check for the existence of of that account number - looks for an exact match on the account number and excludes the current record as a potential match.

    function ValidateAccountNumber() {
        var accountNumber = Xrm.Page.getAttribute("accountnumber").getValue();
    
        var serverUrl = Xrm.Page.context.getClientUrl();
        var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/AccountSet?$select=AccountNumber&$filter=AccountNumber eq '" + accountNumber + "' and AccountId ne guid'" + Xrm.Page.data.entity.getId() + "'&$top=1";
    
        var req = new XMLHttpRequest();
        req.open("GET", oDataSelect, false);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        req.onreadystatechange = function () {
            if (req.readyState === 4) {
                if (req.status === 200) {
                    var retrieved = JSON.parse(req.responseText).d;
                    if (retrieved.results.length > 0) {
                        alert("Account Number already exists");
                    }
                }
            }
        };
        req.send();
    }


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Saturday, September 28, 2013 4:35 PM
    Moderator
  • thanks for the Reply .Actuallly I want to call Both  function on field change event .Validate function and CheckExistancRecord Function.Is It possible 

    How can iAchive this thanks 

    function ValidateAccountNumber() {

        var accountNumber = Xrm.Page.getAttribute("accountnumber").getValue();

        AccRegex=/^[a-zZ-A]{2}[0-9]{6}[a-zA-Z]{1};

    if( !accountNumber.match(AccRegex)
    {
    alert("Please enter a format qw123456y");
    }
    else
    {

    }

    function  CheckexistantRecord(){
        var serverUrl = Xrm.Page.context.getClientUrl();
        var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/AccountSet?$select=AccountNumber&$filter=AccountNumber eq '" + accountNumber + "' and AccountId ne guid'" + Xrm.Page.data.entity.getId() + "'&$top=1";

        var req = new XMLHttpRequest();
        req.open("GET", oDataSelect, false);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        req.onreadystatechange = function () {
            if (req.readyState === 4) {
                if (req.status === 200) {
                    var retrieved = JSON.parse(req.responseText).d;
                    if (retrieved.results.length > 0) {
                        alert("Account Number already exists");
                    }
                }
            }
        };
        req.send();
    }

    Saturday, September 28, 2013 7:05 PM
  • How about something like this:

    function ValidateAccountNumber() {
        var accountNumber = Xrm.Page.getAttribute("accountnumber").getValue();
    	
    	var AccRegex = "/^[a-zZ-A]{2}[0-9]{6}[a-zA-Z]{1}";
        if (!accountNumber.match(AccRegex)) {
            alert("Please enter a format qw123456y");
    		return;
        }
    
        var serverUrl = Xrm.Page.context.getClientUrl();
        var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/AccountSet?$select=AccountNumber&$filter=AccountNumber eq '" + accountNumber + "' and AccountId ne guid'" + Xrm.Page.data.entity.getId() + "'&$top=1";
    
        var req = new XMLHttpRequest();
        req.open("GET", oDataSelect, false);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        req.onreadystatechange = function () {
            if (req.readyState === 4) {
                if (req.status === 200) {
                    var retrieved = JSON.parse(req.responseText).d;
                    if (retrieved.results.length > 0) {
                        alert("Account Number already exists");
                    }
                }
            }
        };
        req.send();
    }
    


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Saturday, September 28, 2013 8:08 PM
    Moderator
  • Hi 

    How If We write the same code in C# as plugin on server side .thanks

    Monday, September 30, 2013 6:07 AM