How to update the daily exchange rate by consuming web services from any bank.
Financial management users have the daily task of updating the exchange rate with respect to any foreign currency, however, this task is often done manually. In the next post, I will explain the simple way to consume a central bank web service in this case of Mexico (It works for any country) and updates the exchange rate table.
It is important to know that for it to be done automatically, you simply need to expose a Job Queue that executes this update task daily.
Below the source code:
procedure GetRateforDateBanxico()
var
Client: HttpClient;
Response: HttpResponseMessage;
ContentTxt: Text;
Root: JsonObject;
CurRate: Decimal;
CurRateTxt: text;
CurDateTxT: Text;
ExchangeRate: Record "Currency Exchange Rate";
BMSetup: Record "GetCurrfromBM Setup";
Pos: Integer;
PosD: Integer;
begin
BMSetup.Get();
if Client.Get('https://www.banxico.org.mx/SieAPIRest/service/v1/series/' BMSetup."Bank Series to Use" '/datos/oportuno?mediaType=json&token=' BMSetup.Token, Response) then begin
if Response.IsSuccessStatusCode() then begin
if Response.Content().ReadAs(ContentTxt) then begin
if Root.ReadFrom(ContentTxt) then begin
if ContentTxt.Contains('dato":"') then begin
Pos := StrPos(ContentTxt, 'dato":"') 7;
CurRateTxt := CopyStr(ContentTxt, Pos, 7);
PosD := StrPos(ContentTxt, 'fecha":"') 8;
CurDateTxT := CopyStr(ContentTxt, PosD, 10);
if Evaluate(CurRate, CurRateTxt) then begin
ExchangeRate.Init();
ExchangeRate."Currency Code" := BMSetup."Currency Code";
ExchangeRate."Starting Date" := ConvDate(CurDateTxT);
if ExchangeRate.Insert(true) then begin
ExchangeRate.Validate("Exchange Rate Amount", 1);
ExchangeRate.Validate("Relational Exch. Rate Amount", CurRate);
ExchangeRate.Validate("Adjustment Exch. Rate Amount", 1);
ExchangeRate.Validate("Relational Adjmt Exch Rate Amt", CurRate);
ExchangeRate.Modify(true);
end;
end else
message('There´s no data to read');
end else
Error('Could not find "dato" in XML (%1)', ContentTxt);
end else
Error('Malformed XML (%1)', ContentTxt);
end else
error('server did not return')
end else begin
if Response.Content().ReadAs(ContentTxt) then
Error('Fail call with code (%1) (Info %2', Response.HttpStatusCode(), ContentTxt)
else
Error('Fail call with cod (%1)', Response.HttpStatusCode)
end;
end else
Error('Cannot connect');
end;
local procedure ConvDate(curdatetxt: text): Date
var
finaldate: Date;
begin
if Evaluate(finaldate, curdatetxt) then
exit(finaldate);
end;Source: https://community.dynamics.com/blogs/post/?postid=035a056f-415a-406c-9ef1-2b8f15a2e83e
Comentarios
Publicar un comentario