SAP MDG · VALIDATION DERIVATION
How do I implement MDG entity validation using USMD_RULE_SERVICE and a shared utility class pattern?
Use the USMD_RULE_SERVICE validation framework for entity checks, read the application context from CL_USMD_APP_CONTEXT, build errors with USMD_S_MESSAGE, and enrich messages with row navigation so the UI can jump to the affected record.
Use the USMD_RULE_SERVICE validation framework for entity checks, read the application context from CL_USMD_APP_CONTEXT, build errors with USMD_S_MESSAGE, and enrich messages with row navigation so the UI can jump to the affected record.
In SAP MDG, entity validation is typically implemented in the BAdI USMD_RULE_SERVICE, method CHECK_ENTITY. The reusable pattern is to read the current application context, evaluate the generic entity rows, build messages in USMD_S_MESSAGE, and then enrich those messages with row navigation so the user can jump directly to the failing record.
Process flow
- Open the MDG validation BAdI implementation for
USMD_RULE_SERVICE. - Implement or enhance CHECK_ENTITY for the relevant entity type.
- Read the current application context with CL_USMD_APP_CONTEXT=>GET_CONTEXT( ).
- Gate the logic by model and entity type before running checks.
- Read entity data from the current change request if the rule depends on other fields or header values.
- Build each error as
USMD_S_MESSAGEwith MSGID, MSGNO, MSGTY, FIELDNAME, and optional variables. - Add row navigation to improve UI usability.
- Append or merge messages into the target message table and activate the objects.
Referenced tables
| Object | Purpose |
|---|---|
USMD_S_MESSAGE | Structure used to build a single MDG validation message row. |
USMD_T_MESSAGE | Internal table type used to return validation messages from the rule logic. |
USMD_T_ENTITY | Entity filter table used when reading entity data from the change request. |
ILLUSTRATIVE ABAP SAMPLE
Reusable MDG utility pattern from the source example
This is the exact helper-class pattern shown in the source, useful as a reusable backend for CHECK_ENTITY validation logic.
1CLASS zcl_usmd_rule_skill_lib DEFINITION
2 PUBLIC
3 FINAL
4 CREATE PUBLIC.
5
6 PUBLIC SECTION.
7 CLASS-METHODS get_context
8 IMPORTING io_model TYPE REF TO if_usmd_model_ext
9 EXPORTING eo_app_context TYPE REF TO if_usmd_app_context
10 ev_crid TYPE usmd_crequest
11 ev_crtype TYPE usmd_crequest_type
12 ev_crstep TYPE usmd_crequest_appstep
13 ev_process TYPE usmd_process
14 RAISING cx_static_check.
15
16 CLASS-METHODS read_bp_header_grouping
17 IMPORTING io_model TYPE REF TO if_usmd_model_ext
18 iv_crequest TYPE usmd_crequest
19 EXPORTING ev_bu_group TYPE bu_group.
20
21 CLASS-METHODS append_error
22 CHANGING ct_message TYPE usmd_t_message
23 IMPORTING iv_msgid TYPE symsgid
24 iv_msgno TYPE symsgno
25 iv_field TYPE usmd_fieldname OPTIONAL
26 iv_msgv1 TYPE symsgv OPTIONAL
27 iv_msgv2 TYPE symsgv OPTIONAL.
28
29 CLASS-METHODS add_row_navigation
30 IMPORTING io_model TYPE REF TO if_usmd_model_ext
31 iv_crequest TYPE usmd_crequest
32 iv_entitytype TYPE usmd_entity
33 CHANGING ct_message TYPE usmd_t_message.
34
35 CLASS-METHODS append_rule_messages
36 CHANGING ct_target TYPE usmd_t_message
37 ct_source TYPE usmd_t_message.
38 PROTECTED SECTION.
39 PRIVATE SECTION.
40ENDCLASS.
41
42CLASS zcl_usmd_rule_skill_lib IMPLEMENTATION.
43
44 METHOD get_context.
45 eo_app_context = cl_usmd_app_context=>get_context( ).
46 IF eo_app_context IS NOT BOUND.
47 RAISE EXCEPTION TYPE cx_static_check.
48 ENDIF.
49
50 CHECK io_model->d_usmd_model = 'BP'.
51
52 eo_app_context->get_attributes(
53 IMPORTING
54 ev_crequest_id = ev_crid
55 ev_crequest_type = ev_crtype
56 ev_crequest_step = ev_crstep
57 ev_process = ev_process ).
58 ENDMETHOD.
59
60 METHOD read_bp_header_grouping.
61 DATA: lt_filter TYPE usmd_t_entity,
62 ls_filter LIKE LINE OF lt_filter.
63 FIELD-SYMBOLS: <lt_header> TYPE ANY TABLE,
64 <ls_header> TYPE any,
65 <lv_group> TYPE any.
66
67 ls_filter = 'BP_HEADER'.
68 INSERT ls_filter INTO TABLE lt_filter.
69
70 io_model->read_entity_data_all(
71 EXPORTING
72 i_fieldname = 'BP_HEADER'
73 if_active = abap_false
74 i_crequest = iv_crequest
75 it_entity_filter = lt_filter
76 IMPORTING
77 et_data_entity = DATA(lt_entity_data)
78 et_message = DATA(lt_msg) ).
79
80 READ TABLE lt_entity_data INTO DATA(ls_entity_data) WITH KEY usmd_entity = 'BP_HEADER'.
81 IF sy-subrc = 0.
82 ASSIGN ls_entity_data-r_t_data->* TO <lt_header>.
83 IF <lt_header> IS ASSIGNED.
84 READ TABLE <lt_header> ASSIGNING <ls_header> INDEX 1.
85 IF sy-subrc = 0.
86 ASSIGN COMPONENT 'BU_GROUP' OF STRUCTURE <ls_header> TO <lv_group>.
87 IF <lv_group> IS ASSIGNED.
88 ev_bu_group = <lv_group>.
89 ENDIF.
90 ENDIF.
91 ENDIF.
92 ENDIF.
93 ENDMETHOD.
94
95 METHOD append_error.
96 DATA ls_message TYPE usmd_s_message.
97 ls_message-msgid = iv_msgid.
98 ls_message-msgno = iv_msgno.
99 ls_message-msgty = 'E'.
100 ls_message-fieldname = iv_field.
101 ls_message-msgv1 = iv_msgv1.
102 ls_message-msgv2 = iv_msgv2.
103 APPEND ls_message TO ct_message.
104 ENDMETHOD.
105
106 METHOD add_row_navigation.
107 zcl_mdg_suppl_utility=>get_message_row_id(
108 EXPORTING
109 io_model = io_model
110 id_crequest = iv_crequest
111 id_entitytype = iv_entitytype
112 CHANGING
113 ct_message = ct_message ).
114 ENDMETHOD.
115
116 METHOD append_rule_messages.
117 IF ct_source IS NOT INITIAL.
118 APPEND LINES OF ct_source TO ct_target.
119 CLEAR ct_source.
120 ENDIF.
121 ENDMETHOD.
122
123ENDCLASS.The remaining configuration, implementation details, and testing guidance continue from this answer more…
Related questions and keywords
Alternative questions
- How do I write CHECK_ENTITY validation logic in SAP MDG?
- How do I build reusable MDG validation helper methods for
USMD_RULE_SERVICE? - How do I add row navigation and error messages in an MDG validation class?
Possible questions
- How do I validate a BP entity in MDG using a utility class pattern?
- How do I read change request context inside MDG validation code?
- How do I attach field-level error messages for MDG validation?
- How do I add navigation to the offending row in MDG validation messages?
- What is the ABAP pattern for reusable MDG rule methods?