Dynamics 365 is a multi-currency system. Each record can be configured to use a different currency if required. To learn more about currency behaviour in Dynamics 365, check out this great article from Joel Lindstrom. In this article, I will show you how to change the currency value and how to resolve one of the issues you may face when changing the currency value.
Change Currency Lookup
You can change the currency lookup (transactioncurrencyid) the same way you would change any other lookup value.
1 2 3 4 5 6 7 8 |
function changeCurrency() { var lookup = new Array(); lookup[0] = new Object(); lookup[0].id = "BA5C8DC2-AD06-E711-810A-C4346BC5C21C"; lookup[0].name = "Euro"; lookup[0].entityType = "transactioncurrency"; Xrm.Page.getAttribute("transactioncurrencyid").setValue(lookup); } |
The issue with above solution is that while the currency lookup changes correctly, the currency symbol of currency type fields don’t change. In this example, I have changed the currency from Australian dollar to Euro, but the symbol is still the ‘$’ symbol.
While researching how to resolve this issue, I found an UNSUPPORTED solution.
1 2 3 4 5 6 7 8 9 10 11 12 |
var obj= { id: '{TransactionCurrencyId}', entityType: 'transactioncurrency', type: 9105, name: '{CurrencyName}', keyValues: { currencysymbol: { name: 'currencysymbol', value: '{CurrencySymbol}' }, currencyprecision: { name: 'currencyprecision', value: "1" } } }; Xrm.Page.getControl('transactioncurrencyid').get_editControlBehavior().handleAfterLookup({ items: [obj] }); |
This code uses undocumented methods and is not recommended.
Change Currency Symbol (Supported Method)
The best way to resolve this issue is by using Xrm.Page.data.refresh(true). This function asynchronously refreshes and saves data on the form which changes the symbol to the correct one.
1 2 3 4 5 6 7 8 9 10 |
function changeCurrency() { var lookup = new Array(); lookup[0] = new Object(); lookup[0].id = "BA5C8DC2-AD06-E711-810A-C4346BC5C21C"; lookup[0].name = "Euro"; lookup[0].entityType = "transactioncurrency"; Xrm.Page.getAttribute("transactioncurrencyid").setValue(lookup); Xrm.Page.data.refresh(true); } |
Pre-2013
If your system is older than Dynamics CRM 2013 then this method can’t be used. You can use the below method to refresh the page. The difference is this method is not asynchronous. User will see the page is refreshing.
1 2 |
Xrm.Page.data.save(); Xrm.Utility.openEntityForm("myEntityName", id); |
Conclusion
When changing currency lookup using JavaScript, you need to call;
1 |
Xrm.Page.data.refresh(true); |
to make sure the currency symbols of all currency fields are also change to the appropriate symbol.
Thank you for visiting Dyn365Apps.com.
Follow me on Twitter to get the latest news, tips and tricks and more …
Until next time…