# BAPI as a Model SAP ABAP Web Dynpro

BAPI as a Model
BAPI are standard SAP interfaces, defined as methods for the business object types that can be used in the business data exchange among the SAP components, and among the SAP and the non SAP components. In our example, we use the YFM_SEARCH_CANDIDATE Function Module to create a simple BAPI example that we use as a model in a WD component. To be able to do this, we should firstly make some changes at the Function Module level.

Creating a BAPI
We create a new Function Group, where we copy the YFM_SEARCH_CANDIDATE and rename it YBAPI_YCANDIDATE_SEARCH. For a BAPI, we have a name convention BAPI_<name_object_type>_<method_name>. In case of asearch( ) BAPI, which reads the details for the object type YCANDIDATE, the name of the associated Function Module is YBAPI_YCANDIDATE_SEARCH. In the Attributes tab of the Function Module, we have to activate the option Remote-Enabled Module.

The remoteenabled module option – RFC

After this, we create a structure in the ABAP Dictionary with the same components as our YPERSON table.

Structure YBAPI_YPERSON
We use this structure to define our parameters id_candidate type YBAPI_ YPERSON-ID_PERSON and itab LIKE YBAPI_YPERSON. Now, we have to make another change, because we cannot use exceptions anymore.
RAISE EXCEPTION TYPE ycx_exception_t100_txt EXPORTING
textid = ycx_exception_t100_txt=>ycx_no_candidate
id_candidate = id_candidate.
In order to return a message in case no records exist in the database table YPERSON with the specified ID, we use an export Return parameter for returning the exception messages to the calling application. With this Return parameter, based on the reference structure BAPIRET2, we are able to record and classify all the possible errors that may occur. The structure BAPIRET2 has the structure presented.

The BAPIRET2 structure

We can fill this structure via the Function Module BALW_BAPIRETURN_GET2 .

CALL FUNCTION 'BALW_BAPIRETURN_GET2'
EXPORTING
type =
cl =
number =
* PAR1 = ' '
* PAR2 = ' '
* PAR3 = ' '
* PAR4 = ' '
* LOG_NO = ' '
* LOG_MSG_NO = ' '
* PARAMETER = ' '
* ROW = 0
* FIELD = ' '
* IMPORTING
* RETURN

Pattern for BALV_BAPIRETURN_ GET2
The Listing shows the Function Module structure. Function module

FUNCTION ybapi_ycandidate_search .
*"----------------------------------------------------------------------
*"*"Global Interface:
*" IMPORTING
*" VALUE(ID_CANDIDATE) TYPE YBAPI_YPERSON-ID_PERSON
*" TABLES
*" ITAB STRUCTURE YBAPI_YPERSON OPTIONAL
*" RETURN STRUCTURE BAPIRET2 OPTIONAL
*"----------------------------------------------------------------------
DATA msg_id_candidate LIKE sy-msgv1.
CLEAR msg_id_candidate.
REFRESH: return, itab.
SELECT *
FROM yperson
INTO TABLE itab
WHERE id_person = id_candidate.
IF sy-subrc <> 0.
msg_id_candidate = id_candidate.
CALL FUNCTION 'BALW_BAPIRETURN_GET2'
EXPORTING
type = 'E'
cl = 'YCL_T100_MSG'
number = '000'
par1 = msg_id_candidate
IMPORTING
return = return.
APPEND return.
ELSE.
APPEND return.
ENDIF.
ENDFUNCTION.

As we can see, instead of raising an exception in case we don’t find any records with the specified ID, we use the BALW_BAPIRETURN_GET2 Function Module to fill the structure BAPIRET2. We have used the defined YCL_T00_MSG message class number 000, and we have replaced the parameter placeholder with the value inserted by the user as a candidate ID. After saving and activating, we can release our Function Module: Function Module ! Release ! Release. As result of our RELEASE action, we’ll get a message of YBAPI_YCANDIDATE_SEARCH type. By using the SWO1 (Business Object Builder) transaction, we create an object named YCANDIDATE.

Creating the object candidate

We add a Method named SEARCH: Utilities ! API Methods ! Add Method.

Creating the API method

We have to release our object. We choose from the menu Edit ! Change Release Status ! Object Type ! To model. We make the same steps for To implement and To release, and the same steps for Object Type Component. After releasing the object, we can test it.

Testing the BAPI

Using the Created BAPI as a Model We create a WD component named Y_BAPI, where we use the created BAPI as a model to obtain the data required from the database table YPERSON.

WD component structure

By right-clicking on the WD component name, we choose from the contextual menu Create ! Service call. The Web Dynpro Wizard is open, creating an appropriate context structure and generating a method that contains the required coding for the service call.
The Wizard steps are:
l Start
l Select controller – we use the existing controller

Selecting the controller
l Select Service Type – we select the Function Module.

Selecting the service type

l Select Service – we specify only the name of the YBAPI_YCANDIDATE_ SEARCH Function Module; the Destination box remains unfilled (blank). In this way, the Function Module will be locally called

Selecting the service

l Adapt Context – here, we can choose which object type we want to use to represent the service function parameters in the WD controller: as a parameter, as a controller attribute or as a context node or context attribute.

l Specify Method Name – we have to specify the name of the generated method that shall execute the service.

Specifying the method name

l Generate Controller
After the last step, we press the COMPLETED button and the wizard ends. I generates the COMPONENTCONTROLLER context and the proper method EXECUTE_ YBAPI_YCANDIDATE_SEARC that shall execute the service

The generated context and method

The node IMPORTING contains the ID_CANDIDATE attribute in which is stored the value of the ID we are looking for. The node CHANGING contains the two nodes ITAB and RETURN. The node ITAB holds the information about the candidate with the searched ID. The node RETURN has the structure BAPIRET2; we need to use its message attribute to display the return messages on the screen for the end user. The View layout has the structure presented.

View layout and view context

When the user presses the button to look for a candidate, the Framework triggers the event handler method onactionsearch.

METHOD onactionsearch.
wd_comp_controller->execute_ybapi_ycandidate_searc( ).
DATA: lr_bapi_node TYPE REF TO if_wd_context_node,
lr_changing TYPE REF TO if_wd_context_node,
lr_return TYPE REF TO if_wd_context_node.
DATA lv_bapi_message TYPE bapi_msg.
lr_bapi_node = wd_context->get_child_node( YBAPI_YCANDIDATE_SEA ).
lr_changing = lr_bapi_node->get_child_node( CHANGING ).
lr_return = lr_changing->get_child_node( RETURN ).
lr_return->get_attribute( EXPORTING name = 'MESSAGE'
IMPORTING value = lv_bapi_message ).
IF lv_bapi_message IS NOT INITIAL.
DATA:lr_api_controller TYPE REF TO if_wd_controller,
lr_message_manager TYPE REF TO if_wd_message_manager.
lr_api_controller ?= wd_this->wd_get_api( ).
lr_message_manager = lr_api_controller->get_message_manager( ).
lr_message_manager->report_error_message(
message_text = lv_bapi_message ).
ENDIF.
ENDMETHOD.

The mode of calling the BAPI is encapsulated in the method generated by the wizard in COMPONENTCONTROLLER. All we have to do is to call this method: wd_comp_controller!execute_ybapi_ycandidate_searc( ). When the searched ID doesn’t exist in the database table YPERSON, we show the proper error message stored in the attribute message of the RETURN node. Figure shows the runtime result.

Runtime