web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

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

(0) ShareShare
ReportReport
Posted on by 60

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, I have also tried Microsoft.Dynamics.CRM.Contains, which results in error too

var accountName = formContext.getAttribute("name").getValue().replace(/\s+/g, '');

var query = "?$select=name&$filter=contains(replace(name, ' ', ''),'" + accountName + "') and statecode eq 0";

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I'm afraid this syntax won't work.

  • Bhaskar Sharma Profile Picture
    60 on at

    Hi Andrew, thanks for your response.

    is there an alternate query data you can suggest to validate active account names do not contain word with similar strings.

    for example:

    Account 1 Name: Microsoft Azure

    Account 2 Name: MicrosoftAzure

    This be identified as a potential duplicate.

    I found an alternate way but this first stores all the active accounts and then check from the array, which is not at all efficient.

    function checkForDuplicateAccountNames(executionContext) {

       var formContext = executionContext.getFormContext();

       var accountName = formContext.getAttribute("name").getValue().replace(/\s+/g, '');

       var accounts = [];

       var query = "?$select=name,statecode&$filter=statecode eq 0";

       executionContext.getEventArgs().preventDefault();

       Xrm.WebApi.retrieveMultipleRecords("account", query).then(

           function success(result) {

               accounts = result.entities;

               var filteredAccounts = accounts.filter(function (account) {

                   return account.name.replace(/\s+/g, '') === accountName;

               });

               if (filteredAccounts.length > 0) {

                   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);

           }

       );

    }

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Create a hidden field on the account and store the name of the account with no spaces over there. Do the check against that field.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 171 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 83

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans