ABAP - Keyword Documentation → ABAP RAP Business Objects → RAP - Behavior Definitions → RAP - BDL for Behavior Definitions → RAP - Managed and Unmanaged Behavior Definitions → RAP - EntityBehaviorDefinition → RAP - EntityBehaviorCharacteristics → 

    RAP - draft table

    Syntax


    ...
    draft table DraftTableName [query DraftQueryView] 
    ...

    Description


    A RAP draft table is a DDIC database table used to store draft data. The draft table must reflect the fields of the underlying CDS entity. It must meet the following requirements:

    • It contains a field for each field of the CDS entity that the current entity is based on. The order of the fields is not relevant.
    • The first field must be a client field that has the data type CLNT.
    • The field names of the fields of the draft tables are the same as the field names of the underlying CDS entity. If the CDS entity has assigned alias names, these alias names must be used.
    • The data types and field lengths must match exactly.
    • Additionally, the draft table must contain some technical information the RAP transactional engine needs to handle the draft. This technical information is added automatically with the draft admin include. The draft admin include is the predefined DDIC include structure SYCH_BDL_DRAFT_ADMIN_INC.

    Here's an example:

    define table DraftTable {
      key client        : abap.clnt not null;
        ...
        "%admin"        : include sych_bdl_draft_admin_inc;}

    The optional addition query can be used to specify a draft query view DraftQueryView. A draft query view allows the definition of read access limitations for draft data based on the data control language (DCL).

    Availability


    Hints

    • The draft database table can be generated automatically via a quick fix in the behavior definition, which is offered as soon as RAP draft handling is enabled for the business object.
    • When using late numbering for a draft-enabled RAP BO, it is mandatory that the draft table has an additional key field DRAFTUUID of data type raw(16).
    • A draft table is a regular DDIC database table and can be accessed as such. Access with ABAP SQL and AMDP is technically possible, but not recommended. It is recommended that a draft table is accessed using the RAP framework only, for example, with EML, so that the draft metadata get updated automatically.

    Further Information


    Development guide for the ABAP RESTful Application Programming Model, topic Draft Database Table.

    Example


    The following example demonstrates why ABAP SQL is not suitable to access draft database tables.

    It shows a managed BDEF based on the CDS root view entity DEMO_RAP_MANAGED_DRAFT_1. The BDEF is draft-enabled and it defines the persistent database table DEMO_DBTAB_ROOT and the draft database table DEMO_DBTAB_DRAFT.

    Note: This example does not fully meet the requirements of the RAP BO contract. It is intentionally kept short and simple and serves demonstration purposes only. See more information on the RAP BO contract in the Development guide for the ABAP RESTful Application Programming Model.

    managed
    implementation in class bp_demo_rap_managed_draft_1 unique;
    strict ( 2 );
    with draft;

    define behavior for DEMO_RAP_MANAGED_DRAFT_1 alias ParentEntity
    persistent table demo_dbtab_root
    draft table demo_dbtab_draft
    lock master
    total etag Timestamp
    etag master LastChangedAt
    authorization master ( global )

    {
      create;
      update;
      delete;
      field ( readonly : update ) KeyFieldRoot;
      field ( readonly ) Timestamp, LastChangedAt;

      draft action Activate optimized;
      draft action Discard;
      draft action Edit;
      draft action Resume;
      draft determine action Prepare;

      mapping for demo_dbtab_root
        {
          KeyFieldRoot  = key_field;
          DataFieldRoot = data_field;
          CharFieldRoot = char_field;
          Timestamp     = crea_date_time;
          LastChangedAt = lchg_date_time;
        }
    }

    The class CL_DEMO_RAP_MANAGED_DRAFT_1 accesses the business object using EML. It contains the same code block twice: first, it clears the draft database table using the ABAP SQL statement DELETE FROM. Then, it creates a new draft instance with the key value '3' using the EML statement MODIFY ENTITIES and commits this entity instance to the draft database table.

    Code snippet - this code block is repeated twice:

    DELETE FROM demo_dbtab_draft. 
    MODIFY ENTITIES OF demo_rap_managed_draft_1
          ENTITY ParentEntity
            CREATE FROM
            VALUE #( ( %is_draft = if_abap_behv=>mk-on
            %control-KeyFieldRoot = if_abap_behv=>mk-on
            %data-KeyFieldRoot = 3 ) )
     
      REPORTED FINAL(create_reported)
      FAILED FINAL(create_failed)
      MAPPED FINAL(create_mapped).
     
    COMMIT ENTITIES.

    The first time, this entry is written to the draft database table and displayed as screen output. The second time, however, the creation of the new draft instance fails and the draft database table remains empty. The reason is that the draft database table cannot be emptied using ABAP SQL. It contains metadata which cannot be deleted with ABAP SQL and therefore, the key is considered to be a duplicate, even though the draft database table was cleared.

    figure

    This demonstrates why the draft database table should only be accessed using the RAP framework, in this case, using either the EML statement DELETE or the draft action Discard to clear all entries from the draft database table.

    The class CL_DEMO_RAP_MANAGED_DRAFT_2 demonstrates how to correctly clear a draft database table:

    • It creates a new draft instance with the key value '4' and discards it using the draft action Discard.
    • Then, it successfully creates the same instance again.
    • The instance is deleted again using the EML statement DELETE FROM and created once again.

    Result: the creation of a new draft instance with the same key value works three times in a row, because Discard and DELETE FROM clear the entries of the draft table as well as the metadata.

    figure

    Continue