Skip to content

Base Currency Format String in CRM 2011

October 24, 2011

EuroOne more utility about getting number formats in CRM 2011.

In this case the function below (in conjunction with the GetNumberFormatString function described in this post here), will return the format string for currency for the organization.

Note that this is the format for the base currency of the organization. Other cases can be derived quite simply from this example.

using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace Test
{
    class Test
    {
        public static string GetBaseCurrencyFormat(CodeActivityContext executionContext)
        {
            IWorkflowContext workflowContext = executionContext.GetExtension();
            Guid orgId = workflowContext.OrganizationId;
            IOrganizationServiceFactory svcFactory = executionContext.GetExtension();
            IOrganizationService svc = svcFactory.CreateOrganizationService(workflowContext.UserId);

            ColumnSet colSet = new ColumnSet("basecurrencyid", "currencydisplayoption", "currencyformatcode",
                "negativecurrencyformatcode", "numbergroupformat");
            Entity organization = svc.Retrieve("organization", orgId, colSet);

            Guid baseCurrencyId = ((EntityReference)organization.Attributes["basecurrencyid"]).Id;
            int currencyDisplayOption = ((OptionSetValue)organization.Attributes["currencydisplayoption"]).Value;
            int currencyFormatCode = ((OptionSetValue)organization.Attributes["currencyformatcode"]).Value;
            int negativeCurrencyFormatCode = (int)organization.Attributes["negativecurrencyformatcode"];
            string numberGroupFormat = (string)organization.Attributes["numbergroupformat"];

            colSet = new ColumnSet("currencyprecision", "currencysymbol", "isocurrencycode");
            Entity currency = svc.Retrieve("transactioncurrency", baseCurrencyId, colSet);

            int currencyPrecision = (int)currency.Attributes["currencyprecision"];
            string currencySymbol = (string)currency.Attributes["currencysymbol"];
            string isoCurrencyCode = (string)currency.Attributes["isocurrencycode"];

            return GetNumberFormatString(currencyPrecision, numberGroupFormat, negativeCurrencyFormatCode, true,
                currencyDisplayOption == 0 ? currencySymbol : isoCurrencyCode, currencyFormatCode);
        }
        (...)

Happy coding!

Alberto “Form-idable” Gemin

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: