3

Prechecks on Update in RAP when using DRAFT

 2 months ago
source link: https://community.sap.com/t5/technology-blogs-by-members/prechecks-on-update-in-rap-when-using-draft/ba-p/13601689
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Sschlegel

Participant

58m ago

I already wrote a small Post on Combination of Prechecks - but when using Prechecks  in DRAFT- enabled scenarios, you'll face an other Problem: Updates only contain changed fields - how to deal with it?

First, we look at the definition. This can be done both at the level of the behavior definition and at the level of the behavior projection - I personally usually prefer this in the projection, as it makes sense to map the check again as a Determination on Save, so that a clean integrity of the BO is ensured:

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, 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 do we do here?

  • Line 1: Here we define a variable that should finally contain ALL entries that we want to check
  • Line 3: We loop over the ENTITIES_UPDATE, as we have to read the other contents 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 particularly important for DRAFT newcomers! We do not use %KEY, but %TKY - we may want to read the content of the DRAFT - and not what is only activated.
  • Line 10: As always, we should also make sure that there is already something 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 with instead of performing a manual comparison
  • Line 22ff: Now we can let off steam with all our magic 🙂

Enjoy!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK