How %is_draft Controls Instance Creation in SAP RAP

How %is_draft Controls Instance Creation in SAP RAP

SAP RAP

When working with draft-enabled RAP applications, you may have noticed a special indicator called %is_draft.

This field is part of BDEF derived types and is used to indicate whether a RAP Business Object instance represents a draft instance or an active instance.

Most of the time, drafts are handled automatically by RAP when users interact with applications through the UI. However, when working with actions or EML, developers can explicitly control how instances are created using this indicator.

In this blog, we will explore what %is_draft represents and how it influences the creation of draft and active instances.


What Can %is_draft Do?

The %is_draft indicator allows us to control how a new instance should be created.

Depending on how it is populated, it can lead to the following scenarios:

  1. Creating a Draft instance from an Active instance

  2. Creating an Active instance from a Draft instance

  3. Creating a Draft instance from a Draft instance

To experiment with this behavior, we will reuse the factory action that we implemented earlier in our Billing Document example.


Factory Action Implementation

Let us first look at the implementation of the instance factory action used to copy a Billing Document.

METHOD copyBillingDocument.

    DATA: lt_billingdocuments TYPE TABLE FOR CREATE zsac_i_bill_header\zsac_i_bill_header,
          lv_billingdoc       TYPE c LENGTH 10.

    " Get highest existing Billing Document ID
    SELECT FROM zsac_i_bill_header
      FIELDS MAX( BillId )
      INTO @lv_billingdoc.

    " Generate next Billing Document ID
    lv_billingdoc = lv_billingdoc + 1.

    " Read source Billing Document(s) to copy
    READ ENTITIES OF zsac_i_bill_header IN LOCAL MODE
      ENTITY zsac_i_bill_header
      ALL FIELDS WITH CORRESPONDING #( keys )
      RESULT DATA(lt_billdoc).

    LOOP AT lt_billdoc ASSIGNING FIELD-SYMBOL().

      " Prepare data for new Billing Document
      APPEND VALUE #(
          %cid      = keys[ KEY entity %key = -%key ]-%cid
          %is_draft = keys[ KEY entity %key = -%key ]-%param-%is_draft
          %data     = CORRESPONDING #(  EXCEPT BillId )
        )
        TO lt_billingdocuments ASSIGNING FIELD-SYMBOL().

      " Adjust copied values
      -BillId   = CONV #( lv_billingdoc ).
      -BillDate = cl_abap_context_info=>get_system_date( ).

    ENDLOOP.

    " Create new Billing Document
    MODIFY ENTITIES OF zsac_i_bill_header IN LOCAL MODE
      ENTITY zsac_i_bill_header
      CREATE
        FIELDS ( BillId BillDate BillType Currency CustomerId NetAmount SalesOrg )
        WITH lt_billingdocuments
      MAPPED DATA(mapped_create).

    " Return mapped instances
    mapped-zsac_i_bill_header = mapped_create-zsac_i_bill_header.

ENDMETHOD.

If you notice the following line in the implementation:

%is_draft = keys[ KEY entity %key = -%key ]-%param-%is_draft

Here we are forwarding the %is_draft value received as an action parameter while creating the new instance.

This indicator determines whether the instance created by the action will be a draft instance or an active instance.

To better understand this behavior, we can execute the action using EML.

Note: You can create a class implementing the interface if_oo_adt_classrun to test the EML execution.


Scenario 1: Create a Draft Instance from an Active Instance

To create a draft instance from an active instance, we pass the key of an active instance and set %param-%is_draft to true.

MODIFY ENTITIES OF zsac_i_bill_header
  ENTITY zsac_i_bill_header
    EXECUTE copyBillingDocument
      FROM VALUE #(

        (
          %cid = 'CID_COPY_001'

          %tky = VALUE #(
                   %key-BillId = '1000000004'
                 )

          %param-%is_draft = if_abap_behv=>mk-on

        )

      ).

In this case:

  • The source instance is an active instance

  • The %is_draft parameter is true

  • The newly created instance will be a draft instance


Scenario 2: Create an Active Instance from a Draft Instance

In this scenario, the source instance is a draft instance, but we request the newly created instance to be active.

MODIFY ENTITIES OF zsac_i_bill_header
  ENTITY zsac_i_bill_header
    EXECUTE copyBillingDocument
      FROM VALUE #(

        (
          %cid = 'CID_COPY_001'

          %tky = VALUE #(
                   %key-BillId = '1000000003'
                   %is_draft   = if_abap_behv=>mk-on
                 )

          %param-%is_draft = if_abap_behv=>mk-off

        )

      ).

Here:

  • The source instance is a draft instance

  • The %is_draft parameter is false

  • The newly created instance becomes an active instance


Scenario 3: Create a Draft Instance from a Draft Instance

In this scenario, both the source and target instances are draft instances.

MODIFY ENTITIES OF zsac_i_bill_header
  ENTITY zsac_i_bill_header
    EXECUTE copyBillingDocument
      FROM VALUE #(

        (
          %cid = 'CID_COPY_001'

          %tky = VALUE #(
                   %key-BillId = '1000000003'
                   %is_draft   = if_abap_behv=>mk-on
                 )

          %param-%is_draft = if_abap_behv=>mk-on

        )

      ).

In this case:

  • The source instance is a draft instance

  • The %is_draft parameter is true

  • The new instance will also be created as a draft instance


Conclusion

The %is_draft indicator gives developers explicit control over how RAP instances are created in draft-enabled applications.

By setting the correct value for %is_draft, we can decide whether the resulting instance should be created as a draft or active instance.