📜 ⬆️ ⬇️

Using external web services in 1C on the example of downloading currency rates

I know that the habre is not very much favor long-suffering 1C. Although, with the release of platform 8.3 (with clients under Linux), they began to love it a little more. By the way, just recently, the interface of one of the main developments of 1C - configuration Management of a manufacturing enterprise - was fully translated into English. Many times I met questions about why they don’t write about 1C here. The answer to them is quite obvious - there are many specialized resources where you can quickly discuss all the issues and read something.

There is every reason to believe that this article will not survive here, but I still risk it, because in 1C there are some interesting things that are worth telling.

For some time now, the opportunity to use web services has appeared in 1C 8.x: 1C can act as both a supplier and a consumer. In this article, I will show how to use 1C as a consumer using the example of obtaining exchange rates from a CBR server.

Web service


The Central Bank has a web service for receiving daily data: exchange rates, news, exchange rates, etc. Service description can be found here http://www.cbr.ru/scripts/Root.asp?Prtid=DWS . We are interested in one of the methods of this service: GetCursOnDate (On_date) - getting currency rates for a given date. One On_date argument is passed to the method - this is the date on which you want to get the courses. As a result, XML is returned that contains the ValuteCursOnDate table (the courses themselves and related information).
')

Configuration


For development, I took 1C 8.2 (8.2.15.317 in my case) and created an empty configuration. There is a WS-link object for using external web services, but it is not necessary to use it, the service can be accessed dynamically from code. I will use the first option and then show how you can use the second. In the configuration, created the processing and called it "LoadingCursesValutSBR." Added a form (managed) and made it basic. On the form, I created the details and placed the controls as shown in the figure.



Now the most important thing is to create a link to the web service description. In the configuration, add a new object of type WS-link. In the window that appears, specify the link to WSDL (the description of this format is beyond the scope of the article, you can read about it on Wikipedia ): http://www.cbr.ru/DailyInfoWebServ/DailyInfo.asmx?WSDL .



1C, based on the received description, will automatically create a visual map of the web service. You can see the name of the web service, see what operations are available to it, as well as the data types used.



The configuration on this is almost complete, it remains to make a couple of strokes to make our application look more aesthetically pleasing. Click the right mouse button on the configuration root and call the "Open command interface of the desktop" menu. In the window that appears, you must uncheck the "Visibility" flag opposite the processing of "Loading CBR exchange rates". Click OK. Next, another right click on the root of the configuration and call the menu "Open the workspace of the desktop", there we will make the setting as in the figure:



These settings will allow us to display the processing form right on the desktop (meaning the desktop of the 1C program) in the 1C Enterprise mode.

Programming


Now it remains to fill in the meaning of our processing: to make it receive exchange rates and display it in a table on a form. In the form editing mode, you need to add a new form command, let's call it the Load Currency. This command must be associated with a button located on the form. Fill the action for the command with the following code (author's comment: wow, there is a 1C code highlighting on the habr, though it doesn't work correctly):

&  ()   ()  ("   !", .); ; ; .(); ();  

Here it is first checked whether the date is filled (if not filled, then we inform the user about it and do nothing else). Then the table on the form is cleared and the LoadCursesCurrency () procedure is called, to which the date is passed.

Procedure code DownloadCoursesCurrency (), explanations given in the comments to the code:

  () //      -, //    URI  ,  ,  .  = WS.CBR_DailyInfoWebServ.WS( "http://web.cbr.ru/", "DailyInfo", "DailyInfoSoap"); //  ,     GetCursOnDate. WS = .XDTO..( "http://web.cbr.ru/").("GetCursOnDate"); //         On_Date. WS = .XDTO.(WS); WS.On_Date = ; //  -,     .  = .GetCursOnDate(WS); //  ValuteCursOnDate,    //      (   ).     .GetCursOnDateResult.diffgram.ValuteData.ValuteCursOnDate   = .(); . = .Vname; . = .Vnom; . = .Vcode; . = .VChCode; . = .Vcurs; ;  

Now you can update the configuration of the database (F7) and run the 1C Enterprise (F5). If everything is done correctly, you should see a window like in the image below:



To check the result, we need to enter the date on which we want to receive exchange rates and click on the “Load currencies” button. In case of a successful request, the table on the form will be filled with the course values:



Finally, I want to show how you can dynamically access an external web service, that is, without adding a WS-link object. Thus, we can use such web services from external processing without reference to the configuration.

In the procedure DownloadCurrenciesCurrency () line

  = WS.CBR_DailyInfoWebServ.WS("http://web.cbr.ru/", "DailyInfo", "DailyInfoSoap"); 

must be replaced by the following two lines

  =  WS("http://www.cbr.ru/DailyInfoWebServ/DailyInfo.asmx?WSDL");  =  WS(, "http://web.cbr.ru/", "DailyInfo", "DailyInfoSoap"); 

First we create the so-called definitions for the web service from its WSDL. Then we also create a proxy to access it.

As you can see, using external web services from 1C as a whole is quite simple (although there is some difficulty in understanding the definition of types, including myself).

If this publication finds a response here, then there are a few more topics that you can talk about.

Under the link you can download the configuration with an example (cf-file).

Source: https://habr.com/ru/post/148658/


All Articles