Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Sschlegel
Participant

A very common activity when working with RAP and Fiori Elements is the validation of user input. Unfortunately, validations are only ever carried out when saving and you often want to give the user feedback earlier - especially when using draft mode. What is the best way to do this? The way to go in this case is to use prechecks. So far all good, but unfortunately the wizard makes it a bit more difficult than necessary, because after defining the prechecks in the behavior definition (or the behavior projection, which I prefer to use) it generates 2 methods if you want to perform the same check for CREATE and UPDATE - e.g. that the customer number should always be filled.

 

define behavior for ZC_MyEntity alias Entity

{
  use create ( augment, precheck );
  use update ( augment, precheck );
  use delete;
}

 

The following is generated here:

 

    METHODS precheck_create FOR PRECHECK
      IMPORTING entities FOR CREATE Entity.

    METHODS precheck_update FOR PRECHECK
      IMPORTING entities FOR UPDATE Entity.

 

However, a look at the documentation shows that there is a simpler way:

 

    METHODS precheck FOR PRECHECK
      IMPORTING entities_create FOR CREATE Entity
                entities_update FOR UPDATE Entity.

 

So that you only have to go through one loop, you then pack the almost identical structures together.

 

  METHOD precheck.

    DATA(entities_check) = entities_create.

    entities_check = VALUE #( BASE entities_check FOR entity_update IN entities_update
                             ( CORRESPONDING #( entity_update ) ) ).

    LOOP AT entities_check INTO DATA(entitiy_check).

    ENDLOOP.

  ENDMETHOD.

 

This approach saves code duplication and also makes maintenance easier.

Important

If you use DRAFT, then, depending on the use case, you unfortunately still have to read the "unchanged fields" during the update, as only the delta (i.e. the changed fields) is transferred via a PATCH. This is hopefully done before the loop - you can find here my personal favorit on how to this.

I hope this helps you and makes your life easier - enjoy 😀

 

 

Labels in this area