RAP Prechecks in DRAFT Mode
RAP Prechecks in DRAFT Mode

RAP Prechecks in DRAFT Mode

Published at February 3, 2024by Sören Schlegel

The following texts were partially or completely generated with the help of generative AI models.

I already wrote a short post on combining prechecks - but when you use prechecks in DRAFT-enabled scenarios, you face a different problem: updates only contain changed fields - how do you deal with that?

First, let's look at the definition. This can be done both at the behavior definition level and at the behavior projection level - personally, I usually prefer doing it at the projection level, since it makes sense to model the check again as a determination on save, ensuring clean integrity of the BO:

define behavior for ZC_MyEntity alias Header
{
  use create ( augment, precheck );
  use update ( augment, precheck );
  use delete;
  ...
}

After defining the behavior, we come to the method definition:

    METHODS precheck FOR PRECHECK
      IMPORTING entities_create FOR CREATE Header
                entities_update FOR UPDATE header.

And yes: last but not least, the implementation:

    DATA(entities_check) = entities_create.

    LOOP AT entities_update INTO DATA(entity_update).

      READ ENTITIES OF ZC_MyEntity IN LOCAL MODE
           ENTITY Header
           ALL FIELDS WITH VALUE #( ( %tky = entity_update-%tky ) )
           RESULT DATA(header_read).

      IF lines( header_read ) = 0.
        CONTINUE.
      ENDIF.

      APPEND INITIAL LINE TO entities_check ASSIGNING FIELD-SYMBOL(<entity_check>).

      <entity_check> = CORRESPONDING #( header_read[ 1 ] ).
      <entity_check> = CORRESPONDING #( BASE ( <entity_check> ) entity_update USING CONTROL ).

    ENDLOOP.


    LOOP AT entities_check INTO DATA(entity_check).
* Here are our checks!
    ENDLOOP.

What are we doing here?

  • Line 1: Here we define a variable that should eventually contain all entries we want to check
  • Line 3: We loop over the ENTITIES_UPDATE, since we have to read the other content for these
  • Line 5: Now comes a READ ENTITIES - depending on where we implement our precheck, on the ZI_* or the ZC_*
  • Line 7: This is especially important for DRAFT newcomers! We don't use %KEY, but %TKY - we might want to read the content of the DRAFT - and not only what has been activated.
  • Line 10: As always, we should also make sure here that something already exists and that we are not in the first change
  • Line 17: Now we combine "old values" and "new values" - we use the CONTROL flags that RAP provides us, instead of performing a manual comparison
  • Line 22ff: Now we can go wild with all our magic 🙂

Have fun!

Sören Schlegel

More articles

New!
New!