Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Replace characters in calculated field

Posted on by Microsoft Employee

Hello,

the task seems to be simple but we are struggling to create a calculated field which replaces the value of another field.

For example we have the field "phone" with the value "+49 (30) 1234 - 56". We want to have the calculated field with the value "4930123456" (without any special chars and spaces).

We found this formula: REPLACE ("+1 923 456 4971", "[^0-9]", "", true)

Unfortunately we just get the error: The formula contains an invalid reference: true

Any ideas what we are missing?

Thank you!

Hannes

  • necsa Profile Picture
    necsa 3,455 on at
    RE: Replace characters in calculated field

    hereby some information:

    www.linkedin.com/.../

  • necsa Profile Picture
    necsa 3,455 on at
    RE: Replace characters in calculated field

    Hi Hannes,

    Can you give me more information about what do you want to achieve in the calculated field?

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Replace characters in calculated field

    Hi,

    Create simple single line of text field to store the unformatted phone number instead of calculated field. Say the schenam name of the field is new_unformattedphonenumber.

    Now on change of Phone field add on change event and call below javascript to set unformatted field value.

    function phoneOnchange(executionContext)
    {
      var _formContext=executionContext.getFormContext();
      var phone=_formContext.getAttribute('PHONE FIELD NAME').getValue();
      unformattedMobileNumber=phoneNumberUnformatting(phone);
      _formContext.getAttribute('new_unformattedphonenumber').setValue(unformattedMobileNumber);
    }
    
    function phoneNumberUnformatting(phoneNumber) {
            phoneNumber = phoneNumber.replace(/\s/g, "").replace(/[^a-zA-Z0-9 ]/g, "");
            if (phoneNumber.length > 10 && phoneNumber.length < 12 && phoneNumber.indexOf('1') == 0) {
                phoneNumber = phoneNumber.slice(1);
            }
            else if (phoneNumber.length == 13 && phoneNumber.indexOf('0') == 0) {
                phoneNumber = phoneNumber.slice(3);
            }
            return phoneNumber;
    
        }

    Note :- This code will only work when record gets created manually from UI. It you are importing the record or creating record from C# through integration you will need to write plugin code to do the same.

    If found helpful, Please mark my answer verified.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Replace characters in calculated field

    Thank you Necdet!

    But can you show me a tutorial or some page where it's described how to embed javascript code for a calculated field?

    I'm new to MS Dynamics, but shouldn't there a return-line in the function?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Replace characters in calculated field

    I found this page: docs.microsoft.com/.../er-functions-text-replace

    But this applies probably only to Dynamics Commerce and not to Dynamics CRM.

  • necsa Profile Picture
    necsa 3,455 on at
    RE: Replace characters in calculated field

    Hi Hannes,

    if you want to replace all special char from phone format then use the following code:

    function testNumber(executionContext){
                   
                   var formContext=executionContext.getFormContext();
                   
                   var number=formContext.getAttribute("telephone1");
                   
                   var phoneNumber=number.getValue();
                   var cleanedNumber=phoneNumber.replace(/[^0-9,A-Z,a-z]/g, "");
    
    }
    

    you will get from 49 (820) 123 456 7891  to 498201234567891

    you use wrong synthax.

  • Suggested answer
    Carlo Velasco Profile Picture
    Carlo Velasco 787 on at
    RE: Replace characters in calculated field

    Hi

    I unfortunately i don´t believe that the replace function is supported for calculated fields.

    pastedimage1596751302189v1.png

    Could you share the documentation where you are getting this function?

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,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans