Convert Currency/Amount field to Words in Dynamics 365
Introduction:
In many business scenarios, displaying currency amounts in words is crucial for clarity and compliance. Dynamics 365 doesn't offer a built-in feature for this conversion. In this blog, we'll walk through the process of implementing a solution to automatically convert currency amounts to words in Dynamics 365 (e.g., 1075 becomes "One Thousand and Seventy Five").
Refer to this video for a detailed implementation guide for converting amount to words-
Requirements:
- Access to Dynamics 365 environment with customization privileges.
- Basic understanding of JavaScript and Dynamics 365 customization.
-----Steps------
Step 1: Create Custom Fields and place these field on the main form
Create two custom fields:
- Amount (Currency) : This field will store the currency amount.
- Amount in Words (Single Line of Text) : This field will display the currency amount in words.
Step 2: Write Conversion Logic
Write JavaScript functions to convert the numeric currency amount to words. Below is the code snippet for the conversion logic:
JavaScript (Download link here)
function convertAmountToWordsDemo() { // Get the numeric value from the new_amount field var amount = Xrm.Page.getAttribute("new_amount").getValue(); // Check if the amount is not null or undefined if (amount != null && amount !== undefined) { // Convert the numeric value to words var amountInWords = convertNumberToWords(amount); // Set the value in the new_amountinwords field Xrm.Page.getAttribute("new_amountinwords").setValue(amountInWords); } } // Function to convert a numeric value to words function convertNumberToWords(n) { if (n < 0) return false; var singleDigit = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine']; var doubleDigit = ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen']; var belowHundred = ['Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']; if (n === 0) return 'Zero'; // Separate the integer and decimal parts var integerPart = Math.floor(n); var decimalPart = Math.round((n - integerPart) * 100); // Convert decimal to cents function translate(n) { var word = ""; if (n < 10) { word = singleDigit[n] + ''; } else if (n < 20) { word = doubleDigit[n - 10] + ' '; } else if (n < 100) { var rem = translate(n % 10); word = belowHundred[(n - n % 10) / 10 - 2] + ' ' + rem; } else if (n < 1000) { var remainder = translate(n % 100); word = singleDigit[Math.trunc(n / 100)] + ' Hundred ' + remainder; } else if (n < 1000000) { var remainder = translate(n % 1000); word = translate(Math.trunc(n / 1000)) + ' Thousand ' + remainder; } else if (n < 1000000000) { var remainder = translate(n % 1000000); word = translate(Math.trunc(n / 1000000)) + ' Million ' + remainder; } else { var remainder = translate(n % 1000000000); word = translate(Math.trunc(n / 1000000000)) + ' Billion ' + remainder; } return word; } // Create the words for the integer part var integerWords = translate(integerPart).trim(); // Create the words for the decimal part var decimalWords = (decimalPart > 0) ? 'and ' + translate(decimalPart).trim() + ' cents' : ''; // Construct the final result var result = (integerWords + ' ' + decimalWords).trim(); return result; }
Code Logic
- The convertAmountToWordsDemo function triggers when needed (e.g., on record save).
- It retrieves the amount value from the new_amount field.
- It calls the convertNumberToWords function to convert the numeric value to words.
- Finally, it sets the converted text in the new_amountinwords field.
The convertNumberToWords function handles the core conversion logic:
- It defines arrays for representing digits, teens, and tens.
- It translates the number part by place value (hundreds, thousands, millions, etc.).
- It handles decimals by converting them to cents and translating them separately.
- It constructs the final result by combining the integer and decimal words.
Step 3: Attach Script to Currency Field
Navigate to your entity customization in Dynamics 365.
Go to the "Forms" section depending on where you want the functionality.
Edit the desired form.
Within the form editor, add a new web resource and paste the JavaScript code.
Attach the `convertAmountToWordsDemo` function to the onchange event of the currency field on the form. This ensures that whenever the currency amount changes, the conversion function is triggered.
Save and Publish the form.
Step 4: Test
Test the solution thoroughly to ensure accurate conversion and proper functionality. Consider scenarios such as different currency amounts, decimal places, and negative values.
Conclusion:
By following these steps, you can implement a solution to automatically convert currency amounts to words in Dynamics 365. This enhancement improves the clarity of displayed currency amounts and ensures compliance with business requirements.
JavaScript code here