Introduction
The SAP system is developing and changing day by day. Sometimes keeping track of changes can be quite challenging. Perhaps the best part of this profession is that it constantly compels you to learn new things. SAP API HUB offers us many services and it is possible to find these services on the on-premise system. In this blog post, I will try to show you how to create BP using the API on-premise system. Even if this article was written only for BP; I think it will give a general idea about other services such as purchase order creation, sales order creation, etc.
STEP-BY-STEP IMPLEMENTATION
◉ In the SAP system, call the / IWFND / MAINT_SERVICE transaction code and click the ADD SERVICE button.
Add Service
◉ After giving the System Alias and External Service name, click the “ADD SELECTED SERVICE” button.
Add Selected Services
◉ Continue by giving your technical service name and desired package on the pop-up screen.
Technical Configuration
◉ You will get a success message when everything goes well. If you wish, you can try the service by clicking the “SAP GATEWAY CLIENT” button or using a 3rd party software such as Postman.
Call Gateway Client
Try out the service
◉ The part until now was only to activate the service. Now is the time to get our hands dirty. For this, we will create an ABAP program and a class. Our program will consist of username, password, and URL information at the selection screen. Sample URL: http://<YOUR_HOST_HERE>:<YOUR_PORT_HERE>/sap /opu/room/sap/ api_business_partner / A_BusinessPartner? Sap-client = 100
Selection Screen
◉ Abap class will contain two methods, the first method will receive CSRF tokens and cookies, and the second method will send a POST request.
REPORT zbp_api.
SELECTION-SCREEN BEGIN OF BLOCK bl0 WITH FRAME TITLE TEXT-t00.
SELECTION-SCREEN BEGIN OF BLOCK bl1 WITH FRAME TITLE TEXT-t01.
PARAMETERS: p_unam TYPE string LOWER CASE OBLIGATORY,
p_pass TYPE string LOWER CASE OBLIGATORY,
p_url TYPE string LOWER CASE OBLIGATORY.
SELECTION-SCREEN END OF BLOCK bl1.
SELECTION-SCREEN END OF BLOCK Bl0.
AT SELECTION-SCREEN OUTPUT.
"have a little decency and hide password field
LOOP AT SCREEN.
IF screen-name = 'P_PASS'.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
START-OF-SELECTION.
CLASS lcl_odata_tool DEFINITION.
PUBLIC SECTION.
CLASS-METHODS get_csrf_token_and_cookie
EXPORTING
!et_cookies TYPE tihttpcki
!ev_token TYPE string .
CLASS-METHODS create_opp
IMPORTING
!iv_token TYPE string
!it_cookies TYPE tihttpcki .
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS lCL_ODATA_TOOL IMPLEMENTATION.
METHOD get_csrf_token_and_cookie.
DATA: lo_http_client TYPE REF TO if_http_client,
lv_status TYPE i,
lt_fields TYPE tihttpnvp,
lv_sysubrc TYPE sysubrc.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = p_url
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
ASSERT sy-subrc = 0.
.....
.....
.....
ev_token = <field>-value.
lo_http_client->response->get_cookies( CHANGING cookies = et_cookies ).
lo_http_client->close( ).
ENDMETHOD.
METHOD create_opp.
DATA:lo_http_client TYPE REF TO if_http_client,
lv_sysubrc TYPE sysubrc,
lv_body TYPE string,
ls_bp TYPE zapi_s_bsnsp,
lt_adress TYPE TABLE OF zapi_s_adress,
lr_out TYPE REF TO data.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = p_url
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
......
......
......
cl_demo_output=>write_json( lv_json ).
cl_demo_output=>display( ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(lo_class) = NEW lcl_odata_tool( ).
lo_class->get_csrf_token_and_cookie(
IMPORTING
et_cookies = DATA(et_cookies)
ev_token = DATA(ev_token)
).
lo_class->create_opp(
EXPORTING
iv_token = ev_token
it_cookies = et_cookies
).
◉ After successfully created BP you can call BP transaction and observe your newly created BP record.
Newly Created BP
If you wish, you can access all the codes on the GITHUB page to review. I shortened codes with dots for don’t cover so much place. I have created many structures in the system, you can pull your own system with the help of the ABAPGIT program. You can see that I start with underscores for some fields in the structure I created it with that way because when I use “/ ui2 / cl_json => serialize” I want to capitalize the field after each underscore. Since Abap does not allow more than 30 characters for field names, I changed some names using REPLACE ALL OCCURRENCES OF. Make sure that the fields assigned as Hard Code, such as BusinessPartnerGrouping, CustomerAccountGroup, are customized in the same way on your system, or change these fields to customization in your system or comment them completely.
No comments:
Post a Comment