1、BAPI的使用说明中文版BAPI Step by Step ExampleAbout the exampleThis BAPI reads system status for a production order from table JEST and system status text from table TJ02T 此BAPI是从JEST读取生产订单系统状态和从表TJ02T中读取文本状态Name ZGetOrderStatus (bapi名称)Function group ZBAPISTATUS (功能组)Function module: Z_BAPI_GET_ORDER_STATUS
2、 (功能模块)Import parameters: ORDER_STATUS_IMPORT type ZBAPI_ORDER_STATUS_IMPORT: 定义左边变量类型为右 AUFNR Order number (Keyfield) SPRAS Language ExcludeInactive - Checkbox - Exclude inactive status Tables T_BAPISTAT type ZBAPISTAT: OBJNR like JEST-OBJNR STAT like JEST-STAT INACT like JEST-INACT TXT04 like TJ02
3、T-TXT04 TXT30 likeTJ02T-TXT30 Export parameters RETURN like BAPIRETURN Step1:定义BAPI用到的结构体 在此步骤里,BAPI中用到功能模块的表和参数均已在这里定义使用数据类型-结构定义以下结构:结构1:ZBAPI_ORDER_STATUS_IMPORT包含以下字段 ORDERID Order number (Keyfield) 订单号 SPRAS Language 语言 ExcludeInactive - Checkbox - Exclude inactive status 结构2:ZBAPISTAT: OBJNR l
4、ike JEST-OBJNR STAT like J_STAT INACT like J_INACT TXT04 like J_TXT04 TXT30 like J_TXT30 OBJNR like J_OBJNR Important note: You will have to define a structure for every parameter in the BAPI. You cannot use the same structures used in existing applications because BAPI structures are frozen when BA
5、PIs are released and then there are restrictions on changing them. 你必须为每个BAPI中的参数定义结构,当BAPI被释放后是限制更改他们的,因为bapi会被冻结,所以你不能在存在的应用中使用同一个结构ZBAPI_ORDER_STATUS_IMPORT 结构1 ZBAPISTAT -结构2 第二步:写入功能模块Important notes: 每个BAPI必须有他们自己的函数组(功能组) 在属性tab里记住选择存取类型是Remote Enabled module,否则功能模块不能通过RFC来invoke它做为一个BAPI来使用
6、输入/输出参数仅仅能被BY VALUE 对于RFC enabled的功能模块 我们在此仅创建了一个BAPI,但你能创建相关的BAPI在同一模块池中,使得他们能共享全局信息功能模块的属性设定输入参数 这里定义功能模块的输入参数为订单输入参数的结构,即是引入结构ZBAPI_ORDER_STATUS_IMPORT做为bapi_order_status_import的参照Export Parameters Tables Code Notes: The subroutine SET_RETURN_MESSAGE is a standard routine used for BAPIs that use
7、the BAPIRETURN structure SET_RETURN_MESSAGE子程序是在BAPI中标准的子程序用来使用BAPIRETURN 结构 在FORM Z_BAPI_GET_ORDER_SYSTEM_STATUS里有个测试是IF 1=2,如果这测试结果为真,则信息就显示,但是这个条件永远不会为真,所以我们就不可能在BAPI里显示出这个信息,为什么要包含这个的原因是,为信息创建一个参照,因此这时SAP标准控制方法,拷贝公司代码层面的代码GetList BAPI. In form Z_BAPI_GET_ORDER_SYSTEM_STATUS there is a test IF 1
8、= 2. If the test is true a message is displayed. The condition will obviously never be true, and we will never want to display a message in a BAPI. The reason why it is included is, that it create a reference for the message, so that the WHERE USED functionality can be used for the message. This is
9、the SAP standard way to handle it, copied from the Company Code GetList BAPI. INCLUDE LZBAPISTATUSUXX * THIS FILE IS GENERATED BY THE FUNCTION LIBRARY. * NEVER CHANGE IT MANUALLY, PLEASE! *INCLUDE LZBAPISTATUSU02. Z_BAPI_GET_ORDER_SYSTEM_STATUSINCLUDE LZBAPISTATUSTOP - Global data FUNCTION-POOL ZBAP
10、ISTATUS. MESSAGE-ID Z3Types: begin of Type_tj02t, istat like tj02t-istat, txt04 like tj02t-txt04, txt30 like tj02t-txt30, end of type_tj02t.DATA:* Declarations for TABLE parameter T_BAPISTAT like ZBAPISTAT occurs 0, G_BAPISTAT like ZBAPISTAT,* Table for object texts t_tj02t type type_tj02t occurs 0,
11、 g_tj02t type type_tj02t.* Structure for return messagesDATA: BEGIN OF MESSAGE, MSGTY LIKE SY-MSGTY, MSGID LIKE SY-MSGID, MSGNO LIKE SY-MSGNO, MSGV1 LIKE SY-MSGV1, MSGV2 LIKE SY-MSGV2, MSGV3 LIKE SY-MSGV3, MSGV4 LIKE SY-MSGV4, END OF MESSAGE.INCLUDE LZBAPISTATUSF01 - Subroutines *INCLUDE LZBAPISTATU
12、SF01 .*-*&-*& Form SET_RETURN_MESSAGE*&-* This routine is used for setting the BAPI return message.* The routine is a standard routine for BAPIs that handles the message* structure for the BAPIRETURN structure. It has been copied from the* BAPI Company Code Getlist*-* -P_MESSAGE text* -P_RETURN text
13、*-*form SET_RETURN_MESSAGE USING VALUE(P_MESSAGE) LIKE MESSAGE CHANGING P_RETURN LIKE BAPIRETURN. CHECK NOT MESSAGE IS INITIAL. CALL FUNCTION BALW_BAPIRETURN_GET EXPORTING TYPE = P_MESSAGE-MSGTY CL = P_MESSAGE-MSGID NUMBER = P_MESSAGE-MSGNO PAR1 = P_MESSAGE-MSGV1 PAR2 = P_MESSAGE-MSGV2 PAR3 = P_MESS
14、AGE-MSGV3 PAR4 = P_MESSAGE-MSGV4* LOG_NO = * LOG_MSG_NO = IMPORTING BAPIRETURN = P_RETURN EXCEPTIONS OTHERS = 1.endform. SET_RETURN_MESSAGEFUNCTION Z_BAPI_GET_ORDER_STATUS FUNCTION z_bapi_get_order_system_status.*-*Local interface:* IMPORTING* VALUE(I_AUFNR) TYPE AUFNR* VALUE(I_SPRAS) TYPE SPRAS DEF
15、AULT SY-LANGU* VALUE(I_EXCLUDEINACTIVE) TYPE CHAR1 OPTIONAL* EXPORTING* VALUE(RETURN) TYPE BAPIRETURN* TABLES* T_BAPISTAT STRUCTURE ZBAPISTAT*-*Local interface:* IMPORTING* VALUE(I_AUFNR) TYPE AUFNR* VALUE(I_SPRAS) TYPE SPRAS DEFAULT SY-LANGU* VALUE(I_EXCLUDEINACTIVE) TYPE CHAR1 OPTIONAL* EXPORTING*
16、 VALUE(RETURN) TYPE BAPIRET2* TABLES* T_BAPISTAT STRUCTURE ZBAPISTAT DATA: l_aufnr LIKE afko-aufnr, l_objnr LIKE jest-objnr.* Check if order exists* SELECT SINGLE aufnr FROM afko INTO l_aufnr WHERE aufnr = BAPI_ORDER_STATUS_IMPORT-orderid. IF sy-subrc NE 0. CLEAR message. message-msgty = E. message-
17、msgid = Z3. message-msgno = 000. message-msgv1 = BAPI_ORDER_STATUS_IMPORT-orderid. PERFORM set_return_message USING message CHANGING return. IF 1 = 2.* The only reason to include this statement, that will obviously* never execute, is that it will create a referecence so that you* can find out where
18、a particular message is being used. This* functionality is used by the BAPIs programmed by SAP MESSAGE e000(z3). ENDIF. ENDIF. CHECK return IS INITIAL.* Read order status* CONCATENATE OR BAPI_ORDER_STATUS_IMPORT-orderid INTO l_objnr. IF BAPI_ORDER_STATUS_IMPORT-i_excludeinactive = X. SELECT objnr st
19、at inact FROM jest INTO TABLE t_bapistat WHERE objnr = l_objnr AND inact X. ELSE. SELECT objnr stat inact FROM jest INTO TABLE t_bapistat WHERE objnr = l_objnr. ENDIF. IF sy-subrc 0.* No object status found CLEAR message. message-msgty = E. message-msgid = Z3. message-msgno = 001. message-msgv1 = BA
20、PI_ORDER_STATUS_IMPORT-orderid. PERFORM set_return_message USING message CHANGING return. IF 1 = 2. MESSAGE e001(z3). ENDIF. ENDIF. CHECK return IS INITIAL.* Read order status texts* SELECT istat txt04 txt30 FROM tj02t INTO TABLE t_tj02t FOR ALL ENTRIES IN t_bapistat WHERE istat = t_bapistat-stat AN
21、D spras = BAPI_ORDER_STATUS_IMPORT-i_spras. SORT t_tj02t BY istat. LOOP AT t_bapistat INTO g_bapistat. READ TABLE t_tj02t WITH KEY istat = g_bapistat-stat BINARY SEARCH INTO g_tj02t. IF sy-subrc = 0. MOVE: g_tj02t-txt04 TO g_bapistat-txt04, g_tj02t-txt30 TO g_bapistat-txt30. MODIFY t_bapistat FROM g
22、_bapistat TRANSPORTING txt04 txt30. ENDIF. ENDLOOP.ENDFUNCTION. STEP 3 - Create the API Method Using the BAPI WIZARDThe BAPI wizard is used toTo expose the remote function module as a BAPI. The wizard will generate some additional code, so the function module is a valid method of the BOR. This allow
23、s the BAPi to be called as a workflow method in addition to be called by an outside program. Note: Each functionmodule corresponds to a method in the BOR Go to the Busines Object Builder SWO1. You can either create the new Object type as a subtype of an existing business object or create a new busin
24、ess object from scratch. In this example it would be obvious to create the Object type as a subtype of BUS2005 Production order. However, to illustrate how to create a new Object type from scratch, we will do this. In the Object/Interface type field write the name of the new Business Object: ZORDERS
25、TAT. Press enter and fill in the additional fields necessary to create the object type. Supertype: Not relevant because we are creating our object from scratch Program. This is the name of the program where the wizard generates code for the Object type, NOT the function module we craeted earlier. Th
26、e program name must not be the name of an existinbg program. Press enter and create the new business object. Note that when you create the business object a standard interface, an attribute ObjectType and the methods ExistenceCheck and Display are automatically generated. These cannot be changed ! T
27、he next step is to add the Z_BAPI_GET_ORDER_STATUS method to the business object. Select Utillities - API methods - Add method and write the name of the functionmodule in the dialogbox. Next the dialogbox show below will be shown. This is the start screen of the BAPI wizard. Proceed with wizard by p
28、ressing the button. After you have finished the wizard, tyou will notice that the ZGetOrderStatus has been added to the business object: You can doubleclick on the method to see its properties. To use the business object you must change the Object type status to Implemented. Use menu Edit-Change releases status-Object type-To implemented. No you can test thge object (Press F8). Not
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1