
Hi,
In this blog we will see how BAPI_CONTRACT_CHANGE can be used to update custom fields in Contract (Table EKKO). We have developed a small program to update contract related custom field in header. Program will read data from excel file and populate internal table containing two columns, Contract No (EBELN) and Custom Level (ZZLEVEL2) which is custom field.
In this blog I'll bypass the excel reading functionality using standard FM and only concentrate on how to pass custom field values to the BAPI. Same concept can be followed while creating contract using BAPI_CONTRACT_CREATE as well to save custom field value during contract creation.
The input excel file contains two columns Contract (EBELN) and ZZLEVEL2 and after reading excel sheet, internal table LT_CONTRACT_EXCEL contains all the contract list to be updated.
1. First we need to enhance EKKO CI include CI_EKKODB and CI_EKKODBX. In the below image, only header field ZZLEVEL2 is related to contract and other custom fields are related to PO. As you can see, there are many non-CHAR type custom fields already present in the CI_EKKODB.
CI_EKKODBX contains the BAPI update indicator fields and total number of fields should be in sync with CI_EKKODB.
Couple of points we need to remember here.
2. Populate the extension tables with this custom field value and pass to BAPI BAPI_CONTRACT_CHNAGE parameter.
DATA:
lt_extension TYPE STANDARD TABLE OF bapiparex,
ls_ext TYPE bapiparex,
ls_headx TYPE bapi_te_meoutheaderx,
lv_contract TYPE ebeln.
LOOP AT LT_CONTRACT_EXCEL INTO DATA(ls_contract).
CLEAR: ls_ext.
ls_ext-structure = 'BAPI_TE_MEOUTHEADER'.
ls_ext-valuepart1 = ls_contract-ebeln && '|' && ls_contract-zzlevel2.
APPEND ls_ext TO lt_extension.
CLEAR: ls_ext.
ls_headx-number = ls_contract-ebeln.
ls_headx-zzlevel2 = 'X'.
ls_ext-structure = 'BAPI_TE_MEOUTHEADERX'.
ls_ext-valuepart1 = ls_headx.
APPEND ls_ext TO lt_extension.
CALL FUNCTION 'BAPI_CONTRACT_CHANGE'
EXPORTING
purchasingdocument = lv_contract
TABLES
extensionin = lt_extension
return = lt_return
EXCEPTIONS
OTHERS = 01.
*&&-- Check retrun table and if there is no error, do commit.
Clear: lv_contract, ls_contract, ls_headx.
ENDLOOP.
3. Implement BADI MEOUT_BAPI_CUST and write below code in method MAP2I_EXTENSIONIN. This BADI is required when you have non char type fields in the CI_EKKODB.
METHOD if_ex_meout_bapi_cust~map2i_extensionin.
DATA: ls_conheader TYPE bapi_te_meoutheader,
lv_ebeln TYPE ebeln,
lv_level2 TYPE char255.
IF sy-cprog = 'ZMM_UPLOAD_TAXONOMY'.
CASE im_name.
WHEN 'CI_EKKODB'.
*&&-- As this contains non character field, we need to manually map it to the BAPI_TE_MEOUTHEADER (here ch_struc)
*&&-- based on the string populated in field1 of EXTENSIONIN. This will be triggered from BAPI_CONTRACT_CHANGE
ls_conheader = ch_struc.
SPLIT im_container AT '|' INTO lv_ebeln lv_level2.
ls_conheader-number = lv_ebeln.
ls_conheader-zzlevel2 = lv_level2.
ch_struc = ls_conheader.
WHEN 'CI_EKKODBX'.
**&&-- No action needed because in this structure all are char field so mapping will be done by standard code automatically
ENDCASE.
ENDIF.
ENDMETHOD.
4. If CI_EKKODB had all the character fields, then this BADI is not required as SAP standard code will take care of the mapping. In that case the code would have looked like below.
DATA:
lt_extension TYPE STANDARD TABLE OF bapiparex,
ls_ext TYPE bapiparex,
ls_headx TYPE bapi_te_meoutheaderx,
lv_contract TYPE ebeln,
ls_head TYPE bapi_te_meoutheader,
lv_string TYPE string.
LOOP AT LT_CONTRACT_EXCEL INTO DATA(ls_contract).
CLEAR: ls_ext.
ls_head-number = ls_contract-ebeln.
ls_head-zzlevel2 = ls_contract-zzlevel2.
lv_string = ls_head.
CLEAR: ls_ext.
SHIFT lv_string RIGHT BY 30 PLACES.
ls_ext = lv_string.
ls_ext-structure = 'BAPI_TE_MEOUTHEADER'.
APPEND ls_ext TO lt_extension.
CLEAR: ls_ext.
ls_headx-number = ls_contract-ebeln.
ls_headx-zzlevel2 = 'X'.
ls_ext-structure = 'BAPI_TE_MEOUTHEADERX'.
ls_ext-valuepart1 = ls_headx.
APPEND ls_ext TO lt_extension.
CALL FUNCTION 'BAPI_CONTRACT_CHANGE'
EXPORTING
purchasingdocument = lv_contract
TABLES
extensionin = lt_extension
return = lt_return
EXCEPTIONS
OTHERS = 01.
*&&-- Check retrun table and if there is no error, do commit.
Clear: lv_contract, ls_contract, ls_head, ls_headx, lv_string.
ENDLOOP.
LS_EXT structure looks like below. So when we pass LV_STRING to LS_EXT, it will place required values at correct position.
5. Using this BAPI we can update custom field at item level as well. For that, include structures would be CI_EKPODB and CI_EKPODBX. Also EXTENSIONIN table required to be populated with additional entries with BAPI_TE_MEOUTITEM and BAPI_TE_MEOUTITEMX as LS_EXT-STRUCTURE field value.
This BAPI comes with a good documentation which you can refer to identify structure names to be considered while populating EXTENSIONIN table parameter of BAPI.
6. Same concept can be used for BAPI_PO_CHANGE and BAPI_PR_CHANGE. Associated BADIs are ME_BAPI_PO_CUST and ME_BAPI_PR_CUST in case you have non char fields in PO and PR.
Please let me know your feedback and I'll try to add more information if required.
Thank you,
Sugato
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
9 | |
8 | |
6 | |
5 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 |