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

Community site session details

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

Replace characters in calculated field

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • necsa Profile Picture
    3,455 on at
    RE: Replace characters in calculated field

    hereby some information:

    www.linkedin.com/.../

  • necsa Profile Picture
    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
    28,983 Moderator 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
    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
    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
    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
    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

Responsible AI policies

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

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 258

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 179

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 129 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans