Working with Currencies, Revisited
In a previous article, I reviewed an error I was receiving when performing some JavaScript calculations on a CRM form. I ran into the same error message this week, but under different circumstances.
The user was receiving the following error message when opening a record of a custom entity we created:
Hmm, I thought to myself, I’ve seen this before.
It turns out that I was creating this record via a plug-in and had forgotten to include the transactioncurrencyid in the record.
Remember: You must always include the transactioncurrencyid attribute on any record you create if there is a money attribute contained anywhere within the entity.
How do you know you have a problem with the record? Instead of your money fields looking like this:
They look like this:
Notice the lack of a currency symbol to the left of the edit box?
The only way to correct such a record is to add the transactioncurrencyid attribute to the form and change the value on the form, and either use JavaScript to add the currency or have the user set it manually.
Again, the above statement applies to records that have been created programmatically. Records created manually should have the transactioncurrencyid populated automatically.
So, to keep from having issues, just add the transactioncurrencyid attribute to your list of attributes that are created.
Here is a method in C# that will pull back the information for the US Dollar:
public static transactioncurrency GetCurrency(ICrmService crmService) { // Build a query for US Dollar currency. QueryByAttribute dollarQuery = new QueryByAttribute(); dollarQuery.EntityName = EntityName.transactioncurrency.ToString(); dollarQuery.Attributes = new string[] { "currencyname" }; dollarQuery.Values = new string[] { "US Dollar" }; dollarQuery.ColumnSet = new AllColumns(); // Create the US currency request. RetrieveMultipleRequest dollarRequest = new RetrieveMultipleRequest(); dollarRequest.Query = dollarQuery; RetrieveMultipleResponse dollarResponse = (RetrieveMultipleResponse)crmService.Execute(dollarRequest); return (transactioncurrency)dollarResponse.BusinessEntityCollection.BusinessEntities[0]; }
which is used like this:
transactioncurrency usCurrency = GetCurrency(crmService); myEntity["transactioncurrencyid"] = new Lookup(EntityName.transactioncurrency.ToString(), usCurrency.transactioncurrencyid.Value);
The above assumes you’re working with Dynamic Entities inside the CRM plug-in framework.
This was originally posted here.
*This post is locked for comments