Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

CRM c# match a phone number ignoring characters [A-Z] and special characters

Posted on by 505

Hello,

I want to retrieve leads matching a phone number but ignoring characters and special characters.

For example if a phone number to match is "234567890" and the phone number on lead is "abc234?>*-;567890" , then my query expression should retrieve this lead.

How can I achieve this using query expression?

*This post is locked for comments

  • Suggested answer
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: CRM c# match a phone number ignoring characters [A-Z] and special characters

    Hi,

    This helps you,

    var input = "abc234?>*-;567890";
    var result = String.Join("", input.Where(c => Char.IsDigit(c) || c == '.'));
    Console.WriteLine(result);

    Thanks,

    Sreevalli

  • Suggested answer
    Daniel Wikell Profile Picture
    Daniel Wikell 2,360 on at
    RE: CRM c# match a phone number ignoring characters [A-Z] and special characters

    I would add some validation to ensure that phone numbers can't be entered like that in the first place.

    If you for some reason want to keep allowing phone numbers to have odd special characters or if this is an attempt at locating and cleaning up data after the introduction of some validation you can pretty much only accomplish this by duplicating the phone number field to a different field, with cleaned out characters. This way you can instead query your other field that only contain the digits of the phone number.

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: CRM c# match a phone number ignoring characters [A-Z] and special characters

    Hi,

    I don't think you can use RegEx within the conditonexpression. However you can retrieve all records and then apply LINQ & RegEx to retrieve the desired records. Something like mentioned in this thread-

    stackoverflow.com/.../how-do-i-search-for-lead-or-account-using-dynamics-crm-sdk

    Hope this helps.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: CRM c# match a phone number ignoring characters [A-Z] and special characters

    Hi Priyank,

    Try with this -

                            QueryExpression qe = new QueryExpression();
                            string AnyValue = "34534534";
                            string phonenumber = "%" + AnyValue + "%";
                            ConditionExpression condition1 = new ConditionExpression();
                            condition1.AttributeName = "phonenumber";
                            FilterExpression filter1 = new FilterExpression();
                            condition1.Operator = ConditionOperator.Like;
                            condition1.Values.Add(phonenumber);
                            filter1.Conditions.Add(condition1);
                            qe.Criteria.AddFilter(filter1);


  • Priyank Bhavsar Profile Picture
    Priyank Bhavsar 505 on at
    RE: CRM c# match a phone number ignoring characters [A-Z] and special characters

    Contains operator doesn't work for me because numeric characters can be at any position in the string.

  • Suggested answer
    Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: CRM c# match a phone number ignoring characters [A-Z] and special characters

    Hello,

    You can add a Condition .AddCondition("phonenumber, ConditionOperator. Contains, "your value")

    docs.microsoft.com/.../gg327700(v%3Dcrm.8)

    Hope this helps,

    Radu

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans