cancel
Showing results for 
Search instead for 
Did you mean: 

ABAP RESTful Application Programming Model - How do I validate mandatory fields?

adamharkus
Participant
0 Kudos

(Using RAP in an unmanaged scenario with SAP API's via consumption models)

I have the following Behaviour Definition setup, with a root SalesOrder entity and it's associated SalesOrderItem entity.

I've setup the mandatory control field on Material and RequestedQty, which adds the red asterisk to those fields in the UI (Elements).


unmanaged;
define behavior for /N4C02/CE_ORDER_SEARCH alias SalesOrder
implementation in class /n4c02/bp_ce_order_search unique
lock master
authorization master ( instance )
{
association _Items { create ; }
}
define behavior for /N4C02/CE_ORDER_SEARCH_ITEMS alias SalesOrderItem
implementation in class /n4c02/bp_ce_order_srch_items unique
lock dependent by _SalesOrder
authorization dependent by _SalesOrder
{
create; field ( readonly ) SalesOrder, SalesOrderItem, SalesOrderItemText;
field ( mandatory ) Material, RequestedQuantity;
association _SalesOrder;
}

Unfortunately, that's all it does...

I've looked a little further into feature controls, but that seems to be focussed on the 'Actions' (e.g. Create, Delete, or user defined).

My question is how exactly are the mandatory fields validated in the Behaviour Implementation?

Is this done manually by checking them individually and throwing an exception? ...

...Or is there a way to perhaps disable the Save button until all mandatory fields have a value?

Accepted Solutions (1)

Accepted Solutions (1)

Andre_Fischer
Product and Topic Expert
Product and Topic Expert

I faced the same problem.

There is unfortunately no out of the box support to check mandantory fields.

Fields can be either mandatory when being defined as such in the behavior definition and in addition using feature control.

For my new Fiori based UI of the RAP Generator I also had to check for mandatory fields.

The trick is to perform a permission request that delivers as a response whether the field is mandatory or not.

Kind regards,

Andre

    DATA permission_request TYPE STRUCTURE FOR PERMISSIONS REQUEST zdmo_c_rapgeneratorbo.

    DATA instance_feature_keys TYPE TABLE FOR INSTANCE FEATURES KEY zdmo_i_rapgeneratorbo\\rapgeneratorbo.
    DATA requested_features  TYPE STRUCTURE FOR INSTANCE FEATURES REQUEST zdmo_i_rapgeneratorbo\\rapgeneratorbo .

    DATA result_instance_features TYPE TABLE FOR INSTANCE FEATURES RESULT zdmo_i_rapgeneratorbo\\rapgeneratorbo.
    DATA failed_instance_features TYPE RESPONSE FOR FAILED EARLY zdmo_i_rapgeneratorbo  .
    DATA reported_instance_features TYPE RESPONSE FOR REPORTED EARLY zdmo_i_rapgeneratorbo .

    DATA(description_permission_request) = CAST cl_abap_structdescr( cl_abap_typedescr=>describe_by_data_ref( REF #( permission_request-%field ) ) ).
    DATA(components_permission_request) = description_permission_request->get_components(  ).

    DATA(description_requested_features) = CAST cl_abap_structdescr( cl_abap_typedescr=>describe_by_data_ref( REF #( requested_features-%field ) ) ).
    DATA(components_requested_features) = description_requested_features->get_components(  ).

    LOOP AT components_permission_request INTO DATA(component_permission_request).
      permission_request-%field-(component_permission_request-name) = if_abap_behv=>mk-on.
    ENDLOOP.

    LOOP AT components_requested_features INTO DATA(component_requested_features).
      requested_features-%field-(component_requested_features-name) = if_abap_behv=>mk-on.
    ENDLOOP.

    " Get Permissions without instance keys, as we are only interested in static / global feature control ( mandatory )
    GET PERMISSIONS OF  zdmo_c_rapgeneratorbo
      ENTITY rapgeneratorbo
      FROM VALUE #(  )
    REQUEST permission_request
    RESULT DATA(permission_result)
    FAILED DATA(f_p)
    REPORTED DATA(r_p).



    instance_feature_keys = VALUE #( FOR key IN keys
       (
         %is_draft  = key-%is_draft
         rapnodeuuid  = key-rapnodeuuid
        "Component Groups
        %key  = key-%key
        %tky  = key-%tky
        %pky  = key-%pky
        ) ) .


    get_instance_features(
      EXPORTING
        keys               = instance_feature_keys
        requested_features = requested_features
      CHANGING
        result             = result_instance_features
        failed             = failed_instance_features
        reported           = reported_instance_features
    ).

    "Get Mandatory Fields
    READ ENTITIES OF zdmo_i_rapgeneratorbo IN LOCAL MODE
    ENTITY rapgeneratorbo
      ALL FIELDS
      WITH CORRESPONDING #( keys )
      RESULT DATA(entities).

    "Do check
    LOOP AT entities INTO DATA(entity).

      LOOP AT components_permission_request INTO component_permission_request.

        IF permission_result-global-%field-(component_permission_request-name) = if_abap_behv=>fc-f-mandatory
           AND entity-(component_permission_request-name) IS INITIAL.

          APPEND VALUE #( %tky = entity-%tky )
                 TO failed-rapgeneratorbo.
          APPEND VALUE #( %tky = entity-%tky
*                   %element-%field-(component_request-name) = if_abap_behv=>mk-on
                   %msg = new_message(
                            id       = 'ZDMO_CM_RAP_GEN_MSG'
                            number   = 013
                            severity = if_abap_behv_message=>severity-error
                            v1       = |{ component_permission_request-name }|
                          )
                           )
          TO reported-rapgeneratorbo.
        ENDIF.
      ENDLOOP.

      LOOP AT components_requested_features INTO component_requested_features.

        IF entity-(component_requested_features-name) IS INITIAL.
          APPEND VALUE #( %tky = entity-%tky )
                 TO failed-rapgeneratorbo.
          APPEND VALUE #( %tky = entity-%tky
*                   %element-%field-(component_request-name) = if_abap_behv=>mk-on
                   %msg = new_message(
                            id       = 'ZDMO_CM_RAP_GEN_MSG'
                            number   = 013
                            severity = if_abap_behv_message=>severity-error
                            v1       = |{ component_requested_features-name }|
                          )
                           )
          TO reported-rapgeneratorbo.
        ENDIF.

      ENDLOOP.


    ENDLOOP.


adamharkus
Participant
0 Kudos

Thanks a lot andre.fischer I'll check this out.

adamharkus
Participant
0 Kudos

HI andre.fischer

This got me curious about Draft mode (which is needed to setup Validations in the Behaviour Definition).

I attempted to set this up, but also ran into problems, so I'm not sure if it's possible in out scenario

https://answers.sap.com/questions/13577693/abap-rap-model-draft-is-not-supported-in-service-b.html

Answers (0)