Saturday, June 9, 2018

Update all dynpro/screen fields in one shot

Lazyness is one of the engines for iprovements. So this is the root cause of this short article.

Once you are trying to update a classic Dynpro you should call FM DYNP_VALUES_UPDATE. Ok, you can collect ITAB for each screen field providing name and value as on bellow example.

      ls_dynpfield-fieldname  = lv_screen_field_name.
      ls_dynpfield-fieldvalue = lv_screen_field_val.
      APPEND ls_dynpfield TO lt_dynpfield.

It is still annoying to do so for each screen field...

Imagine, you can set the screen values as you need and run just once some certain method doing the whole screen update job. Here it is, there is just one simple call. You can reuse it as many times you want. The only condition is to have screen connected to a certain structure variable. That's all.


Screen update method call

    " Update screen
     lo_ctrl_screen->update_screen_fields(
      iv_prog       sy-cprog                                                      " Program name
      iv_screen     '0200'                       " Dynpro number
      iv_struc_name 'GS_SCREEN_STRUCTURE_NAME' " Name of structure holding the screen values
      is_struc_data gs_real_structure                            " Structure variable itself
    ).




The method source code: 


  METHOD update_screen_fields.

    DATA:
      lt_dynpfield         TYPE STANDARD TABLE OF dynpread,
      ls_dynpfield         LIKE LINE OF lt_dynpfield,
      lo_struc_descr       TYPE REF TO cl_abap_structdescr.

    lo_struc_descr ?= cl_abap_structdescr=>describe_by_datais_struc_data ).
    CHECK sy-subrc 0.

    FIELD-SYMBOLS:
      <fs_comp> LIKE LINE OF lo_struc_descr->components,
      <fs_val>  TYPE any.

    LOOP AT lo_struc_descr->components ASSIGNING <fs_comp>.
      ASSIGN COMPONENT <fs_comp>-name OF STRUCTURE is_struc_data TO <fs_val>.

      IF <fs_val> IS NOT INITIAL.
        CLEAR ls_dynpfield.
        CONCATENATE iv_struc_name <fs_comp>-name INTO ls_dynpfield-fieldname.
        ls_dynpfield-fieldvalue <fs_val>.
        APPEND ls_dynpfield TO lt_dynpfield.
      ENDIF.

    ENDLOOP.

    " Update screen
    IF lt_dynpfield IS NOT INITIAL.
      CALL FUNCTION 'DYNP_VALUES_UPDATE'
        EXPORTING
          dyname     iv_prog    "Program name
          dynumb     iv_screen  "Screen number
        TABLES
          dynpfields lt_dynpfield
        EXCEPTIONS
          OTHERS     0.
    ENDIF.

  ENDMETHOD.                    "update_screen_fields