Implementing Change Operations

Objectives

After completing this lesson, you will be able to:
  • Implement a create operation
  • Implement an update operation
  • Implement a delete operation

Implementation of a Create Operation

Create Operation

Create operation essentials

If you want to create or change data, you must retrieve this data from the incoming OData request. In the <Entity Set>_CREATE_ENTITY method, use the read_entry_data method that is offered by importing parameter io_data_provider. This is used to fetch the data sent via a POST request.

To create a proper HTTP request body needed for testing, you should first perform a read request in the SAP Gateway Client and use the Use as Request button.

The server responds with an HTTP 201 response status code if the create operation was successful. The body of the HTTP response then contains the newly created entry.

Screenshot flow about redefining Create

To implement the create operation for a business partner, we follow the same pattern as for the previous operations. The BUSINESSPARTNER_CREATE_ENTITY method can be opened following the path Service ImplementationBusinessPartnerSetCreateGot to ABAP Workbench. It is not yet implemented. So we select it in the navigation area and choose Redefine.

Source code implementing a create operation

For an <Entity Set>_CREATE_ENTITY method, the exporting parameter er_entity should be filled with the created entry including all generated fields. The parameter is typed using a ts_<Entity Type> structure generated in the MPC based on the definition of the entity type in the data model.

The io_data_provider parameter provides the OData request body and can be found in every create or update method. Its read_entry_data() method returns a structure that has the same type as the entity.

In our example, we follow the recommendation to not access the database directly and call a BAPI as reuse unit. This BAPI commits the data automatically. We just need to pass the generated key back to the SAP Gateway framework.

The following block provides the example source code in a copy-friendly way:

Code Snippet
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.

Perform a Create Request by Using the SAP Gateway Client

Screenshot flow of Use as Request in /IWFND/GW_CLIENT

To create a proper HTTP request body needed for testing, read a single entry in the SAP Gateway Client and choose the Use as Request button in the HTTP Response. The response body is then copied to the request body. You can change the request body as you like before continuing as long as you keep the structure.

Screenshot of a create request in /IWFND/GW_CLIENT

For a create request, you first select POST as the HTTP Method. Which entity should be created is set by adding the entity set name to the URI.

Note

A key is not allowed in the URI. If you want to set the key of the new entry, add it in the request body.

The create operation was successful if the HTTP return code is 201. The created entry including all generated fields like the key is returned in the HTTP response body.

Implementation of an Update Operation

Update Operation

Update operation essentials

For an update operation, in the <Entity Set>_UPDATE_ENTITY method, use the read_entry_data method that is offered by importing parameter io_data_provider. This is used to fetch the data sent via a PUT request. To identify the entry, which should be changed, call the get_converted_keys() method of the io_tech_request_context parameter to get the key.

To create a proper HTTP request body needed for testing, you should first perform a read request in the SAP Gateway Client and use the Use as Request button.

The server responds with an HTTP 204 response status code if the update operation was successful. The body of the HTTP response itself will be empty.

Screenshot flow about redefining Update

To implement the update operation for a business partner, we follow the same pattern as for the previous operations. The BUSINESSPARTNER_UPDATE_ENTITY method can be opened following the path Service ImplementationBusinessPartnerSetUpdateGot to ABAP Workbench. It is not yet implemented. So we select it in the navigation area and choose Redefine.

Source code implementing an update operation

For an <Entity Set>_UPDATE_ENTITY method, no entry is passed back to the framework.

Note

The <Entity Set>_UPDATE_ENTITY does have an exporting parameter er_entity. But it is ignored by the framework. This comes from a time before SAP Gateway was exclusively used for OData.

The io_tech_request_context parameter provides the OData request header and can be found in every CRUD or query method. It is typed based on an interface suitable for the individual method it is part of. For an update operation, the /IWBEP/IF_MGW_REQ_ENTITY_U interface provides methods to get request data like the key of the requested resource. Its get_converted_keys() method returns a structure that has the same type as the entity but only the key fields are filled. So there is no need to loop over a list of key fields.

The io_data_provider parameter provides the OData request body and can be found in every create or update method. Its read_entry_data() method returns a structure that has the same type as the entity.

In our example, we follow the recommendation to not access the database directly and call a BAPI as reuse unit. This BAPI commits the data automatically. We just need to set some control flags for the BAPI to operate correctly.

The following block provides the example source code in a copy-friendly way:

Code Snippet
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.
Screenshot of an update request in /IWFND/GW_CLIENT

For an update request, you first select PUT as the HTTP Method. Which entity should be updated is set by adding the entity set name and the key of the entity to the URI.

The update operation was successful if the HTTP return code is 204. No further content is returned. The response body is empty.

Note

In OData V4, the response body contains the updated entry.

Implementation of a Delete Operation

Delete Operation

Delete operation essentials

For a delete operation, in the <Entity Set>_DELETE_ENTITY method, use the get_converted_keys() method that is offered by importing parameter io_tech_request_context to identify the entry, which should be changed.

The body of the HTTP request is empty.

The server responds with an HTTP 204 response status code if the delete operation was successful. The body of the HTTP response is empty.

Screenshot flow about redefining Delete

To implement the delete operation for a business partner, we follow the same pattern as for the previous operations. The BUSINESSPARTNER_DELETE_ENTITY method can be opened following the path Service ImplementationBusinessPartnerSetDeleteGot to ABAP Workbench. It is not yet implemented. So we select it in the navigation area and choose Redefine.

Source code implementing a delete operation

For an <Entity Set>_UPDATE_ENTITY method, no entry is passed back to the framework.

The io_tech_request_context parameter provides the OData request header and can be found in every CRUD or query method. It is typed based on an interface suitable for the individual method it is part of. For a delete operation, the /IWBEP/IF_MGW_REQ_ENTITY_D interface provides methods to get request data like the key of the requested resource. Its get_converted_keys() method returns a structure that has the same type as the entity but only the key fields are filled. So there is no need to loop over a list of key fields.

In our example, we follow the recommendation to not access the database directly and call a BAPI as reuse unit. This BAPI commits the data automatically.

The following block provides the example source code in a copy-friendly way:

Code Snippet
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.
Screenshot of a delete request in /IWFND/GW_CLIENT

For a delete request, you first select DELETE as the HTTP Method. Which entity should be deleted is set by adding the entity set name and the key of the entity to the URI.

The delete operation was successful if the HTTP return code is 204. No further content is returned. The response body is empty.

Implement Create, Update, Delete Operations

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

  1. 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.

    1. In the SAP Easy Access of your S4H, search for SAP Gateway Service Builder or start transaction SEGW.

    2. In the SAP Gateway Service Builder, expand the Service ImplementationBusinessPartnerSet node.

    3. In the context menu of Create, choose Go to ABAP Workbench.

    4. In the Information popup, choose Continue.

      Result

      The method is not yet implemented.
  2. 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.

    1. In the Class Builder, choose Display <-> Change.

    2. Place the curser in the BUSINESSPARTNERS_CREATE_ENTITY method and choose Redefine.

    3. Write the following code or copy it from the solution:

      Code Snippet
      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.
    4. Choose Activate.

    5. In the Inactive Objects popup, select all objects and choose Continue.

    6. Choose Exit.

  3. 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.

    1. In the SAP Gateway Service Builder of your S4H, double-click Service Maintenance.

    2. Choose SAP Gateway Client.

    3. In the SAP Gateway Client, select HTTPS as Protocol.

    4. Choose Entity Set.

    5. In the Entity Sets popup, double-click BusinessPartnerSet.

      Example

      /sap/opu/odata/SAP/ZGW100_##_STUDENT_SRV/BusinessPartnerSet
    6. Choose Execute.

      Result

      The HTTP Response displays the business partners.
    7. 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')
    8. Choose Execute.

      Result

      The HTTP Response displays the business partner.
    9. In the HTTP Response, choose Use as Request.

    10. In the HTTP Request, enter a unique value for EmailAddress.

    11. In the Request URI field, delete the key.

      Example

      /sap/opu/odata/SAP/ZGW100_##_STUDENT_SRV/BusinessPartnerSet
    12. Select the HTTP method POST.

    13. Choose Execute.

      Result

      The HTTP Response displays your new business partner with status 201 (Created).
    14. Memorize the new business partner ID.

Task 2: Implement and Test an Update Operation

Steps

  1. In the SAP Gateway Service Builder of your S4H, navigate to the implementation of the BUSINESSPARTNERS_UPDATE_ENTITY method of your project.

    1. In the SAP Gateway Service Builder of your S4H, expand the Service ImplementationBusinessPartnerSet node.

    2. In the context menu of Update, choose Go to ABAP Workbench.

    3. In the Information popup, choose Continue.

      Result

      The method is not yet implemented.
  2. In the Class Builder, redefine the method BUSINESSPARTNERS_UPDATE_ENTITY to update a business partner by calling the BAPI_EPM_BP_CHANGE function module.

    1. In the Class Builder, choose Display <-> Change.

    2. Place the curser in the BUSINESSPARTNERS_UPDATE_ENTITY method and choose Redefine.

    3. Write the following code or copy it from the solution:

      Code Snippet
      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.
    4. Choose Activate.

    5. In the Inactive Objects popup, select all objects and choose Continue.

    6. Choose Exit.

  3. 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.
    1. In the SAP Gateway Service Builder of your S4H, double-click Service Maintenance.

    2. Choose SAP Gateway Client.

    3. In the SAP Gateway Client, select HTTPS as Protocol.

    4. Choose Entity Set.

    5. In the Entity Sets popup, double-click BusinessPartnerSet.

    6. 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>')
    7. Choose Execute.

      Result

      The HTTP Response displays the business partner.
    8. In the HTTP Response, choose Use as Request.

    9. In the HTTP Request, change the value for City to a value of your choice.

    10. Select the HTTP method PUT.

    11. Choose Execute.

      Result

      The HTTP Response is empty with status 204 (No Content).
    12. Select the HTTP method GET.

    13. Choose Execute.

      Result

      The HTTP Response displays the changed business partner.

Task 3: Implement and Test a Delete Operation

Steps

  1. In the SAP Gateway Service Builder of your S4H, navigate to the implementation of the BUSINESSPARTNERS_DELETE_ENTITY method of your project.

    1. In the SAP Gateway Service Builder of your S4H, expand the Service ImplementationBusinessPartnerSet node.

    2. In the context menu of Delete, choose Go to ABAP Workbench.

    3. In the Information popup, choose Continue.

      Result

      The method is not yet implemented.
  2. In the Class Builder, redefine the method BUSINESSPARTNERS_DELETE_ENTITY to delete a business partner by calling the BAPI_EPM_BP_DELETE function module.

    1. In the Class Builder, choose Display <-> Change.

    2. Place the cursor in method BUSINESSPARTNERS_DELETE_ENTITY and choose Redefine.

    3. 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.
      Code Snippet
      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.
    4. Choose Activate.

    5. In the Inactive Objects popup, select all objects and choose Continue.

    6. Choose Exit.

  3. 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.
    1. In the SAP Gateway Service Builder of your S4H, double-click Service Maintenance.

    2. Choose SAP Gateway Client.

    3. In the SAP Gateway Client, select HTTPS as Protocol.

    4. Choose Entity Set.

    5. In the Entity Sets popup, double-click BusinessPartnerSet.

    6. 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>')
    7. Select the HTTP method DELETE.

    8. Choose Execute.

      Result

      The HTTP Response is empty with status 204 (No Content).
    9. Select the HTTP method GET.

    10. 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

  1. In the SAP Gateway Service Builder of your S4H, set the BusinessPartnerSet entity set to be Creatable, Updatable, and Deletable.

    1. In the SAP Gateway Service Builder of your S4H, expand Data Model.

    2. Double-click Entity Sets.

    3. For the BusinessPartnerSet, set the Creatable, Updatable, and Deletable checkbox.

  2. Set all properties of the entity type BusinessPartner to be Creatable and Updatable except the property BusinessPartnerID.

    1. Expand Data ModelEntity TypesBusinessPartner.

    2. Double-click Properties.

    3. For all properties except BusinessPartnerID, set the Creatable and Updatable checkbox.

    4. Choose Generate Runtime Objects.

Log in to track your progress & complete quizzes