Xrm.WebApi query data with regex not working for dynamics crm

235 views Asked by At

I am trying to query dynamics crm accounts data with regex to compare with the account name as a duplicate detection measure to populate a warning to the end user with a confirm dialog, where user can proceed with creating a duplicate account name if they require. Here, in the query, I am trying to filter the account names ignoring any spaces within the names, then comparing it with the account name being created/updated and then populate a warning if the string matches. I intend to use js as OOB duplicate detection seem to match the complete word only and plugin would restrict the user creating a duplicate record completely.

Please suggest a way to achieve the same

I tried the following code which results in error(The "contains" function isn't supported.), I have also tried Microsoft.Dynamics.CRM.Contains, which results in error too

function checkForDuplicateAccountNames(executionContext) {
    // Use the Xrm.WebApi object to query Dynamics 365 for accounts with the same name

    var formContext = executionContext.getFormContext();
    var accountName = formContext.getAttribute("name").getValue().replace(/\s+/g, '');
    var query = "?$select=name&$filter=contains(replace(name, ' ', ''),'" + accountName + "') and statecode eq 0";

    executionContext.getEventArgs().preventDefault();

    Xrm.WebApi.retrieveMultipleRecords("account", query).then(
        function success(result) {
            if (result.entities.length > 0) {
                // If there are duplicate account names, show a confirm dialog to the user
                var confirmStrings = { text: "An account with this name already exists. Do you still want to create/update it?" };
                var confirmOptions = { height: 200, width: 450 };
                Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
                    function (success) {
                        // If the user clicks Yes, allow the form to save
                        if (success.confirmed) {
                            isValidationNeeded = false;
                            formContext.data.entity.save();
                        }
                    },
                    function (error) {
                        console.log(error.message);
                    }
                );
            }
            else {
                isValidationNeeded = false;
                formContext.data.entity.save();
            }
        },
        function error(error) {
            console.log(error.message);
        }
    );
}
0

There are 0 answers