以下根据系统实例做的 最终效果:
1、 SE11建立数据表,主要是用于存放附加的数据;
MANDT MANDT CLNT 3 0 客户端
MBLNR MBLNR CHAR 10 0 物料凭证编号 MJAHR MJAHR NUMC 4 0 物料凭证年度 ZEILE MBLPO NUMC 4 0 物料凭证中的项目
BADI_ERFMG ERFMG QUAN 13 3 以输入单位计的数量 BADI_ERFME ERFME UNIT 3 0 条目单位
2、 建立结构,在屏幕数据交换时使用
3、 建立表类型
设置关键字段:
4、 SE80建立函数组
创建屏幕1000,
设计格式,插入附加字段
创建function: ZJIM_BADI_EXAMPLE_GET_DATA
*\------
*\ *\
*\ *\
*\------
* Get all data from fields of external screen * First move all customer-fields
MOVE-CORRESPONDING zjim_badi_exampl TO es_migo_badi_screen_fields.
* Second move all GOITEM-fields (displayed on external screen)
function: ZJIM_BADI_EXAMPLE_PUT_DATA
*\------
*\ *\
*\ *\
*\------
* Put all data from fields to external screen * First fill all customer-fields
MOVE-CORRESPONDING is_migo_badi_screen_fields TO ZJIM_badi_exampl.
* second fill all GOITEM-fields (displayed on external screen)
function: ZJIM_BADI_EXAMPLE_UPDATE_DATA
*\------
*\ *\
*\ *\
*\
*\------
* Databse update:
INSERT zjim_BADI_EXAMPL FROM TABLE IT_MIGO_BADI_EXAMPLE. IF sy-subrc <> 0.
MESSAGE a398(00) WITH 'Error update MIGO_BADI_EXAMPL'. ENDIF.
INCLUDE : LZJIM_BADI001TOP
TABLES : Zjim_badi_exampl.
到这里增强的准备工作就做完了,下面开始创建实现: 5、 SE19创建实现
6、 创建执行类ZCL_IM_JIM_MIGO_BADI,激活ZJIM_MIGO_BADI
双击类
维护类属性
GT_EXTDATA 用于数据交换,类型为ZJIM_TY_T_EXTDATA表类型, 7、 激活以下方法并维护:
method IF_EX_MB_MIGO_BADI~INIT.
* Regristration of BAdI-Implementation:
* Append class attribute GF_CLASS_ID (='MIGO_BADI_IMPLEMENTATION1') to
* regristration table.
APPEND gf_class_id TO ct_init. endmethod.
METHOD IF_EX_MB_MIGO_BADI~PBO_DETAIL .
DATA: ls_extdata TYPE zjim_badi_example_screen_field.
* This check is obligatory, otherwise the program flow is incorrect
* (If there would be more than one implementation of BAdI MB_MIGO_BADI,
* only one subscreen would be displayed). CHECK i_class_id = gf_class_id. * Show screen only if there is an item CHECK i_line_id <> 0. * External subscreen:
* The content of global field G_NO_INPUT (set in method MODE_SET) will
* influence the number of external subsreen: if g_no_input is initial.
e_cprog = 'SAPLZJIM_BADI001'(006). \程序名为定义的函数组的名称前加“SAPL“。
e_dynnr = '1000'. \t
e_heading = 'ZJIM'(004). \显示标签的名称
endif.
* Set G_LINE_ID (= line_id of item displayed on detail-tabstrip) g_line_id = i_line_id. * Read data
READ TABLE gt_extdata INTO ls_extdata WITH TABLE KEY line_id = i_line_id.
* Export data to function group (for display on subscreen) CALL FUNCTION 'ZJIM_BADI_EXAMPLE_PUT_DATA' EXPORTING
is_migo_badi_screen_fields = ls_extdata.
ENDMETHOD. \
METHOD IF_EX_MB_MIGO_BADI~PAI_DETAIL .
*-----------------------------------------------------------------------
* Changing parameter E_FORCE_CHANGE can be set to 'X'. In this case
* method LINE_MODIFY is called. * ATTENTION:
* DO NOT SET parameter E_FORCE_CHANGE = ' '. In this case you might
* overwrite parameter E_FORCE_CHANGE of another BAdI implementation.
*-----------------------------------------------------------------------
DATA: ls_extdata_new TYPE zjim_badi_example_screen_field, ls_extdata_old TYPE zjim_badi_example_screen_field.
* Only if a line exists CHECK i_line_id <> 0.
* Get data from external screen
CALL FUNCTION 'ZJIM_BADI_EXAMPLE_GET_DATA' IMPORTING
es_migo_badi_screen_fields = ls_extdata_new. * Compare new data with old data
READ TABLE gt_extdata INTO ls_extdata_old WITH TABLE KEY line_id = i_line_id. ls_extdata_new-line_id = i_line_id. IF ls_extdata_old <> ls_extdata_new.
* If there were any changes, it's obligatory to force MIGO to trigger
* method LINE_MODIFY.
e_force_change = 'X'. ENDIF.
ENDMETHOD. \
METHOD IF_EX_MB_MIGO_BADI~LINE_MODIFY .
DATA: ls_extdata_old TYPE zjim_badi_example_screen_field, ls_extdata_new TYPE zjim_badi_example_screen_field, ls_migo_badi_exampl TYPE zjim_badi_exampl, l_subrc TYPE sy-subrc.
* Get external data from internal table: READ TABLE gt_extdata INTO ls_extdata_old WITH TABLE KEY line_id = i_line_id. l_subrc = sy-subrc.
* Update data in internal table: IF l_subrc <> 0.
* Line is new: If GOITEM has a reference to a material document, * the already existing external data can be read. IF NOT cs_goitem-mblnr IS INITIAL AND NOT cs_goitem-mjahr IS INITIAL
AND NOT cs_goitem-zeile IS INITIAL.
SELECT SINGLE * FROM zjim_badi_exampl INTO ls_migo_badi_exampl
WHERE mblnr = cs_goitem-mblnr AND mjahr = cs_goitem-mjahr AND zeile = cs_goitem-zeile. IF sy-subrc = 0.
MOVE-CORRESPONDING ls_migo_badi_exampl TO ls_extdata_new. ENDIF. ENDIF.
* ls_extdata_new-sgtxt = cs_goitem-sgtxt. ls_extdata_new-line_id = i_line_id.
INSERT ls_extdata_new INTO TABLE gt_extdata. ELSE.
* Line exists: Get external data entered on BAdI-subscreeen, but only
* if line_modify was called for the item displayed in the
* 'detail tabstrip'. check g_line_id = i_line_id.
CALL FUNCTION 'ZJIM_BADI_EXAMPLE_GET_DATA' IMPORTING
es_migo_badi_screen_fields = ls_extdata_new. ls_extdata_new-line_id = i_line_id.
* if ls_extdata_new-sgtxt <> ls_extdata_old-sgtxt. ** Field was changed on external screen * cs_goitem-sgtxt = ls_extdata_new-sgtxt. * else.
** Take data from GOITEM
* ls_extdata_new-sgtxt = cs_goitem-sgtxt. * endif.
MODIFY TABLE gt_extdata FROM ls_extdata_new. ENDIF.
ENDMETHOD. \
METHOD IF_EX_MB_MIGO_BADI~LINE_DELETE .
DELETE TABLE gt_extdata WITH TABLE KEY line_id = i_line_id. ENDMETHOD. \
METHOD IF_EX_MB_MIGO_BADI~POST_DOCUMENT .
DATA: ls_migo_badi_example TYPE ZJIM_badi_exampl,
lt_migo_badi_example TYPE TABLE OF zjim_badi_exampl, ls_extdata TYPE zjim_badi_example_screen_field, ls_xmseg TYPE mseg.
FIELD-SYMBOLS:
* Any errors here MUST be issued as A-message (better: X-message)
* Copy data from material document into internal table LOOP AT gt_extdata INTO ls_extdata. IF g_cancel IS INITIAL.
READ TABLE it_mseg INTO ls_xmseg
WITH KEY line_id = ls_extdata-line_id. ELSE.
READ TABLE it_mseg INTO ls_xmseg WITH KEY smbln = ls_extdata-mblnr smblp = ls_extdata-zeile sjahr = ls_extdata-mjahr. ENDIF.
IF sy-subrc IS INITIAL.
MOVE-CORRESPONDING ls_extdata TO ls_migo_badi_example. MOVE-CORRESPONDING ls_xmseg TO ls_migo_badi_example. APPEND ls_migo_badi_example TO lt_migo_badi_example. ENDIF. ENDLOOP.
* The data from external detail screen can be saved now: CHECK gt_extdata IS NOT INITIAL.
CALL FUNCTION 'ZJIM_BADI_EXAMPLE_UPATE_DATA' IN UPDATE TASK TABLES
it_migo_badi_example = lt_migo_badi_example. * The data from external header screen can be saved now: * MOVE-CORRESPONDING is_mkpf TO gs_exdata_header.
* CALL FUNCTION 'MIGO_BADI_EXAMPLE_UPDATE_HEAD' IN UPDATE TASK * EXPORTING
* is_migo_badi_header_fields = gs_exdata_header.
ENDMETHOD. \
METHOD IF_EX_MB_MIGO_BADI~RESET . * Clear all internal data: CLEAR: gt_extdata, g_no_input, gs_exdata_header, g_cancel, g_line_id.
ENDMETHOD. \
激活所有,运行T_code MIGO 就可以看到第一个图的效果了。