Skip to main content

Notifications

Announcements

No record found.

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

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 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,325 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.

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,642 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,371 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans