cancel
Showing results for 
Search instead for 
Did you mean: 

Handling of mandatory fields in ABAP RAP based web API?

Jelena
Active Contributor

Let's say I'm working on a web API to provide CRUD operations for a custom (Z...) table. I'm using a textbook unmanaged RAP scenario implementation with CDS, behavior definition and all that. There are, say, 20 fields in the table and 10 fields out of them are mandatory/required, i.e. must be filled in in order to perform create and update operations with a table. Fiori is not used in this scenario.

In the good old SEGW world I believe we had "not nullable" (?) checkbox that would trigger a message from SAP Gateway. Or we also had an option to get a list of all properties/values at the beginning of the corresponding method, run through them and provide a message (or multiple messages) respectively.

With ABAP RAP, unfortunately, I'm just not finding any examples of implementing similar functionality. There is "mandatory" designation for the field but it does not actually do anything. There doesn't seem to be any simple "not nullable" option.

The scenarios that talk about field validation are typically more complex and use FOR VALIDATION methods. Is this the way to go in this scenario? Creating 10 methods with boilerplate "IF ... IS INITIAL" code seems a bit excessive... I can do it if that's the way but is there by chance some other option?

There was a similar question about a Fiori-related scenario but the answer there doesn't really answer my question: https://answers.sap.com/questions/13576599/abap-restful-application-programming-model-how-do.html

Thanks!

Accepted Solutions (1)

Accepted Solutions (1)

Andre_Fischer
Product and Topic Expert
Product and Topic Expert

Hi Jelena,

I have posted an answer how to check not only mandatory fields that have been defined as such in the behavior defintion as described by Kushagra but also the ones that change their status dynamically based on the (features : instance) key word.

How to check mandatory fields in RAP | SAP Blogs

Unfortunately so far the validation of mandatory fields does not work out of the box and requires some coding efforts. But we are working on making it easier ;-).

Kind regards,

Andre

Jelena
Active Contributor

Thanks for a reply, Andre! I'm closing the question, so marked this as an answer even though it's not what I was hoping for. 🙂 I hope that mandatory can start working as the name suggests and this post will become obsolete. Until then, I feel picking unmanaged option was a mistake on our part as it also removes the ability to use validation method. (You can still write code to validate but there just isn't a "clean" place to put it in). I think "managed with custom save" is what we actually needed and I guess we just were not well informed on actual differences of that and unmanaged. I suspect we're not the only ones, so maybe it's something that could be explained in more detail. Thanks again!

Answers (2)

Answers (2)

RichHeilman
Developer Advocate
Developer Advocate

Hey Jelena. The help on this topic says the following.

"Defines that it is mandatory to enter values into the specified fields before persisting them on the database. These fields are marked as mandatory on the user interface in an OData scenario. However, there's no runtime check for mandatory fields and no runtime error occurs if a mandatory field is not filled. If a runtime check is required, the application developer should implement it using a validation on save."

So it would appear that you would have to do the check in your implementation class. I have to say, for me, this is not ideal. I would have expected that the "mandatory" keyword would do a little more than simply documenting the fact in the OData service, or putting the "mandatory star" next to the field on the UI. I'll check with PM on this.

Cheers,

Rich

Jelena
Active Contributor

Thank you! It was also my initial impression that when field is labeled as "mandatory", its value should be at minimum checked for existence. Alas, ABAP RAP let me down here. 🙂 Hopefully this can be improved and simplified. After all, OBLIGATORY worked before in classic ABAP.

kushagratandon
Explorer

Hi Jelena,

The field (mandatory) just marks the fields with * in the Fiori UI Screen. It doesn't validates it.

For validation, you will have to create validation method that may trigger on save:

field ( mandatory : create, readonly : update ) Zxxkey, Zxxkeytext;
field ( mandatory ) Zxxmaterial, Zxxwork;

validation validatematerial on save
  { create; update; }

In validatematerial method, you may put all the validations required or you can even add in more validation methods for different fields and actions. The example is shown as below:

METHOD validatematerial.
    DATA lv_companycode TYPE char4.
    READ ENTITY zc_xxx_xx FROM VALUE #(
          FOR <root_key> IN keys ( %key-Zxxkey = <root_key>-Zxxkey
                                   %control = VALUE #( Zxxkey = if_abap_behv=>mk-on
  Zxxkeytext = if_abap_behv=>mk-on
  Zxxmaterial = if_abap_behv=>mk-on
  Zxxwork = if_abap_behv=>mk-on
) ) )
          RESULT DATA(lt_xxxid).
    LOOP AT lt_xxxid INTO DATA(ls_xxxid).
      IF ls_xxxid-Zxxkey IS INITIAL.
        APPEND VALUE #( Zxxkey = ls_imcid-Zxxkey ) TO failed-aliasnameforbehavior.
        APPEND VALUE #(  Zxxkey = ls_imcid-Zxxkey
                         %msg      = new_message( id       = 'ZMSG_xxx'
                                                  number   = 'xxx'
                                                  v1       = 'Key ID'
                                                  severity = if_abap_behv_message=>severity-error )
                         %element-Zxxkey  = if_abap_behv=>mk-on ) TO reported-aliasnameforbehavior.
      ENDIF.
ENDLOOP.

You may face issue in authorization also if you are on 2020 version. Authorization check only triggers for Update and Delete scenarios in managed scenarios in RAP. For create, you will have to put authorization checks on validations on Save. So check out all your requirements before continuing app development with RAP.

Regards,

Kushagra

Jelena
Active Contributor

Thank you! I appreciate you sharing a code example, it's very helpful.

Jelena
Active Contributor

Just a note for those who might try this option: it works only in managed scenario or unmanaged with a draft. Ours is unmanaged without a draft and I'm getting a syntax error for validation command. Bummer.