ABAP - BOPF Validation
What's the easiest way to keep your data model clean? The simple answer you will learn today.
Table of contents
The topic of cleanliness of the data is also very important in the BOPF and provides with the validations a strong method of checking consistency. Similar to the determination, the validation in the data model is also created on the corresponding node. Today we'll show you how to define a validation and implement the appropriate logic in the class.
Create a validation
The validation, like the determination, is again defined on the corresponding node to which it applies. To do so, right-click and select "Consistency validation" to create a new empty object.
Fill in the name and description of the validation and then you can generate the class name again by suggestion of the system.
The condition must now be activated for certain actions (triggers). In this case, we activate it for all cases, except for deletion, since it makes no sense there. You can only go through validations during generation, if the data is then no longer changeable.
Implementation
The logic is again implemented in the created class implementing the interface /BOBF/IF_FRW_VALIDATION. In the EXECUTE method, all checks are implemented on the data, with the following example:
DATA:
lt_price_data TYPE ztest_t_price_scale.
eo_message = /bobf/cl_frw_factory=>get_message( ).
io_read->retrieve(
EXPORTING
iv_node = is_ctx-node_key
it_key = it_key
iv_fill_data = abap_true
IMPORTING
et_data = lt_price_data
).
LOOP AT lt_price_data REFERENCE INTO DATA(lr_price_data).
DATA(ld_error) = abap_false.
IF lr_price_data->min_quantity <= 0.
ld_error = abap_true.
ENDIF.
IF lr_price_data->discount < 0.
ld_error = abap_true.
ENDIF.
IF ld_error = abap_true.
APPEND VALUE #( key = lr_price_data->key ) TO et_failed_key.
eo_message->add_message(
EXPORTING
is_msg = VALUE #( msgty = 'E'
msgid = 'ZTEST_MSG'
msgno = '001'
)
iv_node = is_ctx-node_key
iv_key = lr_price_data->key
).
ENDIF.
ENDLOOP.
The data must first be read from the data model, because at this point only the keys are available. Afterwards, the data is checked for correctness. If the data has an error, then the key must be transferred to the failed keys table and a message can be generated. In any case, make sure that a new message instance has been created by the factory method before adding the message.
Test
When creating the test data, we will now receive a corresponding error message if the data is not filled correctly. The data can only be backed up if there are no more validation errors.
Conclusion
Consistent data in the data model should no longer be a problem for you. With today's article, you can do simple data checks at runtime. Have fun trying it out with your own solution.