Business Example
You are a developer or solution architect in your company. You need to perform a basic implementation of the methods for creating, updating, and deleting a single entity.
- Template:
- GW100_S_PAGING (SAP Gateway Project)
- Solution:
- GW100_S_CUD (SAP Gateway Project)
Note
This exercise requires an SAP Learning system. Login information are provided by your system setup guide.Note
You may continue with your solution of the previous exercise or copy the template to ZGW100_##_CUD. Whenever the values or object names in this exercise include ##, replace ## with the number of your user.Prerequisites
The read operation for business partner was implemented in exercise Implement Bi-Directional Navigation.
Task 1: Implement and Test a Create Operation
Steps
In the SAP Gateway Service Builder of your SAP S/4HANA (S4H) system, navigate to the implementation of the BUSINESSPARTNERS_CREATE_ENTITY method of your project.
In the SAP Easy Access of your S4H, search for SAP Gateway Service Builder or start transaction SEGW.
In the SAP Gateway Service Builder, expand the Service Implementation → BusinessPartnerSet node.
In the context menu of Create, choose Go to ABAP Workbench.
In the Information popup, choose Continue.
Result
The method is not yet implemented.
In the Class Builder, redefine the method BUSINESSPARTNERS_CREATE_ENTITY to create a new business partner by calling the BAPI_EPM_BP_CREATE function module.
In the Class Builder, choose Display <-> Change.
Place the curser in the BUSINESSPARTNERS_CREATE_ENTITY method and choose Redefine.
Write the following code or copy it from the solution:
1234567891011121314151617181920212223242526272829303132333435363738394041424344
METHOD businesspartners_create_entity.
DATA: ls_headerdata TYPE bapi_epm_bp_header,
ls_bp_id TYPE bapi_epm_bp_id,
lt_return TYPE TABLE OF bapiret2.
* Get request data
io_data_provider->read_entry_data(
IMPORTING es_data = er_entity ).
* Map request fields to function module parameters
ls_headerdata = VALUE #(
bp_role = er_entity-businesspartnerrole
email_address = er_entity-emailaddress
company_name = er_entity-companyname
currency_code = er_entity-currencycode
city = er_entity-city
street = er_entity-street
country = er_entity-country
address_type = er_entity-addresstype ).
* Create data
CALL FUNCTION 'BAPI_EPM_BP_CREATE'
EXPORTING
headerdata = ls_headerdata
* PERSIST_TO_DB = ABAP_TRUE
IMPORTING
businesspartnerid = ls_bp_id
TABLES
return = lt_return.
IF lt_return IS NOT INITIAL.
"Message Container
mo_context->get_message_container( )
->add_messages_from_bapi( lt_return ).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
textid = /iwbep/cx_mgw_busi_exception=>business_error
message_container = mo_context->get_message_container( ).
ENDIF.
* Fill response data
er_entity = VALUE #( BASE er_entity
businesspartnerid = ls_bp_id-bp_id ).
ENDMETHOD.
Choose Activate.
In the Inactive Objects popup, select all objects and choose Continue.
Choose Exit.
In the SAP Gateway Client of your S4H, test the create operation in your service. Create a business partner using an existing one as template. Enter a unique email address.
In the SAP Gateway Service Builder of your S4H, double-click Service Maintenance.
Choose SAP Gateway Client.
In the SAP Gateway Client, select HTTPS as Protocol.
Choose Entity Set.
In the Entity Sets popup, double-click BusinessPartnerSet.
Example
/sap/opu/odata/SAP/ZGW100_##_STUDENT_SRV/BusinessPartnerSet
Choose Execute.
Result
The HTTP Response displays the business partners.
In the Request URI field, add the key of a business partner to the URI.
Example
/sap/opu/odata/SAP/ZGW100_##_STUDENT_SRV/BusinessPartnerSet('0100000000')
Choose Execute.
Result
The HTTP Response displays the business partner.
In the HTTP Response, choose Use as Request.
In the HTTP Request, enter a unique value for EmailAddress.
In the Request URI field, delete the key.
Example
/sap/opu/odata/SAP/ZGW100_##_STUDENT_SRV/BusinessPartnerSet
Select the HTTP method POST.
Choose Execute.
Result
The HTTP Response displays your new business partner with status 201 (Created).
Memorize the new business partner ID.
Task 2: Implement and Test an Update Operation
Steps
In the SAP Gateway Service Builder of your S4H, navigate to the implementation of the BUSINESSPARTNERS_UPDATE_ENTITY method of your project.
In the SAP Gateway Service Builder of your S4H, expand the Service Implementation → BusinessPartnerSet node.
In the context menu of Update, choose Go to ABAP Workbench.
In the Information popup, choose Continue.
Result
The method is not yet implemented.
In the Class Builder, redefine the method BUSINESSPARTNERS_UPDATE_ENTITY to update a business partner by calling the BAPI_EPM_BP_CHANGE function module.
In the Class Builder, choose Display <-> Change.
Place the curser in the BUSINESSPARTNERS_UPDATE_ENTITY method and choose Redefine.
Write the following code or copy it from the solution:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
METHOD businesspartners_update_entity.
DATA: ls_bp_id TYPE bapi_epm_bp_id,
ls_headerdata TYPE bapi_epm_bp_header,
ls_headerdatax TYPE bapi_epm_bp_headerx,
lt_return TYPE TABLE OF bapiret2.
* Get key
io_tech_request_context->get_converted_keys(
IMPORTING
es_key_values = er_entity
).
ls_bp_id-bp_id = er_entity-businesspartnerid.
* Get request data
io_data_provider->read_entry_data(
IMPORTING es_data = er_entity ).
* Map request fields to function module parameters
ls_headerdata = VALUE #(
bp_id = er_entity-businesspartnerid
bp_role = er_entity-businesspartnerrole
email_address = er_entity-emailaddress
company_name = er_entity-companyname
currency_code = er_entity-currencycode
city = er_entity-city
street = er_entity-street
country = er_entity-country
address_type = er_entity-addresstype ).
* Map constant value to function module parameters
ls_headerdatax-bp_id = er_entity-businesspartnerid.
ls_headerdatax-email_address = 'X'.
ls_headerdatax-company_name = 'X'.
ls_headerdatax-currency_code = 'X'.
ls_headerdatax-city = 'X'.
ls_headerdatax-street = 'X'.
ls_headerdatax-country = 'X'.
ls_headerdatax-address_type = 'X'.
ls_headerdatax-bp_role = 'X'.
* Change data
CALL FUNCTION 'BAPI_EPM_BP_CHANGE'
EXPORTING
bp_id = ls_bp_id
headerdata = ls_headerdata
headerdatax = ls_headerdatax
* PERSIST_TO_DB = ABAP_TRUE
TABLES
return = lt_return.
IF lt_return IS NOT INITIAL.
"Message Container
mo_context->get_message_container( )
->add_messages_from_bapi( lt_return ).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
textid = /iwbep/cx_mgw_busi_exception=>business_error
message_container = mo_context->get_message_container( ).
ENDIF.
ENDMETHOD.
Choose Activate.
In the Inactive Objects popup, select all objects and choose Continue.
Choose Exit.
In the SAP Gateway Client of your S4H, test the update operation in your service. Change the city of the business partner you created before.
Caution
Do not change any other business partners to not interfere with other users. Use only the one(s) you created.In the SAP Gateway Service Builder of your S4H, double-click Service Maintenance.
Choose SAP Gateway Client.
In the SAP Gateway Client, select HTTPS as Protocol.
Choose Entity Set.
In the Entity Sets popup, double-click BusinessPartnerSet.
In the Request URI field, add the memorized key of the business partner you created before to the URI.
Example
/sap/opu/odata/SAP/ZGW100_##_STUDENT_SRV/BusinessPartnerSet('<your business partner id>')
Choose Execute.
Result
The HTTP Response displays the business partner.
In the HTTP Response, choose Use as Request.
In the HTTP Request, change the value for City to a value of your choice.
Select the HTTP method PUT.
Choose Execute.
Result
The HTTP Response is empty with status 204 (No Content).
Select the HTTP method GET.
Choose Execute.
Result
The HTTP Response displays the changed business partner.
Task 3: Implement and Test a Delete Operation
Steps
In the SAP Gateway Service Builder of your S4H, navigate to the implementation of the BUSINESSPARTNERS_DELETE_ENTITY method of your project.
In the SAP Gateway Service Builder of your S4H, expand the Service Implementation → BusinessPartnerSet node.
In the context menu of Delete, choose Go to ABAP Workbench.
In the Information popup, choose Continue.
Result
The method is not yet implemented.
In the Class Builder, redefine the method BUSINESSPARTNERS_DELETE_ENTITY to delete a business partner by calling the BAPI_EPM_BP_DELETE function module.
In the Class Builder, choose Display <-> Change.
Place the cursor in method BUSINESSPARTNERS_DELETE_ENTITY and choose Redefine.
Write the following code or copy it from the solution:
Note
If you copy the code from the solution, replace the references to the model provider class of the solution with one to your model provider class.1234567891011121314151617181920212223242526272829
METHOD businesspartners_delete_entity.
DATA: ls_entity TYPE zcl_zgw100_##_student_mpc=>ts_businesspartner,
ls_bp_id TYPE bapi_epm_bp_id,
lt_return TYPE TABLE OF bapiret2.
* Get key
io_tech_request_context->get_converted_keys(
IMPORTING
es_key_values = ls_entity ).
ls_bp_id-bp_id = ls_entity-businesspartnerid.
* Delete data
CALL FUNCTION 'BAPI_EPM_BP_DELETE'
EXPORTING
bp_id = ls_bp_id
* PERSIST_TO_DB = ABAP_TRUE
TABLES
return = lt_return.
IF lt_return IS NOT INITIAL.
"Message Container
mo_context->get_message_container( )
->add_messages_from_bapi( lt_return ).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
textid = /iwbep/cx_mgw_busi_exception=>business_error
message_container = mo_context->get_message_container( ).
ENDIF.
ENDMETHOD.
Choose Activate.
In the Inactive Objects popup, select all objects and choose Continue.
Choose Exit.
In the SAP Gateway Client of your S4H, test the delete operation in your service. Delete the business partner you created before.
Caution
Do not delete any other business partners to not interfere with other participants. Use only the one(s) you created.In the SAP Gateway Service Builder of your S4H, double-click Service Maintenance.
Choose SAP Gateway Client.
In the SAP Gateway Client, select HTTPS as Protocol.
Choose Entity Set.
In the Entity Sets popup, double-click BusinessPartnerSet.
In the Request URI field, add the memorized key of the business partner you created before to the URI.
Example
/sap/opu/odata/SAP/ZGW100_##_STUDENT_SRV/BusinessPartnerSet('<your business partner id>')
Select the HTTP method DELETE.
Choose Execute.
Result
The HTTP Response is empty with status 204 (No Content).
Select the HTTP method GET.
Choose Execute.
Result
The HTTP Response displays the error Invalid Business Partner Id <your business partner id>. The business partner was deleted successfully.
Task 4: Document Create, Update, and Delete as Operational in Metadata
Steps
In the SAP Gateway Service Builder of your S4H, set the BusinessPartnerSet entity set to be Creatable, Updatable, and Deletable.
In the SAP Gateway Service Builder of your S4H, expand Data Model.
Double-click Entity Sets.
For the BusinessPartnerSet, set the Creatable, Updatable, and Deletable checkbox.
Set all properties of the entity type BusinessPartner to be Creatable and Updatable except the property BusinessPartnerID.
Expand Data Model → Entity Types → BusinessPartner.
Double-click Properties.
For all properties except BusinessPartnerID, set the Creatable and Updatable checkbox.
Choose Generate Runtime Objects.