
If you integrate SAP S/4HANA Cloud with an SAP on-premise solution and APIs don't work for your specific scenario, you can use a limited set of Business Application Programming Interfaces (BAPIs).
For more information about using BAPIs, see this blog post.
You can find a list of all supported BAPIs in this SAP Note.
In this blog post, we explain how you can use custom fields in BAPIs. In our example, we use BAPI_SALESORDER_CREATEFROMDAT2, a BAPI used for creating sales orders.
In the Custom Fields app, you start by creating custom fields in the Sales: Sales Document and Sales: Sales Document Item business contexts. If you're not yet familiar with custom fields and key user extensibility in general, we recommend that you check out this blog post first.
For our scenario, we create the following two custom fields:
In the details of the custom fields, on the BAPIs tab, you can see the BAPIs for which the usage of the respective custom field is automatically enabled. For example, the following BAPIs use our custom field on sales document level:
All these BAPIs have an EXTENSIONIN parameter of type BAPIPAREX that you can use for custom fields.
To do so, you first need a mapping of the custom fields. For this purpose, you can use the CL_CFD_BAPI_MAPPING class.
To create a sales order with custom fields using BAPI_SALESORDER_CREATEFROMDAT2, you can, for example, create a report like the following simple example report in transaction SE80:
report zsd_bapi_extensibility.
data lo_bapi_mapping type ref to if_cfd_bapi_mapping.
data lv_salesorder type bapivbeln-vbeln.
data ls_header type bapisdhd1.
data lt_partner type standard table of bapiparnr.
data lt_item type standard table of bapisditm.
data lt_itemx type standard table of bapisditmx.
data lt_sline type standard table of bapischdl.
data lt_slinex type standard table of bapischdlx.
data lt_returncreate type standard table of bapiret2.
data lr_header_ext type ref to bape_sdsalesdoc.
data lr_item_ext type ref to bape_sdsalesdocitem.
data lt_item_ext type standard table of bape_sdsalesdocitem.
data lt_bapiparex type bapiparextab.
" Fill all needed Standard Header Fields
" You cannot fill header custom fields here!
ls_header = VALUE #( doc_type = 'TA' sales_org = '1010' distr_chan = '10' division = '00' ).
" Fill Partner fields
lt_partner = VALUE #( ( partn_role = 'AG' partn_numb = '0010100001' ) ).
" Fill Item Fields
" You cannot fill item custom fields here!
lt_item = VALUE #( ( itm_number = '000010' material = 'TG11' )
( itm_number = '000020' material = 'TG11' ) ).
lt_itemx = VALUE #( ( updateflag = 'I' itm_number = '000010' material = 'X' )
( updateflag = 'I' itm_number = '000020' material = 'X' ) ).
" Fill Schedule Line Fields
lt_sline = VALUE #( ( itm_number = '000010' sched_line = '0001' req_qty = '1' )
( itm_number = '000020' sched_line = '0001' req_qty = '2' ) ).
lt_slinex = VALUE #( ( updateflag = 'X' itm_number = '000010' sched_line = '0001' req_qty = 'X' )
( updateflag = 'X' itm_number = '000020' sched_line = '0001' req_qty = 'X' ) ).
" Fill header custom fields
create data lr_header_ext.
lr_header_ext->data-yy1_test_head_text_sdh = 'Hello'.
lr_header_ext->datax-yy1_test_head_text_sdh = 'X'.
" Do header custom fields mapping
lo_bapi_mapping = cl_cfd_bapi_mapping=>get_instance( ).
lo_bapi_mapping->map_to_bapiparex_single( exporting ir_source_structure = lr_header_ext
changing ct_bapiparex = lt_bapiparex ).
" Fill item custom fields
create data lr_item_ext.
lr_item_ext->data-yy1_test_item_text_sdi = 'ITM10'.
lr_item_ext->datax-yy1_test_item_text_sdi = 'X'.
lr_item_ext->key = '0000000000000010'.
insert lr_item_ext->* into table lt_item_ext.
create data lr_item_ext.
lr_item_ext->data-yy1_test_item_text_sdi = 'ITM20'.
lr_item_ext->datax-yy1_test_item_text_sdi = 'X'.
lr_item_ext->key = '0000000000000020'.
insert lr_item_ext->* into table lt_item_ext.
" Do item custom fields mapping
get reference of lt_item_ext into data(ltr_item_ext).
lo_bapi_mapping->map_to_bapiparex_multi( exporting ir_source_table = ltr_item_ext
changing ct_bapiparex = lt_bapiparex ).
" Call BAPI
call function 'BAPI_SALESORDER_CREATEFROMDAT2'
exporting order_header_in = ls_header
importing salesdocument = lv_salesorder
tables return = lt_returncreate
order_items_in = lt_item
order_items_inx = lt_itemx
order_partners = lt_partner
order_schedules_in = lt_sline
order_schedules_inx = lt_slinex
extensionin = lt_bapiparex.
write: 'Sales Order:', lv_salesorder.
" Commit Work
call function 'BAPI_TRANSACTION_COMMIT'.
To check if the values of your custom fields were saved, you can add them to the UI of an app like Manage Sales Orders - Version 2. This blog post explains how you can do this.
Was this blog post helpful for you? Please share your ideas and feedback, they are very much appreciated for future blog posts.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
31 | |
5 | |
4 | |
3 | |
3 | |
3 | |
2 | |
2 | |
2 | |
2 |