Skip to main content

Notifications

Adding Exchange Rates using the AIF LedgerExchangeRateService Service

The following example should help if you need to add an Exchange Rate to an existing Currency Pair in Microsoft Dynamics AX 2012 R3 using the AIF LedgerExchangeRateService service.  When you create the AIF Inbound Port for this service, I recommend to enable logging under the Troubleshooting section in case you need to review the XML that is created.

 

C# Console Application Example

The existing Currency Pair (EUR to USD) already contains a couple Exchange Rates. This example will show how to add a new set of Exchange Rates.

 

 

The code below adds a new Exchange Rate to this existing Currency Pair. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using UpdateExchangeRateTester.ERService;

namespace UpdateExchangeRateTester
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeRateServiceClient proxy = new ExchangeRateServiceClient();

            CallContext cc = new CallContext();
            cc.Company = "USMF";

            // Call Read First per http://community.dynamics.com/ax/f/33/t/75714.aspx
            //Criteria will be the From and To for the CurrencyPair (EUR to USD)
            QueryCriteria qc = new QueryCriteria();
            qc.CriteriaElement = new CriteriaElement[2];
            qc.CriteriaElement[0] = new CriteriaElement();
            qc.CriteriaElement[0].FieldName = "FromCurrencyCode";
            qc.CriteriaElement[0].DataSourceName = "CurrencyPair";
            qc.CriteriaElement[0].Value1 = "EUR";
            qc.CriteriaElement[1] = new CriteriaElement();
            qc.CriteriaElement[1].FieldName = "ToCurrencyCode";
            qc.CriteriaElement[1].DataSourceName = "CurrencyPair";
            qc.CriteriaElement[1].Value1 = "USD";
            EntityKey[] keylist = proxy.findKeys(cc, qc);
            AxdLedgerExchangeRate axdExchangeRate = proxy.read(cc, keylist);


            foreach (AxdEntity_CurrencyPair currencyPair in axdExchangeRate.CurrencyPair)
            {
                currencyPair.action = AxdEnum_AxdEntityAction.update;
                currencyPair.actionSpecified = true;        
                currencyPair.RateType = null; // do not send in a CurrencyPair RateType on updates
               
                //create new exchange rate
                AxdEntity_ExchangeRate newExchangeRate = new AxdEntity_ExchangeRate();
                newExchangeRate.ExchangeRate = 95.60M; //AX value is .95600
                newExchangeRate.ExchangeRateSpecified = true;
                newExchangeRate.ValidFrom = new DateTime(2015, 2, 5);
                newExchangeRate.ValidFromSpecified = true;
                //newExchangeRate.ValidTo = new DateTime(2015, 1, 16);
                //newExchangeRate.ValidToSpecified = true;
                newExchangeRate.action = AxdEnum_AxdEntityAction.create;
                newExchangeRate.actionSpecified = true;
               
                currencyPair.ExchangeRate = new AxdEntity_ExchangeRate[1] { newExchangeRate };
            }

 

            try
            {
                proxy.update(cc, keylist, axdExchangeRate);
                Console.WriteLine("Update Call Complete");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("The AxdLedgerExchangeRate was not updated.");
            }
            Console.ReadLine();
        }
    }
}


A couple items I had to do was set the RateType to null on the AxdEntity_CurrencyPair object otherwise I was receiving an AIF Exception on it. I also did not send the existing Exchange Rate data in the AxdEntity_ExchangeRate[] collection.I just created a new AxdEntity_ExchangeRate with the action property set to create. The final result looks like:

 

 

XML Example

This is the XML document that AIF processes from the C# Console Application. You can use this as a starting point if you need to know what the XML schema should look like if you need to create XML for a File Adapter Inbound port.

<?xml version="1.0" encoding="UTF-8"?>
<ExchangeRateServiceUpdateRequest xmlns="http://schemas.microsoft.com/dynamics/2008/01/services">
  <EntityKeyList xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKeyList">
    <EntityKey xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKey">
      <KeyData>
        <KeyField>
          <Field>RecId</Field>
          <Value>22565423204</Value>
        </KeyField>
      </KeyData>
    </EntityKey>
  </EntityKeyList>
  <LedgerExchangeRate xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/LedgerExchangeRate">
    <DocPurpose>Original</DocPurpose>
    <SenderId>USMF</SenderId>
    <ValidAsOfDateTime>2015-02-05T00:00:00Z</ValidAsOfDateTime>
    <ValidFromDateTime xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></ValidFromDateTime>
    <ValidTimeStateType>AsOf</ValidTimeStateType>
    <ValidToDateTime xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></ValidToDateTime>
    <CurrencyPair class="entity" action="update">
      <_DocumentHash>911f6653fc2da5d3ce42d69fb95f7269</_DocumentHash>
      <RecId>22565423204</RecId>
      <RecVersion>1</RecVersion>
      <FromCurrencyCode>EUR</FromCurrencyCode>
      <ToCurrencyCode>USD</ToCurrencyCode>
      <ExchangeRateType>Average</ExchangeRateType>
      <ExchangeRateDisplayFactor>One</ExchangeRateDisplayFactor>
      <ExchangeRate class="entity" action="create">
        <ExchangeRate>95.60</ExchangeRate>
        <ValidFrom>2015-02-05</ValidFrom>
      </ExchangeRate>
    </CurrencyPair>
  </LedgerExchangeRate>
</ExchangeRateServiceUpdateRequest>

 

Comments

*This post is locked for comments