Wednesday, March 12, 2014

Disable a button in the CRM webclient if mandatory fields exist

The CRM Webclient can be used for all kinds of actions. Most of these actions involve changes to data, which will be entered by a user. While processing these data changes, the CRM Webclient is able to validate the input and, for example, inform the user with a message if the input was incorrect. One of these checks is standard, namely the mandatory field check. Fields can be set as mandatory within the view configuration.

You could think of a scenario where you want to prevent users clicking a button if empty mandatory fields exist. For example, you do not want the user to be able to save if empty mandatory fields exist. Unfortunately this cannot be configured within the view, so we need to adjust the UI component, containing the view and the button. This blog describes the steps to perform.

Create method to check for empty mandatory fields

We need to create a method that checks for empty mandatory fields. An overview page could, for example, contain some views and a viewset, which also contains views again. Therefore we need a recursive mechanism to check all views directly linked to the overviewpage, but also the views within the viewsets.

As an importing parameter the method needs the viewarea context, to be able to check all relevant views. The result is a flag indicating if mandatory fields exist. Please note that we defined the method as a functional method, so it can be used directly in a logical expression.

METHOD empty_mandatory_field_exists.
     ls_viewarea_content TYPE ltype_va_content.
   LOOP AT it_viewarea_content INTO ls_viewarea_content.
     "Check view
     IF ls_viewarea_content-current-controller->get_empty_mandatory_fields( ) IS NOT INITIAL.
       rv_result = abap_true.
     "Check sub-views
     IF empty_mandatory_field_exists( ls_viewarea_content-current-controller->viewarea_cont ) EQ abap_true.
       rv_result = abap_true.

Enable or disable a button based on mandatory field check

We created the functional method to check for empty mandatory fields. Now we need to enable or disable the button based on the result of this check. While creating the button we call the functional method, passing the viewarea context, to check for empty mandatory fields. The result is stored in field “enabled” of the button.

METHOD if_bsp_wd_toolbar_callback~get_buttons.
     ls_button TYPE crmt_thtmlb_button_ext.
     ls_button-type = cl_thtmlb_util=>gc_icon_save.
     ls_button-text = 'Save'.
     ls_button-on_click = 'SAVE'.
    IF empty_mandatory_field_exists( me->viewarea_cont ) EQ abap_true.
      ls_button-enabled = abap_false.
      ls_button-enabled = abap_true.
    ls_button-page_id = me->component_id.
    APPEND ls_button TO rt_buttons.


Although we need some coding to enable or disable buttons based on the status of mandatory fields, it could be really useful. It’s up to you to decide whether or not you need this functionallity. At least you know how to do the trick.