The following texts were partially or completely generated with the help of generative AI models.
A very common activity when working with RAP and Fiori Elements is the validation of user input. Unfortunately, validations are always performed only on save, and you often want to give the user feedback earlier - especially when using DRAFT mode. What is the best way to accomplish this? The most elegant approach in this case is to use so-called prechecks. So far so 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) 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;
}
Here the following is generated:
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 reveals 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 run through a single loop, you then combine the almost identical structures.
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).
* Here are our checks!
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, since only the delta (i.e. the changed fields) is transmitted via PATCH. This should ideally happen before the loop - you can find my personal favorite approach here.
I hope this helps you and makes your life easier - have fun!🙂 Sören Schlegel



