Make a field Dynamically Mandatory
Please note that this blog was originally created internally and i apologize for any sarcasm or criticism that i unintentionally leave in the blog. It is my hope that this basic guide will assist anyone searching with creating dynamic mandatory fields by providing a clear and detailed description how to do it.
When a field is required to be mandatory, the easiest method is to set the field as statically “Mandatory” via the page configuration. In order to do this you would go to the page configuration view and set the “Mandatory” checkbox. This means that the field is disabled on the current configuration of this view. Often, however, we have some requirement to set a field as mandatory, based off some entries or values of the current view context. This is completed as follows:
Please note that this tutorial assumes the developer has a general understanding of UI components, context nodes, and their various getter methods
Step 1: Redefine / Generate the P Getter method
The P getter is a ‘Property’ getter method that receives an input argument dependent on a set functionality check. Sometimes this method is already defined and you simply have to right click and ‘Redefine’ the method. Other times, as shown below, you need to right click and ‘Generate P-GETTER’.
The result should be as follows:
Step 2: Modify the P Getter method to handle the check for Mandatory
When the awesome P getter method is repeatedly called, a parameter is passed to show what check is being executed so we create a switch / case statement to handle the input variable.
Double click into the above highlighted method and enter the following:
As shown you do your custom logic in the area mentioned in comments, returning abap_true(X) or abap_false( ) depending on your custom logic.
In my example i need to check an attribute in a structure of the current view controller class so this will be easy as the attribute is stored as an abap_bool already.
Step 3: Set the input type to ensure no ‘complications’ occur
One important thing to note about the P Getter is, as previously, mentioned… it is repeatedly called and the return value is different depending on what the input was. Throughout the processing of preparing a page, setting details and so on the P getter is called with various inputs. Due to this…, it is important to set the input type here as we have redefined the property getter and the field may no longer know what type of input it is. The following code is required:
I should point out that this is the same way we change the input type to other types. There are a range of options you are welcome to explore. Some examples are:
cl_bsp_dlc_view_descriptor=>field_type_picklist – (a drop down list box)
cl_bsp_dlc_view_descriptor=>field_type_textarea – (a text area field)
cl_bsp_dlc_view_descriptor=>field_type_checkbox – (you get the idea…)
At this point the functionality should ‘Sort of’ work or … ‘Function as Designed’ according to the following official link:
Unfortunately the reality is that this is flawed and further steps are required to fully make your view attributes dynamically mandatory. To see this just leave this as is and try changing the settings as required to trigger your mandatory rules. You should find that initially the page finds the mandatory fields without any problem. But when changing other fields that may make your field become mandatory, the field will not realise it is now mandatory until 2 page refreshes. This is, however, the first step
The follow up which includes a brief summary of the issues mentioned can be found here