Example for Developing the BAPI Function Module
FUNCTION BAPI_TRAVELAGENCY_CREATE .
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" VALUE(AGENCYDATA_IN) LIKE BAPISADTIN
*" STRUCTURE BAPISADTIN
*" EXPORTING
*" VALUE(AGENCYNUMBER) LIKE BAPISADETA-AGENCYNUM
*" TABLES
*" EXTENSIONIN STRUCTURE BAPIPAREX OPTIONAL
*" RETURN STRUCTURE BAPIRET2 OPTIONAL
*"----------------------------------------------------------------------
* global buffer is t_sbuspart, t_stravelag
************************************************************************
* length of name field for extension table in extension structure
CONSTANTS:
C_LENSTRUC TYPE I VALUE 30.
* work areas for the table extension structures
DATA:
WA_BAPI_TE_SA LIKE BAPI_TE_SA,
WA_BAPI_TE_SP LIKE BAPI_TE_SP.
* clear workareas for database tables
CLEAR T_SBUSPART.
CLEAR T_STRAVELAG.
************************************************************************
* perform authority checks
************************************************************************
* check the incoming data of SAP parameter AGENCYDATA_IN
PERFORM CHECK_AGENCYDATA_IN TABLES RETURN
USING AGENCYDATA_IN.
CLEAR RETURN.
LOOP AT RETURN WHERE TYPE = 'E' OR TYPE = 'A'.
EXIT.
ENDLOOP.
IF RETURN-TYPE = 'E' OR RETURN-TYPE = 'A'.
EXIT.
ENDIF.
************************************************************************
* Here a customer exit should be provided to check all data
* including the EXTENSIONIN parameter.
* This exit should be realized with the new exit technology,
* however this technology is still under construction.
* This example will be extended as soon as this technology is
* available.
* The Exit should have the following parameters:
* -> All BAPI-parameters: AGENCYDATA_IN, EXTENSIONIN
* <- RETURN
* The function module should pass the RETURN parameter from
* the exit to the client. 'E' means that the client can
* 'commit' the BAPI call, if there is an 'A' message in the
* RETURN parameter, the client should 'rollback work'.
************************************************************************
* get the missing id's STRAVELAG-ID & SBUSPART-BUSPARTNUM
CALL FUNCTION 'NUMBER_GET_NEXT'
EXPORTING
NR_RANGE_NR = '01'
OBJECT = 'SBUSPID'
QUANTITY = '1'
IMPORTING
NUMBER = AGENCYNUMBER
EXCEPTIONS
INTERVAL_NOT_FOUND = 1
NUMBER_RANGE_NOT_INTERN = 2
OBJECT_NOT_FOUND = 3
QUANTITY_IS_0 = 4
QUANTITY_IS_NOT_1 = 5
INTERVAL_OVERFLOW = 6
OTHERS = 7.
IF SY-SUBRC <> 0.
RETURN-TYPE = 'E'.
RETURN-ID = 'BCTRAIN'.
RETURN-NUMBER = '601'.
RETURN-MESSAGE_V1 = TEXT-500.
APPEND RETURN.
EXIT.
ENDIF.
************************************************************************
* Before adding data to the buffer, subscribe the function module
* to delete this buffer in case of 'rollback work' by the client.
CALL FUNCTION 'BUFFER_SUBSCRIBE_FOR_REFRESH'
EXPORTING
NAME_OF_DELETEFUNC = 'SAGENCY_REFRESH_BUFFER'.
************************************************************************
* Fill global buffer for table STRAVELAG.
MOVE-CORRESPONDING AGENCYDATA_IN TO T_STRAVELAG.
T_STRAVELAG-AGENCYNUM = AGENCYNUMBER.
APPEND T_STRAVELAG.
* Fill global buffer for table SBUSPART.
T_SBUSPART-BUSPARTNUM = AGENCYNUMBER.
T_SBUSPART-CONTACT = T_STRAVELAG-NAME.
T_SBUSPART-CONTPHONO = T_STRAVELAG-TELEPHONE.
T_SBUSPART-BUSPATYP = 'TA'.
APPEND T_SBUSPART.
* Collect the appended fields of tables STRAVELAG & SBUSPART.
LOOP AT EXTENSIONIN.
CASE EXTENSIONIN-STRUCTURE.
WHEN 'BAPI_TE_SA'.
MOVE EXTENSIONIN+C_LENSTRUC TO WA_BAPI_TE_SA.
* check if table entry to extension entry exist. for that
* we fill the internal given number into the extension table.
MOVE AGENCYNUMBER TO WA_BAPI_TE_SA-AGENCYNUM.
READ TABLE T_STRAVELAG
WITH KEY AGENCYNUM = AGENCYNUMBER.
CATCH SYSTEM-EXCEPTIONS CONVERSION_ERRORS = 1.
MOVE-CORRESPONDING WA_BAPI_TE_SA TO T_STRAVELAG.
ENDCATCH.
IF SY-SUBRC <> 0.
RETURN-TYPE = 'E'.
RETURN-ID = 'BCTRAIN'.
RETURN-NUMBER = '888'.
RETURN-MESSAGE_V1 = TEXT-800.
RETURN-MESSAGE_V2 = SY-TABIX.
RETURN-MESSAGE_V3 = TEXT-900.
APPEND RETURN.
EXIT.
ENDIF.
MODIFY T_STRAVELAG INDEX SY-TABIX.
WHEN 'BAPI_TE_SP'.
MOVE EXTENSIONIN+C_LENSTRUC TO WA_BAPI_TE_SP.
MOVE AGENCYNUMBER TO WA_BAPI_TE_SP-BUSPARTNUM.:
READ TABLE T_SBUSPART
WITH KEY BUSPARTNUM = AGENCYNUMBER.
CATCH SYSTEM-EXCEPTIONS CONVERSION_ERRORS = 1.
MOVE-CORRESPONDING WA_BAPI_TE_SP TO T_SBUSPART.
ENDCATCH.
IF SY-SUBRC <> 0.
RETURN-TYPE = 'E'.
RETURN-ID = 'BCTRAIN'.
RETURN-NUMBER = '888'.
RETURN-MESSAGE_V1 = TEXT-700.
RETURN-MESSAGE_V2 = SY-TABIX.
RETURN-MESSAGE_V3 = TEXT-900.
APPEND RETURN.
EXIT.
ENDIF.
MODIFY T_SBUSPART INDEX SY-TABIX.
ENDCASE.
ENDLOOP.
IF RETURN-TYPE = 'E'.
************************************************************************
* If an error of type 'E' occurred, we will have to make the
* buffer consistent again by deleting the data from this call.
* Errors of type 'A' can not occur at this point.
DELETE T_SBUSPART WHERE BUSPARTNUM = T_SBUSPART-BUSPARTNUM.
DELETE T_STRAVELAG WHERE AGENCYNUM = T_STRAVELAG-AGENCYNUM.
EXIT.
ENDIF.
************************************************************************
* Create agencies from global buffer in update task. This form will
* be performed only once during the 'commit' statement of the
* client, thus the data from all BAPI calls can be written to
* the database with only one array insert for each table
PERFORM CREATE_AGENCYS ON COMMIT.
************************************************************************
* Here a customer exit should be provided in some way
* to insert the data from the ExtensionIn parameter.
* This exit should be realized with the new exit technology,
* however this technology is still under construction.
* This example will be extended as soon as this technology is
* available.
* Within this exit, customer should modify db IN UPDATE TASK
* to guarantee consistency of the data !
* Be aware that nothing has happened on database yet so the
* customer cannot read data from this or any previous call in
* this LUW.
* The customer could use buffers too, but has to be aware of
* the refreshing concept!
* -> All BAPI-parameters: AGENCYDATA_IN, EXTENSIONIN and the id
* AGENCYNUMBER.
* Nothing comes back!
************************************************************************
* assume that everything is O.K., fill return parameters
RETURN-TYPE = 'S'.
RETURN-ID = 'BCTRAIN'.
RETURN-NUMBER = '603'.
RETURN-MESSAGE_V1 = TEXT-600.
RETURN-MESSAGE_V2 = T_STRAVELAG-AGENCYNUM.
RETURN-MESSAGE_V3 = TEXT-400.
APPEND RETURN.
ENDFUNCTION.