Removing numbers from User Input in Microsoft Dynamics 365 Fields using JavaScript

Microsoft Dynamics 365 is a powerful customer relationship management (CRM) platform that provides organizations with a wide range of features to manage and analyze customer interactions. One common requirement in CRM systems is to sanitize user input by removing specific characters or data types. Having good data quality can be key in avoiding duplicates and errors along the way. In this blog post, we will explore how to remove numbers from user input in Microsoft Dynamics 365 fields using JavaScript. It can be used for example when using DATEV Debtor Numbers).

How to implement the script

To follow along with this tutorial, you should have a basic understanding of JavaScript and be familiar with the customization capabilities of Microsoft Dynamics 365.

Step 1: Accessing the Form’s OnSave Event To begin, we need to access the form’s OnSave event, which allows us to intercept the user’s input before it is saved to the CRM system. Open the form customization editor in Microsoft Dynamics 365, select the desired form, and navigate to the Form Properties. Locate the OnSave event and choose “Form Libraries” from the drop-down menu.

Step 2: Create a JavaScript Web Resource Next, we need to create a JavaScript web resource that contains the code responsible for removing numbers from the user’s input. In the Dynamics 365 customization editor, navigate to Web Resources and click “New” to create a new web resource. Choose “Script (JScript)” as the type and provide a meaningful name for the resource. Paste the following code into the editor:


function checkForDigits(executionContext) {
    var formContext = executionContext.getFormContext();
    var textValue = formContext.getAttribute("new_yourfieldname").getValue();
    //check if the text value is number type
    var patt = new RegExp("^[0-9]*$");
    if (!patt.test(textValue)) {
        formContext.getAttribute("new_yourfieldname").setValue(textValue.replace(/\D/g,''));
    }
}

Make sure to replace “new_yourfieldname” with the actual logical name of the field you want to remove numbers from. This code retrieves the field’s value, removes all numerical characters using a regular expression, and sets the sanitized value back to the field.

Step 3: Associate the Web Resource with the Form After saving the JavaScript web resource, go back to the form customization editor and open the “Field Properties” for the desired form. Under the “Event Handlers” tab, click “Add” to associate a new event handler. Select the event “OnChange” and choose the previously created web resource from the “Library” drop-down menu. Make sure you use the correct function name (“checkForDigits”) and pass the execution context.

Step 4: Testing and Deployment Now that we have completed the necessary configurations, it’s time to test the functionality. Save and publish the changes made to the form customization. Open a record with the form you modified, enter a value containing numbers into the designated field, and save the record. The JavaScript code will automatically remove the numbers from the input after the system recognizes a change in the dataset.

Conclusion

Conclusion: In this blog post, we explored how to remove numbers from user input in Microsoft Dynamics 365 fields using JavaScript. By leveraging the customization capabilities of Dynamics 365 and the powerful JavaScript API provided by the platform, we were able to intercept the OnSave event and sanitize the user’s input. This technique can be extended to handle other types of data or character removal requirements, providing organizations with greater control over their CRM data quality.

Remember to thoroughly test any customizations before deploying them to a production environment and consider the impact on other system functionalities. With the knowledge gained from this tutorial, you can now enhance the data validation and sanitization processes in your Microsoft Dynamics 365 implementation.

Leave a comment