Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Sales forum / Xrm.WebApi query data ...
Sales forum
Suggested answer

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

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

Categories:
  • Suggested answer
    a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: Xrm.WebApi query data with regex not working for dynamics crm

    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.

  • Bhaskar Sharma Profile Picture
    Bhaskar Sharma 60 on at
    RE: Xrm.WebApi query data with regex not working for dynamics crm

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

           }

       );

    }

  • a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: Xrm.WebApi query data with regex not working for dynamics crm

    Hello,

    I'm afraid this syntax won't work.

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,277 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,126 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans