Monday, March 12, 2012

Consume Web Service from SAP

This is show a step by step guide how to invoke a web service from SAP. There are three steps you need to follow.
  1. Create proxy object
  2. Create logical port
  3. Create program to consume created proxy object
Prerequisites: You need to have your webservice URL ready Obviously! :P Check the webservice URL using IE to check the attributes and methods. This will give you a understanding of the behavior of the service.

I will explain each step with more detail.

1. Create Proxy Object

We need to create proxy object in SAP for the web-service URL you are going to invoke.
eg URL: http://<IP of the host>:8090/<Namespace>/<Service Name>.asmx?WSDL

Go to se80>select package>right click>Create>Enterprise Services/Web Services>Proxy Object














Select URL/HTTP Destination option and type in the web service URL and click ok. Give the Class name.









If the URL is available and without any errors, proxy object will be generated.

You can check the created class using transaction se24. Select the method and it's parameters you are going to access. This will be needed to create data objects at 3rd step.

2. Create logical port 

Go to transaction "lpconfig" and create logical port with the desired name.






Hit the create button.

Give the webservice URL in call parameter tab as below.













3. Create program

Proxy object and port are created, now we write a program to access methods of the created proxy class. You need to have a understanding of basic ABAP OO concepts to develop this program.

Using se24, check the structure and parameters of the method of proxy class.

Basic structure of the program is as below:

DATA: lo_send_mail  TYPE REF TO zproxy_class.
ls_input TYPE <check_se24>, 
ls_output TYPE <check_se24>. 

*create object: Port name = Port created in step 2
TRY.
    CREATE OBJECT lo_send_mail
      EXPORTING
        logical_port_name  = <PORT_NAME>.

  CATCH cx_ai_system_fault .
ENDTRY.

This section of the code should include the logic to fill the ls_input or any other parameters that need to pass to the proxy object method.

*call desired method with correct parameters
TRY.

    CALL METHOD lo_send_mail-><Method name from se24>
          EXPORTING
            input  = ls_input
          IMPORTING
            output = ls_output.
    CATCH cx_ai_system_fault INTO lo_error.
    CATCH cx_ai_application_fault.

ENDTRY.

4 comments:

  1. good one.
    few things to note;
    If your consuming a web service from another system ( .net , java ) make sure all the parameters are defined and it does not have any dynamic parameters coz if you have any dynamic parameters you wont be able to created the proxy object.


    plus the logical port is a client dependent thing the port needs to be created on all clients.

    ReplyDelete
  2. Very helpful information. Thank you Nafran!

    ReplyDelete
  3. nice post very useful thanks for sharing from Swathi

    ReplyDelete