Thursday 18 October 2018

ABAP Object Services Technique

ABAP Object Services

1. Creating Persistent Object with single-table


SAP Community has introduced excellent Professionals to SAP world as well as providing opportunity for all to writing blog posts which has helped us to improve SAP knowledge.

One of many important SAP techniques which I have been reading about is ABAP Object Services applying object-oriented concept.

This first blog post will show a simple way of creating persistence class working with single-table.

1. ABAP Object Services – Creating Persistent Object with single-table


◈ Objective: handling the synchronization of data stored in objects with database table creating a method to synchronize object data with a relational database table considering object relational mapping (ORM) tool.

◈ Following simple steps to create persistence class.

1.1. Creating Persistent Class (SE80)

◈ Start transaction SE80 to create a Persistence class without “Final” class checked.

SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Study Material

◈ Confirm “Yes” to activate Class Agent – “At runtime, Class Agent run between persistent objects and the ABAP Object Services”.

SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Study Material

◈ Agent ABAP Persistent Class implemented.

SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Study Material

1.2. Working with single-table using Mapping Assistant tool

◈ Going to Persistence Representant to add single-table

SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Study Material

◈ Insert table/structure for corresponding class.

SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Study Material

◈ By Business Key – ABAP Dictionary tables which use exiting primary key.

SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Study Material

◈ Mapping Assistant tool  – Click on Generator Settings button and uncheck the minimum Interface for methods CREATE_PERSISTENT and CREATE_TRANSIENT.

SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Study Material

1.3. Persistent objects – Consumer Perspective

◈ Persistent objects are managed by object services.

SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Study Material

1.4. Creating Persistent Object Instance with class agent

◈ ABAP code example: Simple report with corresponding statement Persistent Class and Agent Class working with synchronization of data stored in objects with a relational database table.

*&---------------------------------------------------------------------*
*& Report ZABAP_PERSISTENT_OBJECT
*&---------------------------------------------------------------------*
REPORT zabap_persistent_object.

*&---------------------------------------------------------------------*
*& DATA OBJECT
*&---------------------------------------------------------------------*
DATA: lo_cl_persist TYPE REF TO zcl_single_table_persistent,
      lx_os_ex      TYPE REF TO cx_os_object_existing,
      lv_text       TYPE string.

*&---------------------------------------------------------------------*
*& START-OF-SELECTIONf)
*&---------------------------------------------------------------------*
START-OF-SELECTION.

  TRY.
      lo_cl_persist = zca_single_table_persistent=>agent->create_persistent(
                        i_char20 = 'TEST01'
                        i_int4   = 1
                        i_char50 = 'TEST-01'
                        i_fltp   = '100.50'
                        i_tims   = sy-uzeit ).
      COMMIT WORK.

    CATCH cx_os_object_existing INTO lx_os_ex.
      lv_text = lx_os_ex->get_text( ).
  ENDTRY.

* COMMIT WORK: Persistence Service convert in-memory record into the Database table.

1.5. Record converted into Database table

◈ Transactions SE11, SE16 and SE16N to check it.

SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Study Material

Certainly, learning ABAP Object Services technique will improve technical knowledge as well as makes the ABAP code flexible to implement persistence without writing SQL.

No comments:

Post a Comment