Category Archive ABAP code

FM to check if executed in dialog mode

FM to check if Gui is active :


DATA : lw_gui_actif TYPE c.
  CALL FUNCTION 'GUI_IS_AVAILABLE'
    IMPORTING
      return = lw_gui_actif.

FM to check if code is executed in dialog mode :


DATA : lw_is_dialog TYPE xfeld.
CALL FUNCTION 'ZFM_CHECK_DIALOG_MODE'
  IMPORTING
    ew_is_dialog = lw_is_dialog.  " = X si en mode Dialog, vide sinon

Read More

Create an ALV with FM REUSE_ALV_GRID_DISPLAY

Abap code example to display an ALV with FM REUSE_ALV_GRID_DISPLAY.

Read More

Generate random string with ABAP

Use FM : GENERAL_GET_RANDOM_STRING.

Example :


  DATA : lw_guid TYPE string.

  CALL FUNCTION 'GENERAL_GET_RANDOM_STRING'
    EXPORTING
      number_chars  = 32
    IMPORTING
      random_string = lw_guid.

Convert date to external format

Convert date to default external format :


DATA : lw_date_ext TYPE char10,
       lw_date TYPE datum. 
WRITE lw_date TO lw_date_ext.

Other way to do that since 7.40 :


DATA lv_date TYPE d value '20190718'.
DATA(l_user_format) = |{ lv_date DATE = USER }|. "RAW, ISO, USER, ENVIRONMENT
WRITE l_user_format.

Convert date to specified external format :


DATA : lw_date_ext TYPE char10,
       lw_date     TYPE datum.

  CALL FUNCTION 'SLS_MISC_CONVERT_TO_DATE'
    EXPORTING
      p_date        = lw_date
      p_date_format = 'DD.MM.YYYY'
    IMPORTING
      p_date_string = lw_date_ext.

See also Convert date to internal format.

Convert date to internal format

With 7.40 :


DATA : lw_date_ext TYPE char10,
       lw_date TYPE datum.

lw_date = |{ lw_date_ext+6(4) }{ lw_date_ext+3(2) }{ lw_date_ext(2) }|.

With FM :


DATA : lw_date_ext TYPE char10,
       lw_date     TYPE datum.

  CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
    EXPORTING
      date_external = lw_date_ext    " ==> 10.10.2017
    IMPORTING
      date_internal = lw_date        " ==> 20171010
    EXCEPTIONS
      date_external_is_invalid = 1.

See also Convert date to external format.

Increase line size of report

Synthax to increase line size of report :
(To be specified at begining of report with report name declaration)


  REPORT zreport_name LINE-SIZE 130.

Calculate the runtime with ABAP

Calculate runtime in microseconds :


DATA: lw_t1          TYPE i,
      lw_t2          TYPE i,
      lw_elapsed     TYPE i.
 
* Get the time and put in lw_t1.
GET RUN TIME FIELD lw_t1.
 
* Wait 3 seconds.
CALL FUNCTION 'ENQUE_SLEEP'
  EXPORTING
    SECONDS = 3.
 
* Get the time and put in lw_t2.
GET RUN TIME FIELD lw_t2.
 
* Calculate the different between lw_t2 and lw_t1.
lw_elapsed = lw_t2 - lw_t1.          " In microseconds.
lw_elapsed = ELAPSED / 1000000.      " In seconds.
 
* Display the runtime.
WRITE:/ ' Runtime =', lw_elapsed, 'seconds'.

If your measure may overload 36 min, do not store the run time in a type i (integer) variable.
You should use a type numeric, like :


DATA: lw_t1(200)          TYPE n,

Use statement CLIENT SPECIFIED when mandant is specified in SELECT statement

If you want to add the mandant in your where clause (to force use of an index for instance), you have to add statement CLIENT SPECIFIED.


    SELECT * FROM table_name CLIENT SPECIFIED INTO TABLE gi_table_name
      FOR ALL ENTRIES IN li_table
      WHERE mandt     = sy-mandt                 " To force Index 001
        AND field1    = li_table-field1
        AND field1    = li_table-field2.

Display messages in popup


  CALL FUNCTION 'C14ALD_BAPIRET2_SHOW'
    TABLES
      I_BAPIRET2_TAB = T_RETURN_MESSAGE. " T_RETURN with type BAPIRET2_TAB

Add message in BAPIRET2 table


FORM add_message      USING pw_type TYPE sy-msgty
                            pw_id   TYPE sy-msgid
                            pw_num  TYPE sy-msgno
                            pw_v1   "TYPE sy-msgv1
                            pw_v2   "TYPE sy-msgv2
                            pw_v3   "TYPE sy-msgv3
                            pw_v4.  "TYPE sy-msgv4.

  DATA ls_bapiret2 TYPE bapiret2.

  ls_bapiret2-type       = pw_type .
  ls_bapiret2-id         = pw_id   .
  ls_bapiret2-number     = pw_num  .
  ls_bapiret2-message_v1 = pw_v1   .
  ls_bapiret2-message_v2 = pw_v2   .
  ls_bapiret2-message_v3 = pw_v3   .
  ls_bapiret2-message_v4 = pw_v4   .

  IF ls_bapiret2-type IS INITIAL.
    ls_bapiret2-type = 'S'.
  ENDIF.

  IF ls_bapiret2-id IS INITIAL.
    ls_bapiret2-id     = '0H'.
  ENDIF.

  IF ls_bapiret2-number IS INITIAL.
    ls_bapiret2-number = '000'.
  ENDIF.


  MESSAGE    ID ls_bapiret2-id
           TYPE ls_bapiret2-type
         NUMBER ls_bapiret2-number
           WITH ls_bapiret2-message_v1
                ls_bapiret2-message_v2
                ls_bapiret2-message_v3
                ls_bapiret2-message_v4
           INTO ls_bapiret2-message.

  APPEND ls_bapiret2 TO gi_user_message.

ENDFORM.