Programming for Multi-Currency Applications

In addition to multilingual support, Microsoft Dynamics CRM supports multi-currency functionality for globally based deployments. Microsoft Dynamics CRM implements multiple currencies by allowing each record to be associated with its own currency. Microsoft Dynamics CRM calculates the value of the record in its specified currency in addition to calculating the record’s values using the organization’s base currency. This multi-currency functionality allows each user to work in his or her preferred currency, but it still allows users and management to report across the board in the base currency. Converting the currency values for each record into a base currency allows for greater efficiency when your organization runs reporting throughout the entire organization. Microsoft Dynamics CRM stores this base currency information in the basecurrencyid field of the OrganizationBase table in the database. Entity records that support a transaction currency will have a transactioncurrencyid attribute that specifies which Transaction Currency entity to use for the exchange rate between the transaction currency and the base currency.

Tip

Tip

Add new currencies to your Microsoft Dynamics CRM organization through the Currencies link on the Business Management screen of the Settings section.

When programming with money attributes, you need to consider which value you need: the value converted to the base currency or the value in the currency selected on the record. Microsoft Dynamics CRM actually makes this very easy. Each money attribute created in the database also has a second field associated with it that contains the value converted into the base currency. These associated fields are named with the following format:

<fieldname>_base

These _base fields are also created for any custom attributes you create with the type of money. For example, let’s say you want to write a piece of code that calculates the total estimated revenue from a user’s open opportunities. Users can have opportunity records in multiple currencies, and you want the total returned in the base currency. The following code sample demonstrates how to accomplish this:

CrmService crmService = CrmServiceUtility.GetCrmService("ServerName", "OrgName");

QueryExpression query = new QueryExpression();
query.EntityName = EntityName.opportunity.ToString();

ColumnSet opportunityCols = new ColumnSet();
opportunityCols.AddColumns("estimatedvalue_base");

query.ColumnSet = opportunityCols;

ConditionExpression stateCondition = new ConditionExpression();
stateCondition.AttributeName = "statecode";
stateCondition.Operator = ConditionOperator.Equal;
stateCondition.Values = new object[] { 0 };

WhoAmIRequest whoAmIRequest = new WhoAmIRequest();
WhoAmIResponse whoAmIResponse = (WhoAmIResponse)crmService.Execute(whoAmIRequest);

Guid userId = whoAmIResponse.UserId;

ConditionExpression ownerCondition = new ConditionExpression();
ownerCondition.AttributeName = "ownerid";
ownerCondition.Operator = ConditionOperator.Equal;
ownerCondition.Values = new object[] { userId };

FilterExpression filter = new FilterExpression();
filter.FilterOperator = LogicalOperator.And;
filter.Conditions.Add(stateCondition);
filter.Conditions.Add(ownerCondition);

query.Criteria = filter;

BusinessEntityCollection bec = crmService.RetrieveMultiple(query);

decimal totalEstimatedValue = 0.0m;

foreach (BusinessEntity be in bec.BusinessEntities)
{
    opportunity crmOpportunity = (opportunity)be;

    totalEstimatedValue += crmOpportunity.estimatedvalue_base.Value;
}

The preceding example is pretty straightforward. You can see how we use the Opportunity entity’s estimatedvalue_base field to calculate our estimated revenue in the base currency. Using this base field saves us from having to retrieve the exchange rate and do the conversion ourselves.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset