Enterprise Resource Planning Blog Posts by SAP
Get insights and updates about cloud ERP and RISE with SAP, SAP S/4HANA and SAP S/4HANA Cloud, and more enterprise management capabilities with SAP blog posts.
cancel
Showing results for 
Search instead for 
Did you mean: 
CE6
Product and Topic Expert
Product and Topic Expert
3,734

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:

CustomFields.png

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:

BAPIs.png

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.

head.png

item.png

 

Was this blog post helpful for you? Please share your ideas and feedback, they are very much appreciated for future blog posts.

2 Comments
krishna_ABAP
Discoverer
0 Kudos

Thanks a lot, 

It was simple to understand and to be used, 

I have  a same requirement but need to update custom fields using "BAPI_SALESORDER_CHANGE " which were added through CFL.
When i tried to update the fields in header was able to do it, but when trying to do for the line item level its not working, I have checked bape_sdsalesdocitem  structure but this does not have any update flag, I would like to update only custom field using change bapi, Your input will be helpful.

Thanks in advance

Krishna 

CE6
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi @krishna_ABAP ,
I just tested in our system and it was working as expected with the following coding:

Maybe you missed to add the sales order number to the variable lr_item_ext->key ?

 

REPORT zsd_bapi_extensibility.

DATA lo_bapi_mapping TYPE REF TO if_cfd_bapi_mapping.
DATA lt_item         TYPE STANDARD TABLE OF bapisditm.
DATA lt_itemx        TYPE STANDARD TABLE OF bapisditmx.
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.
DATA lt_return       TYPE STANDARD TABLE OF bapiret2.
DATA ls_order_header_inx TYPE bapisdh1x.


CLEAR ls_order_header_inx.
ls_order_header_inx-updateflag = 'U'.

" Fill header custom fields
CREATE DATA lr_header_ext.
lr_header_ext->data-yy1_test_head_text_sdh = 'Hell2'.
lr_header_ext->datax-yy1_test_head_text_sdh = 'X'.

" Do header custom fields mapping
lo_bapi_mapping = cl_cfd_bapi_mapping=>get_instance( ).

TRY.
    lo_bapi_mapping->map_to_bapiparex_single( EXPORTING ir_source_structure = lr_header_ext
                                              CHANGING  ct_bapiparex        = lt_bapiparex ).
  CATCH cx_cfd_bapi_mapping INTO DATA(lx_cfd_bapi_mapping_error).

ENDTRY.

" Fill Item Fields
" You cannot fill item custom fields here!
lt_item  = VALUE #( ( itm_number = '000010' )
                    ( itm_number = '000020' ) ).
lt_itemx = VALUE #( ( updateflag = 'I' itm_number = '000010' )
                    ( updateflag = 'I' itm_number = '000020' ) ).

" Fill item custom fields
CREATE DATA lr_item_ext.
lr_item_ext->data-yy1_test_item_text_sdi = 'TESTY'.
lr_item_ext->datax-yy1_test_item_text_sdi = 'X'.
lr_item_ext->key = '0000022600000010'.
INSERT lr_item_ext->* INTO TABLE lt_item_ext.

CREATE DATA lr_item_ext.
lr_item_ext->data-yy1_test_item_text_sdi = 'TESTX'.
lr_item_ext->datax-yy1_test_item_text_sdi = 'X'.
lr_item_ext->key = '0000022600000020'.
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).

TRY.
    lo_bapi_mapping->map_to_bapiparex_multi( EXPORTING ir_source_table = ltr_item_ext
                                             CHANGING  ct_bapiparex    = lt_bapiparex ).
  CATCH cx_cfd_bapi_mapping INTO lx_cfd_bapi_mapping_error.

ENDTRY.

CALL FUNCTION 'BAPI_SALESORDER_CHANGE'
  EXPORTING
    salesdocument    = '0000022600'
    order_header_inx = ls_order_header_inx
  TABLES
    return           = lt_return
    order_item_in    = lt_item
    order_item_inx   = lt_itemx
    extensionin      = lt_bapiparex.

" Commit Work
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.

 

If it is not working for you, please post your coding here.
best regards,
Christian