RAP Web API Service: Validate Duplicate Business Keys Using Create Prechecks

Standard SAP duplicate key errors are technically correct but often not user-friendly. Using a RAP create precheck makes it possible to validate business keys upfront and return meaningful error messages to API consumers before the create operation is processed.

Behavior Definition

...
  create ( precheck );
  update;
  delete;
...  

Behavior Implementation

CLASS lhc_z4kopf DEFINITION INHERITING FROM cl_abap_behavior_handler.
  PRIVATE SECTION.
    METHODS get_global_authorizations FOR GLOBAL AUTHORIZATION
      IMPORTING
      REQUEST requested_authorizations FOR Z4Kopf
      RESULT result.
    METHODS precheck_create FOR PRECHECK
      IMPORTING entities FOR CREATE Z4Kopf.
ENDCLASS.


CLASS lhc_z4kopf IMPLEMENTATION.
  METHOD get_global_authorizations.
  ENDMETHOD.

  METHOD precheck_create.
    DATA validator           TYPE REF TO zcl_xxx_validation.
    DATA validation_messages TYPE bapiret2_t.

    LOOP AT entities INTO DATA(entity).
      validator = NEW zcl_xxx_validation( ).
      validation_messages = validator->validate_duplicate_delivery( i_companycode  = entity-CompanyCode
                                                                    i_sourcesystem = entity-SourceSystem
                                                                    i_fiscalyear   = entity-FiscalYear
                                                                    i_fiscalperiod = entity-FiscalPeriod ).

      LOOP AT validation_messages INTO DATA(validation_message).
        IF validation_message-type = 'E'.
          APPEND VALUE #( %key = entity-%key
                          %cid = entity-%cid ) TO failed-z4kopf.
        ENDIF.

        APPEND VALUE #( %key        = entity-%key
                        %cid        = entity-%cid
                        %state_area = 'VALIDATE_DUPLICATEENTRY'
                        %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 ) ) TO reported-z4kopf.

      ENDLOOP.
    ENDLOOP.
  ENDMETHOD.
ENDCLASS.

Leave a Reply

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