Vous êtes sur la page 1sur 9

INTEGRATE

Microsoft Dynamics AX
®

Consuming external web


services in Microsoft
Dynamics AX 2009
White Paper

Date: April, 2009

www.microsoft.com/dynamics/ax
Table of Contents
Introduction ................................................................................................ 3

Connecting Microsoft Dynamics AX 2009 to the web service....................... 3


Creating a new service reference in AOT .................................................................................. 3
Create a new class in AOT ...................................................................................................... 4

Using the web service in the application ..................................................... 5


How to add a new button to the currency form. ........................................................................ 5
Using the newly created currency button to get the currency conversion rate for the selected
currency .............................................................................................................................. 7

Additional information ................................................................................ 8

CONSUMING EXTERNAL WEB SERVICES IN DYNAMICS AX 2009


Introduction

With Microsoft Dynamics® AX 2009, external web services usage has been made easier
for developers. By using external web services, Microsoft Dynamics AX 2009 can be
made open to a broader range of applications as well as leveraging existing functionality
that is provided by hundreds of suppliers and vendors. Some of the web services may be
used free of charge, whereas other web services have different licensing models.

This white paper shows the usage of a free web service that enables the automation of
getting currency exchange rates. The web service is free of charge and hosted by a
German vendor.

Connecting Microsoft Dynamics AX 2009 to the web


service

Creating a new service reference in AOT

The first step when integrating external web services is to add a new service reference in
the Application Object Tree (AOT).

1. Logon as a user that has access to the development environment. Make sure you
have a connection to the internet and then do as follows.
2. Open the AOT and navigate to the References node.
3. Right click on References node and select “Add service reference”.

CONSUMING EXTERNAL WEB SERVICES IN DYNAMICS AX 2009


4. In the window that opens, specify the URL location of the web service.
5. In the “Add service reference” window, enter the following values:
WSDL URL: http://www.currencyserver.de/webservice
.NET code namespace: daeNetCurrencyService
Reference name: daeNetCurrencyService

For more information about adding a service reference in AOT, see


http://msdn.microsoft.com/en-us/library/cc636424.aspx

Create a new class in AOT

1. Open the AOT and navigate to the Classes node.


2. Right click on Classes and select “New class”. The editor window will open.
3. Name the class: daenetCurrencyConverterTest.
4. Create a new method called “getRate” in the class and enter the following code:

public static server Exchrate getRate( str _fromCurrency, str _toCurrency,


EUROTriangulation _EUROTriangulation = NoYes::No)
{

daeNetCurrencyService.CurrencyServerWebServiceSoapClient soapClient;

ExchRate conversionrate;
;
try
{
new InteropPermission(InteropKind::ClrInterop).assert();

// Create the search request.


soapClient = new
daeNetCurrencyService.CurrencyServerWebServiceSoapClient("CurrencyServerWebServiceSoap");

// If the currency is triangulated, the from and to currencies must be switched


if (_EUROTriangulation)
conversionrate = soapClient.getCurrencyValue("4",_toCurrency,_fromCurrency);
else

CONSUMING EXTERNAL WEB SERVICES IN DYNAMICS AX 2009


conversionrate = soapClient.getCurrencyValue("4",_fromCurrency,_toCurrency);

CodeAccessPermission::revertAssert();

// return the found currency conversion rate


return conversionrate;

}
catch(Exception::CLRError)
{
throw error(AifUtil::getClrErrorMessage());
}
}

5. Create a new method in the class called “main” and enter the following code:

public static void main(Args _args)


{
Exchrate searchResults;
int i;
;

// Replace the currencies with the currencies you want to search for
searchResults = daenetCurrencyConverterTest::getRate("USD","EUR",NoYes::Yes);

// Display the search results.


info(strfmt("%1",searchResults * LedgerParameters::find().ExpressExchRate));
}

Using the web service in the application


The newly created class and service reference can now be used in the application.
However, a new button must be created in the currency form. This button will use the
new class to get the currency conversion rate for a selected currency.

How to add a new button to the currency form.

1. Open the AOT and navigate to the Forms node, locate the form named Currency and
expand the following node:
Designs/Design/[Group:GroupCurrency]/[ButtonGroup:Upper]
2. Right click on [ButtonGroup:Upper],select “New control/MenuButton”, and open the
properties window for the new button.

CONSUMING EXTERNAL WEB SERVICES IN DYNAMICS AX 2009


3. Name the new button GetExchrateWebService.
4. Change the text property to “Get current exchange rate”.
5. Right-click on the methods node of the newly created button, and select “Override
method/clicked”.
6. Modify the clicked method as follows:

void clicked()
{
ExchRate exchRate;
ExchRates localExchRates;
EUROTriangulation euroTriangulation;

;
super();

// only if the selected currency is not equal company currency


if (Currency.CurrencyCode != CompanyInfo::find().CurrencyCode)
{
// Cannot use a find method from ExchRates, as they never search on the exact date
select forupdate localExchRates where localExchRates.CurrencyCode ==
currency.CurrencyCode && localExchRates.FromDate == systemdateget();
// Check if the company uses a triangulation currency. If yes, exchange rates should
be inserted using triangulation flag

euroTriangulation = (CompanyInfo::find().EuroCurrencyCode!="" ? NoYes::Yes :


NoYes::No);

// Check for update/insert


if (localExchRates.RecId)
{
if (Box::yesNo(strfmt("Exchange rate for %1 already exists.
Overwrite?",systemdateget()),DialogButton::Yes) ==
DialogButton::Yes)

CONSUMING EXTERNAL WEB SERVICES IN DYNAMICS AX 2009


{
ttsbegin;

// Get the current Exchange rate via web service


exchRate = daenetCurrencyConverterTest::getRate(Currency.CurrencyCode,
CompanyInfo::find().CurrencyCode,euroTriangulation);

localExchRates.ExchRate = exchrate * LedgerParameters::find().ExpressExchRate;


localExchRates.Triangulation = euroTriangulation;
localExchrates.update();
ExchRates_ds.research();
ttscommit;
}
}
else
{
// Get the current Exchange rate via web service
exchRate = daenetCurrencyConverterTest::getRate(Currency.CurrencyCode,
CompanyInfo::find().CurrencyCode,euroTriangulation);

localExchRates.initValue();
localExchRates.CurrencyCode = Currency.CurrencyCode;
localExchRates.FromDate = systemdateget();
localExchRates.ExchRate = exchrate * LedgerParameters::find().ExpressExchRate;
localExchRates.Triangulation = euroTriangulation;
localExchrates.insert();
ExchRates_ds.research();
}
}
}

7. Close the editor window, save and compile the changed form.

Using the newly created currency button to get the currency


conversion rate for the selected currency

1. Open the modified currency form. It now displays the new button Get current
exchange rate

CONSUMING EXTERNAL WEB SERVICES IN DYNAMICS AX 2009


2. Click on the Get current exchange rate button. If there is already an exchange
rate defined for the selected currency and system date, you will be prompted if you
want to overwrite the existing exchange rate. The currency conversion rate that is
fetched by the web service will be inserted with the current system date.

Additional information

For more information about consuming web services in Microsoft Dynamics AX 2009, see
MSDN: http://msdn.microsoft.com/en-us/library/cc654149.aspx

The following internet sites provide a search interface for web services and are examples
of search providers that will enable you to search for web services:

http://seekda.com/browse (University of Innsbruck Spin-Off)


http://demo.service-finder.eu/search (Consortium including seekda site from above and
University of Sheffield)

CONSUMING EXTERNAL WEB SERVICES IN DYNAMICS AX 2009


Microsoft Dynamics is a line of integrated, adaptable business management solutions that enables you and your
people to make business decisions with greater confidence. Microsoft Dynamics works like and with familiar
Microsoft software, automating and streamlining financial, customer relationship and supply chain processes in a
way that helps you drive business success.

U.S. and Canada Toll Free 1-888-477-7989


Worldwide +1-701-281-6500
www.microsoft.com/dynamics

The information contained in this document represents the current view of Microsoft Corporation on the issues discussed as of the
date of publication. Because Microsoft must respond to changing market conditions, this document should not be interpreted to be a
commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the date of
publication.
This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED, OR STATUTORY, AS
TO THE INFORMATION IN THIS DOCUMENT.
Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of
this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means
(electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of
Microsoft Corporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject
matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this
document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

© 2009 Microsoft Corporation. All rights reserved.


Microsoft, the Microsoft Dynamics Logo, FRx, Microsoft Dynamics, SharePoint, Visual Basic, Visual Studio, Windows, and Windows
Server are either registered trademarks or trademarks of Microsoft Corporation, FRx Software Corporation, or Microsoft Business
Solutions ApS in the United States and/or other countries. Microsoft Business Solutions ApS and FRx Software Corporation are
subsidiaries of Microsoft Corporation.

CONSUMING EXTERNAL WEB SERVICES IN DYNAMICS AX 2009

Vous aimerez peut-être aussi