JavaScript Required

We're sorry, but we doesn't work properly without JavaScript enabled.

Looking for an Expert Development Team? Take two weeks Trial! Try Now

Troubleshoot Dynamics 365 (CRM) Error: A currency is required if a value exists in a money field. Select a currency and try again

Let’s discuss the tips given by a Microsoft Dynamics CRM Services Team to solve “A currency is required if a value exists in a money field. Select a currency and try again”

Above error is common while Recently I got above error when I am trying to set amount field in the forms, the reason behind that I am setting Currency lookup using JavaScript in the form onload and then I am going to set an amount field manually with certain value.

Here is my code to set currency lookup -

var lookup = new Array(); lookup[0] = new Object(); lookup[0].id = "GUID of Active currency”; lookup[0].name = “Currency Name”; lookup[0].entityType = "transactioncurrency"; Xrm.Page.getAttribute("transactioncurrencyid").setSubmitMode("always");"etel_currencyid").setValue(lookup);

And when I fill the amount field, I could not see any currency prefix in the field, so I getting below screenshot error.

screenshot error

The behavior I have seen that when we set currency lookup manually by hand, I can see the currency symbol/prefix correctly. So, I realize that something happening internally when I set currency lookup manually.

lookup currncy

Here you can see when I set value currency fields is automatically set the currency symbol here in the field and its expected behavior.

currency symbol

I am bit surprised how to get rid-off this issue, I don’t want the user to set currency lookup manually and I found some unsupported way to set the currency symbol in the amount field. Then I thought that I would go with business rules to set the currency lookup, but I got the same error. So, business rules will not help us in this scenario.

Now we have left other two other options either plugin or workflow, first I moved ahead with workflow and I create workflow to set the currency lookup while saving and its working fine for me. But there is a problem, as you know if you have a multiple environment with multiple currencies and currency lookup GUID may not be the same for the entire environment. So, if I set currency lookup using the workflow you need to be make sure that the entire environment should be the same currency lookup GUID to work with workflow.

In my case I checked that I had a different currency GUID for the same currency in different environment. So, I decided to go with plugin. The plugin I register in Create event Pre operation to avoid additional update. Here is my plugin look like -

using System; using System.Text; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; namespace MSCRMPlugins { public class SetDefaultCurrency: IPlugin { public void Execute(IServiceProvider serviceProvider) { try { IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory) serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService _service = serviceFactory.CreateOrganizationService(context.InitiatingUserId); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { Entity entity = (Entity) context.InputParameters["Target"]; if (entity.LogicalName == "Entit Name") { string fetchCurrency = @ "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" + "<entity name='transactioncurrency'>" + "<attribute name='transactioncurrencyid' />" + "<attribute name='currencyname' />" + "<attribute name='isocurrencycode' />" + "<attribute name='currencysymbol' />" + "<attribute name='statuscode' />" + "<attribute name='statecode' />" + "</entity>" + "</fetch>"; EntityCollection result = new EntityCollection(); result = _service.RetrieveMultiple(new FetchExpression(fetchCurrency)); if (result.Entities.Count > 0) { foreach(Entity currency in result.Entities) { if (currency.Attributes.Contains("isocurrencycode")) { // Here I write hard coded check after getting all currency if (currency.Attributes["isocurrencycode"].ToString() == "XPF") { //Set the currency lookp here. entity["transactioncurrencyid"] = new EntityReference("transactioncurrency", currency.Id); } } } } } } } catch (Exception ex) { throw new InvalidPluginExecutionException(ex.Message); } } } }

Screenshot of plugin steps -

plugin setup

In summary, when you have any currency type field in your form and if your application support multiple currency makes sure you set currency lookup value for the user manually or server side using plugin or workflow otherwise you may face this kind of issue.

Recent Blogs


NSS Note

Some of our clients