Collection of code snippets which I regularly use in MPC_EXT DEFINE method.
I use these code in conjunction with Fiori Elements templates. There are CDS annotations which achieve the same end result but if you are not on the latest version then these code snippet will come handy.
Setting Date Format and Filter Restriction
Display date in short form in report instead of date + time format.
The filter-restriction makes date filter to display calendar control where user can specify from and to date.
data : lo_annotation type ref to /iwbep/if_mgw_odata_annotation,
lo_property type ref to /iwbep/if_mgw_odata_property,
lo_entity_set type ref to /iwbep/if_mgw_odata_entity_set.
try .
data(lo_entity_type) = model->get_entity_type( iv_entity_name = gc_applog ).
lo_property = lo_entity_type->get_property( iv_property_name = 'Systemdate' ).
lo_annotation = lo_property->/iwbep/if_mgw_odata_annotatabl~create_annotation( 'sap' ).
lo_annotation->add( iv_key = 'display-format' iv_value = 'Date' ).
lo_annotation->add( iv_key = 'filter-restriction' iv_value = 'interval' ).
catch /iwbep/cx_mgw_med_exception .
endtry.
CDS annotation @Consumption.filter.selectionType: #INTERVAL will have a similar effect. Depending on UI5 version and descriptors in manifest file filter-restriction can also render dynamic date picker in the filter bar. Read more about it in blog Fiori Element List Report: Dynamic Date Picker.
Set the reference field to enable/disable Action
When you have an action defined on the entity, you can make it enabled/disabled by linking that action to a boolean field in that entity using applicable-path.
For example, where some of the Purchase orders are not relevant for ‘Approve’ action you can link ‘Approve’ action (Function Import) to a boolean field which will have false value for those Purchase orders and when user select those Purchase orders, ‘Approve’ action will be disabled in UI.
data : lo_annotation type ref to /iwbep/if_mgw_odata_annotation,
lo_property type ref to /iwbep/if_mgw_odata_property,
lo_entity_set type ref to /iwbep/if_mgw_odata_entity_set,
lo_function type ref to /iwbep/if_mgw_odata_action.
try .
lo_function = model->get_action( iv_action_name = 'SetDefault' ) .
lo_annotation = lo_function->/iwbep/if_mgw_odata_annotatabl~create_annotation( 'sap' ).
lo_annotation->add( iv_key = 'applicable-path'
iv_value = 'IsAction' ).
catch /iwbep/cx_mgw_med_exception .
endtry.
Make filter mandatory in List Report
Guess that is self explanatory.
data : lo_annotation type ref to /iwbep/if_mgw_odata_annotation,
lo_property type ref to /iwbep/if_mgw_odata_property,
lo_entity_set type ref to /iwbep/if_mgw_odata_entity_set,
lo_entity_type type ref to /iwbep/if_mgw_odata_entity_typ.
try .
"make plant mandatory and single value
lo_entity_type = model->get_entity_type( iv_entity_name = gc_pidocument ).
lo_property = lo_entity_type->get_property( iv_property_name = 'Plant' ).
lo_annotation = lo_property->/iwbep/if_mgw_odata_annotatabl~create_annotation( 'sap' ).
lo_annotation->add( iv_key = 'required-in-filter'
iv_value = 'true' ).
lo_annotation->add( iv_key = 'filter-restriction'
iv_value = 'single-value' ).
catch /iwbep/cx_mgw_med_exception .
endtry.
CDS annotation for same is @Consumption.filter.mandatory: true .
Disable Conversion Exit
Some time you may not want conversion exit to kick in.
model->get_entity_type( gc_materialpick )->get_property( 'WBSId' )->disable_conversion( ) .
model->get_action( 'WorkListAdd' )->get_input_parameter( 'WBSId' )->disable_conversion( ) .
model->get_entity_type( 'LANGUAGE' )->get_property( 'Language' )->disable_conversion( ).
Display Drop Down instead of Value help in new window
When you like to display dropdown on a field instead of F4 value help dialog you can add sap:value-list=”fixed-values” on the field.
data : lo_annotation type ref to /iwbep/if_mgw_odata_annotation,
lo_property type ref to /iwbep/if_mgw_odata_property,
lo_entity_set type ref to /iwbep/if_mgw_odata_entity_set.
try .
data(lo_entity_type) = model->get_entity_type( iv_entity_name = gc_zcsalesorderitemtype ).
lo_property = lo_entity_type->get_property( iv_property_name = 'OverallSDProcessStatus' ).
lo_property->set_value_list( /iwbep/if_mgw_odata_property=>gcs_value_list_type_property-fixed_values ) .
catch /iwbep/cx_mgw_med_exception .
endtry.
In CDS when you define value help provider view with @ObjectModel.resultSet.sizeCategory: #XS it will have same effect.
Very nice article. How to get read parameters from metadataUrlParams in MPC class?
How to validate for age field that it should take age only above 18?
How to validate age field?
Need help immediately, I want to load my date “20240217” instead of \/Date(1708041600000)\/
in payload how to change the format, Please help me out.
Thanks