Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take 2 weeks Free 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”

The above error is common. Recently I got the above error when I am trying to set the amount field in the forms. The reason behind that I am setting Currency lookup using JavaScript in the form on load. Then I am going to set an amount field manually with a 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"); Xrm.Page.data.entity.attributes.get("etel_currencyid").setValue(lookup);

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

screenshot error

The behavior I have seen is that when we set currency lookup manually, 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 it automatically set the currency symbol here in the field and its expected behavior.

currency symbol

I am a bit surprised at how to get rid of 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 a workflow to set the currency lookup while saving, and it’s working fine for me. But there is a problem, as you know if you have multiple environments 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 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 a different environment. So, I decided to go with the plugin. The plugin I register in Create event Pre-operation to avoid the additional update. Here is what my plugin looks 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 currencies make sure you set currency lookup value for the user manually or server-side using a plugin or workflow otherwise you may face this kind of issue.

Software Development Team
Need Software Development Team?
captcha
🙌

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs

Categories

NSS Note
Trusted by Global Clients