Wednesday, 10 February 2021

Complete process to create Change Document in ABAP and SAPUI5

Requirement:

To show the record of changes in ‘city’ and ‘name’ defined as fields in customer-specific table using SAP Fiori app.

Solution:

Create Change Document Object for the respective table.

Implementation:

Step by step procedure to implement the solution.

Part1:

◉ Create table and Activate the change document for the required fields (transaction SE11).

◉ Create the change document with Change Document Objects (transaction SCDO).

Part2:

◉ Expose CDS views and Insert the appropriate calls in the corresponding redefined methods.

◉ Incorporate UI changes.

Creation of Change Document Object in ABAP:

1. Run the transaction code SE11.

2. Create a table ‘ZDB_STUDENT’. (For which you want to keep change document). Column name: MNDT, GUID, NAME, DOB, CITY

SAP ABAP Development, SAPUI5, SAP ABAP Exam Prep, SAP ABAP Preparation, SAP ABAP Career, SAP ABAP Study Materials, SAP ABAP Guides

3. Maintain a user defined Data Element and in ‘Further Characteristics’ check the box for ‘Change Document’ for those fields you want to maintain changes.

SAP ABAP Development, SAPUI5, SAP ABAP Exam Prep, SAP ABAP Preparation, SAP ABAP Career, SAP ABAP Study Materials, SAP ABAP Guides

Note: Don’t check ‘log changes’ for the table in technical settings.

4. Run the transaction code SCDO.

5. Now create a change document for the table. Give ’zcd_student’ name of the change document object and click ‘create’ button.

SAP ABAP Development, SAPUI5, SAP ABAP Exam Prep, SAP ABAP Preparation, SAP ABAP Career, SAP ABAP Study Materials, SAP ABAP Guides

6. Follow the instructions in the next dialog box and enter the package name, to which the change document object belongs, and enter a transport request. Click on ‘save’ button.

SAP ABAP Development, SAPUI5, SAP ABAP Exam Prep, SAP ABAP Preparation, SAP ABAP Career, SAP ABAP Study Materials, SAP ABAP Guides

7. In the dialog box ‘Properties of Change Document Object’, enter a description for the change document under Text and enter the database tables that belong to this info type under Table. Save these changes and then click on ‘Generate’.

SAP ABAP Development, SAPUI5, SAP ABAP Exam Prep, SAP ABAP Preparation, SAP ABAP Career, SAP ABAP Study Materials, SAP ABAP Guides

8. The “Generate Update Pgm” dialog box is displayed. Fill the required details and give the name of function group. This function group will contain function module and includes, generated by further process. Then click on ‘Generate’ button.

SAP ABAP Development, SAPUI5, SAP ABAP Exam Prep, SAP ABAP Preparation, SAP ABAP Career, SAP ABAP Study Materials, SAP ABAP Guides

SAP ABAP Development, SAPUI5, SAP ABAP Exam Prep, SAP ABAP Preparation, SAP ABAP Career, SAP ABAP Study Materials, SAP ABAP Guides

9. Then click on the ‘Activate’ button.

SAP ABAP Development, SAPUI5, SAP ABAP Exam Prep, SAP ABAP Preparation, SAP ABAP Career, SAP ABAP Study Materials, SAP ABAP Guides

10. After this process Change Document Object is created and we can use created function module and includes to track created, updated and deleted entries.

SAP ABAP Development, SAPUI5, SAP ABAP Exam Prep, SAP ABAP Preparation, SAP ABAP Career, SAP ABAP Study Materials, SAP ABAP Guides

Changes will be logged in the following tables: CDHDR and CDPOS. CDHDR is a header table and CDPOS contains the actual data. Created function module ‘ZDB_STUDENT_CD_WRITE_DOCUMENT’ will be used to write changes to these tables.

SAP ABAP Development, SAPUI5, SAP ABAP Exam Prep, SAP ABAP Preparation, SAP ABAP Career, SAP ABAP Study Materials, SAP ABAP Guides
cdhdr

SAP ABAP Development, SAPUI5, SAP ABAP Exam Prep, SAP ABAP Preparation, SAP ABAP Career, SAP ABAP Study Materials, SAP ABAP Guides
cdpos

11. Create a CDS view for the ‘ZDB_C_STUDENT’ database table in SAP HANA Studio/Eclipse.

Expose CDS views to OData service.


12. Run the transaction code SEGW.
13. Expose the CDS view to OData service.

SAP ABAP Development, SAPUI5, SAP ABAP Exam Prep, SAP ABAP Preparation, SAP ABAP Career, SAP ABAP Study Materials, SAP ABAP Guides

SAP ABAP Development, SAPUI5, SAP ABAP Exam Prep, SAP ABAP Preparation, SAP ABAP Career, SAP ABAP Study Materials, SAP ABAP Guides

And redefine methods in data provider class (dpc_ext).

14. Create_Entity Method ==> In this method:

i. Read data (received from the front-end) using ‘io_data_provider’ into ‘ls_data’ work area.
ii. Insert entry into database table to create new record.
iii. After creating entry, write following code to create entry for change document.

DATA: lt_cdtxt TYPE STANDARD TABLE OF cdtxt,
      lt_xstu TYPE STANDARD TABLE OF YZDB_STUDENT,
      ls_xstu TYPE YZDB_STUDENT.

  MOVE-CORRESPONDING ls_data TO ls_xstu.
  ls_xstu-kz = 'I'.
  APPEND ls_xstu TO lt_xstu.

  CALL FUNCTION 'ZDB_STUDENT_CD_WRITE_DOCUMENT' "Create Change Doc for New Domain
    EXPORTING
      objectid                  = CONV cdobjectv( lv_guid )
      tcode                     = sy-tcode
      utime                     = sy-uzeit
      udate                     = sy-datum
      username                  = sy-uname
      object_change_indicator   = 'I'
      upd_icdtxt_zdb_student_cd = 'I'
      upd_zdb_student           = 'I'
    TABLES
      icdtxt_zdb_student_cd     = lt_cdtxt
      xzdb_student              = lt_xstu.​

After successful creation of entry in database, we’ll call ‘ZDB_STUDENT_CD_WRITE_DOCUMENT’ function module to create entry in change document object.

15. Update_Entity Method ==> In this method:

i. Read data (received from the front-end) using ‘io_data_provider’ into ‘lt_new’ table.
ii. Before updating database record, select the old record into ‘ls_data’ work area.

SELECT SINGLE * FROM ZDB_STUDENT INTO @DATA(ls_data) WHERE guid EQ @lv_guid. "#EC CI_ALL_FIELDS_NEEDED​

iii. Write the logic to update database record.
iv. After Updating record in database table, write following code to maintain changes for change document.

DATA: lt_cdtxt TYPE STANDARD TABLE OF cdtxt,
      lw_cdtxt TYPE cdtxt,
      lt_xstu TYPE STANDARD TABLE OF YZDB_STUDENT,
      ls_xstu TYPE YZDB_STUDENT,
      lt_ystu TYPE STANDARD TABLE OF YZDB_STUDENT.

      MOVE-CORRESPONDING lt_new TO lt_xstu.
      READ TABLE lt_xstu INTO ls_xstu INDEX 1.
      REFRESH lt_xstu.
      ls_xstu-guid = lv_guid.
      ls_xstu-mandt = sy-mandt.
      ls_xstu-kz = 'U'.
      APPEND ls_xstu TO lt_xstu.

      CLEAR ls_xstu.
      MOVE-CORRESPONDING ls_data TO ls_xstu.
      ls_xstu-kz = 'U'.
      APPEND ls_xstu TO lt_ystu.

      CALL FUNCTION 'ZDB_STUDENT_CD_WRITE_DOCUMENT' " Function Module for writing changes in Change Documents
        EXPORTING
          objectid                = conv CDOBJECTV( lv_guid )
          tcode                   = sy-tcode
          utime                   = sy-uzeit
          udate                   = sy-datum
          username                = sy-uname
          object_change_indicator = 'U'   " For Updating 
          upd_zdb_student        = 'U'
        TABLES
          icdtxt_zdb_student_cd        = lt_cdtxt
          XZDB_STUDENT                  = lt_xstu  " Updated Data 
          YZDB_STUDENT                  = lt_ystu. " Old Data 

16. Delete_Entity Method ==> In this method:

i. Take primary key(’iv_guid’) of the record to be deleted.
ii. Before deleting record from the database, select the record into ‘ls_xstu’ work area.
iii. Write the logic to delete database record.
iv. After deleting record from the database table, write code to maintain changes for change document.

DATA: lt_cdtxt TYPE STANDARD TABLE OF cdtxt,
      lt_xstu TYPE STANDARD TABLE OF YZDB_STUDENT,
      ls_xstu TYPE YZDB_STUDENT,
      lt_ystu TYPE STANDARD TABLE OF YZDB_STUDENT.

  SELECT SINGLE * FROM ZDB_STUDENT INTO ls_xstu WHERE guid = iv_guid. "#EC CI_ALL_FIELDS_NEEDED
  DELETE FROM ZDB_STUDENT WHERE guid =  iv_guid.

    ls_xstu-kz = 'D'.
    APPEND ls_xstu TO lt_ystu.
    CALL FUNCTION 'ZDB_STUDENT_CD_WRITE_DOCUMENT' " Create Change Document for Deleted Domain
      EXPORTING
        objectid                = conv CDOBJECTV( iv_guid )
        tcode                   = sy-tcode
        utime                   = sy-uzeit
        udate                   = sy-datum
        username                = sy-uname
        object_change_indicator = 'D'
        upd_zdb_student            = 'D'
      TABLES
        icdtxt_zdb_student_cd   = lt_cdtxt
        YZDB_STUDENT         = lt_ystu.​

17. Expose ‘C_ChangeDocuments’ CDS view in OData Service. This CDS view will fetch data from CDHDR and CDPOS tables. This is standard CDS view to show the details of all available change document objects. To see the change records of only our change document object, we will filter records from the UI based on change document object name (ChangeDocObjectClass) and primary key field of our database table (ChangeDocObject).

SAPUI5 Changes

18. Create Project.
19. OData service:

i. Add destination for OData service.
ii. Right click on the project and then click on new then choose OData service.

20. View and Controller.

i. ‘Main’ View: Create a ‘Main’ view to show the records of ‘zdb_student’ table. Create a smart table and bind it to created CDS entity for ‘zdb_student’ table.
ii. ‘ChangeHistory’ View: Create another view to show the list of changes made in ‘city’ and ‘name’ field of the selected record from ‘Main’ view. Create a smart table in view and on ‘beforeRebindTable’ event, add filter and set ‘entitySet’ to this smart table.

SAP ABAP Development, SAPUI5, SAP ABAP Exam Prep, SAP ABAP Preparation, SAP ABAP Career, SAP ABAP Study Materials, SAP ABAP Guides

◉ Set ‘setDefaultCountMode’ of model to ‘inline’ so that there is no OData call to count.

◉ Then it will set following filters to make sure that only necessary records are being fetched.

     ◉ ‘ChangeDocObjectClass’ should be equal to ’zcd_student’ (Change Document of ‘zdb_student’ table).

     ◉ ‘ChangeDocObject’ should be equal to value of Key (primary key of the record).

◉ Then set property ‘setEntitySet’ to ‘C_ChangeDocuments’ of change history smart table.

No comments:

Post a Comment