For the prupose of this article we will use an XML feed provided free by the European Central Bank (https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml). The feed does not contain all currencies, so be sure to check the list.
Create a new Console Application and paste the following code into the Main method.
Listing 1
Dictionary<string, double> rates = new Dictionary<string, double>();
using (XmlTextReader reader = new XmlTextReader("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"))
{
while (reader.Read())
{
switch (reader.Name)
{
case "Cube":
var currency = reader.GetAttribute("currency");
var rate = reader.GetAttribute("rate");
if (null != rate)
{
rates.Add(currency, Double.Parse(rate.ToString()));
}
break;
}
}
}
The code above stores all currencies provided by the feed in a rates list object. All that is required now is to connect to the company database using the DI API and call the SetCurrencyRate method as shown in Listing 2 below.
Listing 2
SAPbobsCOM.Company company = new SAPbobsCOM.Company();
company.Server = "server ip";
company.CompanyDB = "company db";
company.DbUserName = "db username";
company.DbPassword = "db password";
company.UserName = "sap user";
company.Password = "sap password";
company.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_HANADB;
int result = company.Connect();
if (result == 0)
{
SAPbobsCOM.SBObob bo = company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoBridge);
bo.SetCurrencyRate("USD", DateTime.Now, rates["USD"], true);
company.Disconnect();
}
else
{
Console.WriteLine(company.GetLastErrorDescription());
}
You can only set a curreny rate for a currency that has been added to the exchange rates for the selected company. If you attempt to set a rate for a non existing currency, you will recieve a System.Runtime.InteropServices.COMException: 'Invalid currency ' exception.
The base currency for the rates feed is EUR. If your base currency is different, you will need to convert the rates using your base currency. For example, if your base currency is GBP and you want the USD rate, you can use the formula below.
Listing 3
double rateValue = rates["USD"] / rates["GBP"];
-
Stock Transfer Part 1
The SAP B1 Service Layer provides an API to move stock between warehouses. This article explains the construction of the JSON payload data that is posted to the StockTransfers API.
12 December 2019 - 1146 views -
php-sapb1 Library Documentation v1
A simple and easy to use PHP library for SAP Business One Service Layer API.
16 August 2019 - 1882 views -
node-sapb1 Library Documentation v1
This article provides documentation on the SAPb1 NodeJs library.
01 August 2019 - 1200 views -
PHP Service Layer Example Part 2
This article is part 2 of PHP Service Layer Example. In the previous article, I explained how to initiate an authentication request to get a Service Layer session id and route id. In this article we use these details to execute a request to retrieve a list of business partners.
25 January 2018 - 4180 views -
PHP Service Layer Example Part 1
This article explains how to get connected to the Service Layer using PHP.
09 January 2018 - 5468 views -
Introduction To SAP Business One Service Layer
An introduction to the SAP Business One Service Layer.
08 December 2017 - 5138 views -
What Is SAP DI API
This article is an introduction to SAP DI API with a simple code sample.
21 June 2017 - 3100 views