Code Snippets for Validations

Required Field Validation – Child Entity

  • Seminar is the root entity
  • Teilnehmer is the child entity
  • Validation is only triggered on main view

Simple Required Field Validation

Behavior Definition

Define the fields as mandatory

   field ( mandatory )
   Title,
   Description,
   Difficulty;

Define a validation for the fields

  validation validateReqFields on save { create; update; field Title, Description, Difficulty; }

Your source code should look like this:

managed implementation in class ZBP_R_JS_RECEIPT unique;
strict ( 2 );
with draft;
define behavior for ZR_JS_RECEIPT alias Receipt
persistent table ZJS_RECEIPT
draft table ZJS_RECEIPT_D
etag master LocalLastChangedAt
lock master total etag LastChangedAt
authorization master( global )
early numbering

{
...

   field ( mandatory )
   Title,
   Description,
   Difficulty;


  create;
  update;
  delete;

  validation validateReqFields on save { create; update; field Title, Description, Difficulty; }

...
  draft determine action Prepare
  {
    validation validateReqFields;
  }
...
}

Behavior Implementation

💡Use quick assist to add method definition in behavior class

💡%element-FIELDNAME = if_abap_behv=>mk-on results into connecting the error message and the field via activeLink in the MessagePopover

CLASS lhc_zr_js_receipt DEFINITION INHERITING FROM cl_abap_behavior_handler.
  PRIVATE SECTION.
    METHODS:
      validateReqFields FOR VALIDATE ON SAVE
        IMPORTING keys FOR Receipt~validateReqFields.
ENDCLASS.

CLASS lhc_zr_js_receipt IMPLEMENTATION.
  METHOD validateReqFields.

*--- read necessary data by keys
    READ ENTITIES OF zr_js_receipt IN LOCAL MODE
    ENTITY Receipt
    FIELDS ( Title Description Difficulty )
    WITH CORRESPONDING #( keys )
    RESULT DATA(receipts).

*--- do business logic
    LOOP AT receipts INTO DATA(receipt).
      APPEND VALUE #( %tky = receipt-%tky
                      %state_area = 'VALIDATION' ) TO reported-receipt.
      "check for field Title
      IF receipt-Title IS INITIAL.
        APPEND VALUE #( %tky = receipt-%tky ) TO failed-receipt.
        APPEND VALUE #( %tky = receipt-%tky
                            %state_area = 'VALIDATION'
                            %msg = me->new_message(
                             id       = 'ZJS_MESSAGES'
                             number   = '001'
                             v1       = 'Title'
                             severity = if_abap_behv_message=>severity-error
                            )
                            %element-Title = if_abap_behv=>mk-on
                             ) TO reported-receipt.
      ENDIF.

      "check for field Description
      IF receipt-Description IS INITIAL.
        APPEND VALUE #( %tky = receipt-%tky ) TO failed-receipt.
        APPEND VALUE #( %tky = receipt-%tky
                            %state_area = 'VALIDATION'
                            %msg = me->new_message(
                             id       = 'ZJS_MESSAGES'
                             number   = '001'
                             v1       = 'Description'
                             severity = if_abap_behv_message=>severity-error
                            )
                            %element-Description = if_abap_behv=>mk-on
                             ) TO reported-receipt.
      ENDIF.

      "check for field Difficulty
      IF receipt-Difficulty IS INITIAL.
        APPEND VALUE #( %tky = receipt-%tky ) TO failed-receipt.
        APPEND VALUE #( %tky = receipt-%tky
                            %state_area = 'VALIDATION'
                            %msg = me->new_message(
                             id       = 'ZJS_MESSAGES'
                             number   = '001'
                             v1       = 'Difficulty'
                             severity = if_abap_behv_message=>severity-error
                            )
                            %element-Difficulty = if_abap_behv=>mk-on
                             ) TO reported-receipt.
      ENDIF.

    ENDLOOP.

  ENDMETHOD.

ENDCLASS.

Child Entity Validation and the %path Parameter

🔢 Implemented in S/4 HANA ON PREMISE 2023 – SP02

Behavior Definition

managed implementation in class ZBP_FIXXX unique;
strict ( 2 );

define behavior for ZR_FIXXXKOPF alias Z4Kopf
persistent table zfixxxz4kopf
etag master LocalLastChangedAt
lock master
authorization master ( global )

{
...


  create ( precheck );
  update;
  delete;

  association _Z4Pos { create; }

  determination fillAuditFields on modify { create; }
  validation validateHeaderFields on save { create; field CompanyCode, SourceSystem, FiscalYear, FiscalPeriod; }
...
}

define behavior for ZR_FIAWVZ4POS alias Z4Position
persistent table zfiawvz4pos
lock dependent by _Z4Kopf
authorization dependent by _Z4Kopf
early numbering
{
...
  update;
  delete;

  association _Z4Kopf;

  determination fillAuditFields on modify { create; }
  validation validatePositionFields on save { create; field DebitCreditIndicator, SupplyingCountry, StateCentralBankIndicator; }
...

}

Behavior Implementation

CLASS lhc_z4position DEFINITION INHERITING FROM cl_abap_behavior_handler.

  PRIVATE SECTION.
    METHODS fillAuditFields FOR DETERMINE ON MODIFY
      IMPORTING keys FOR Z4Position~fillAuditFields.
    METHODS validatePositionFields FOR VALIDATE ON SAVE
      IMPORTING keys FOR Z4Position~validatePositionFields.

ENDCLASS.


CLASS lhc_z4position IMPLEMENTATION.
 ...
  METHOD validatePositionFields.
    DATA supplyingcountrys          TYPE SORTED TABLE OF t005 WITH UNIQUE KEY land1.
    DATA statecentralbankindicators TYPE SORTED TABLE OF t015l WITH UNIQUE KEY lzbkz.
    DATA validator                  TYPE REF TO zcl_xxx_validation.
    DATA validation_messages        TYPE bapiret2_t.

    " --- read relevant data
    READ ENTITIES OF zr_fixxx4kopf IN LOCAL MODE
         ENTITY Z4Position
         FIELDS ( DebitCreditIndicator SupplyingCountry StateCentralBankIndicator )
         WITH CORRESPONDING #( keys )
         RESULT DATA(z4entries).

    READ ENTITIES OF zr_fixxx4kopf IN LOCAL MODE
    ENTITY Z4Position BY \_Z4Kopf
    FROM CORRESPONDING #( z4entries )
    LINK DATA(z4kopf_z4position_links).

    " --- build supplyingcountry table
    supplyingcountrys = CORRESPONDING #( z4entries DISCARDING DUPLICATES MAPPING land1 = SupplyingCountry EXCEPT * ).
    DELETE supplyingcountrys WHERE land1 IS INITIAL.

    " --- build statecentralbankindicator table
    statecentralbankindicators = CORRESPONDING #( z4entries DISCARDING DUPLICATES MAPPING lzbkz = StateCentralBankIndicator EXCEPT * ).
    DELETE statecentralbankindicators WHERE lzbkz IS INITIAL.

    " --- setup validation
    validator = NEW zcl_xxx_validation( it_supplyingcountrys          = supplyingcountrys
                                        it_statecentralbankindicators = statecentralbankindicators ).

    " --- validate
    LOOP AT z4entries INTO DATA(z4entrie).
      APPEND VALUE #( %tky        = z4entrie-%tky
                      %state_area = 'VALIDATE_POSITIONFIELDS' ) TO reported-z4position.

      validation_messages = validator->validate_position_fields(
                                i_debitcreditindicator      = z4entrie-DebitCreditIndicator
                                i_supplyingcountry          = z4entrie-SupplyingCountry
                                i_statecentralbankindicator = z4entrie-StateCentralBankIndicator ).

      LOOP AT validation_messages INTO DATA(validation_message).
        IF validation_message-type = 'E'.
          APPEND VALUE #( %tky = z4entrie-%tky ) TO failed-z4position.
        ENDIF.

        APPEND VALUE #( %tky        = z4entrie-%tky
                        %state_area = 'VALIDATE_POSITIONFIELDS'
                        %msg        = new_message( id       = validation_message-id
                                                   number   = validation_message-number
                                                   severity = validator->map_severity( validation_message-type )
                                                   v1       = validation_message-message_v1
                                                   v2       = validation_message-message_v2
                                                   v3       = validation_message-message_v3
                                                   v4       = validation_message-message_v4 )
                        %path = VALUE #( z4kopf-%tky = z4kopf_z4position_links[ KEY id  source-%tky = z4entrie-%tky ]-target-%tky )                            ) TO reported-z4position.
      ENDLOOP.

    ENDLOOP.
  ENDMETHOD.
ENDCLASS.

💡 When implementing validations on a child entity in RAP, ensure that the %path parameter is provided when reporting messages. RAP requires the navigation path back to the parent entity to correctly associate the message with the business object instance. Without it, message processing may fail with the generic provider error /IWBEP/CM_V4S_RUN/000.

{"error":{"code":"/IWBEP/CM_V4S_RUN/000","message":"Unspecified provider error occurred. See Error Context and Call Stack.","@SAP__common.ExceptionCategory":"Provider_Application_Error"}}

Leave a Reply

Your email address will not be published. Required fields are marked *