Category Archive Blog

Generate a random number in ABAP

Use class CL_ABAP_RANDOM_INT to generate a random integer number :


DATA(lv_random_num) = cl_abap_random_int=>create( seed = cl_abap_random=>seed( )
                                                  min  = 1
                                                  max  = 100 )->get_next( ).
* lv_random_num will be type i

Read More

DELKZ : Code list of MRP elements

Most used :
ValueDesignation ENDesignation FR
AR Dependent reservationRéservation dépendante
FE Production orderOrdre de fabrication
PP Planned independent requirementBesoins indépendants Prévisionnel (BIP)
QM Inspection lot for quality management Lot de contrôle QM
SA Simulation orderOrdre de simulation
SM Sim. dependent reqmtsBesoin dépendant de simulation
WB Plant stockStock division
All values :
Read More

Create ALV with class CL_SALV_TABLE

Example of implementation in SE38 : SALV_DEMO_TABLE_SELECTIONS.

Simply display ALV table


  DATA : go_alv_table TYPE REF TO CL_SALV_TABLE.

  TRY.
      cl_salv_table=>factory(
        IMPORTING
          r_salv_table = go_alv_table
        CHANGING
          t_table      = gt_table_to_display ).
    CATCH cx_salv_msg.
  ENDTRY.

  " Optimize columns width
  DATA(lo_columns) = go_alv_table->get_columns( ).
  lo_columns->set_optimize( ).

  " Apply zebra style to lv_rows
  DATA(lo_display) = go_alv_table->get_display_settings( ).
  lo_display->set_striped_pattern( cl_salv_display_settings=>true ).

  " Display ALV
  go_alv_table->display( ).

Read More

Message : synthax templates

Trigger message from standard data :


    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

Set BAPIRET2 structure from standard data :


  ls_return-type = sy-msgty.
  ls_return-id   = sy-msgid.
  ls_return-number = sy-msgno.
  ls_return-message_v1 = sy-msgv1.
  ls_return-message_v2 = sy-msgv2.
  ls_return-message_v3 = sy-msgv3.
  ls_return-message_v4 = sy-msgv4.
  MESSAGE ID sy-msgid
          TYPE sy-msgty
          NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
          INTO ls_return-message.

Trigger message from BAPIRET2 structure :


  MESSAGE ID     ls_return-id
          TYPE   ls_return-type
          NUMBER ls_return-number
          WITH   ls_return-message_v1
                 ls_return-message_v2
                 ls_return-message_v3
                 ls_return-message_v4.

Manipulate dates in ABAP

Check dedicated post to Add days/month/year to date.

Manipulate dates in worked days (or factory days)

Add working days to a date

You can use Function BKK_ADD_WORKINGDAY.

Difference between 2 dates in worked days

You can use Function RKE_SELECT_FACTDAYS_FOR_PERIOD.

Read More

Template ABAP for local classes

Local class template :
See also Template for ABAP local ALV event class


CLASS lcl_sample_class DEFINITION.
*------------------------------
* Public section
*------------------------------
  PUBLIC SECTION.

    TYPES : BEGIN OF ts_public_type,
              flag  TYPE abap_bool,
              value TYPE char20,
            END OF ts_public_type.

    DATA : gt_public_table TYPE STANDARD TABLE OF ts_public_type WITH DEFAULT KEY. "Global table inside the class (table must be fully typed to be used in return types)

    METHODS constructor IMPORTING iv_input TYPE char20.


    METHODS public_method IMPORTING iv_input         TYPE char20
                          RETURNING VALUE(rs_output) TYPE ts_public_type.

    CLASS-METHODS public_static_method IMPORTING iv_input         TYPE char20
                                       RETURNING VALUE(rv_output) TYPE char20.


*------------------------------
* Private section
*------------------------------
  PRIVATE SECTION.

    TYPES : BEGIN OF ts_private_type,
              flag  TYPE abap_bool,
              value TYPE char20,
            END OF ts_private_type,
            ti_private_type TYPE STANDARD TABLE OF ts_private_type WITH DEFAULT KEY. " (tables must be fully typed to be used in return types)

    DATA : gt_private_table TYPE ti_private_type. "Global table inside the class

    CLASS-DATA : gs_static_private_structure TYPE mara,
                 gv_static_private_var       TYPE matnr.

    METHODS _private_method IMPORTING iv_input         TYPE char20
                            RETURNING VALUE(rs_output) TYPE ts_public_type.

    CLASS-METHODS _private_static_method IMPORTING iv_input         TYPE char20
                                         RETURNING VALUE(rv_output) TYPE char20.

ENDCLASS. " End of lcl_sample_class DEFNITION



CLASS lcl_sample_class IMPLEMENTATION.

  METHOD constructor.
    " Init process for instanciated class
  ENDMETHOD.

  METHOD public_method.
    " Public (instanciated) method
    " Code...
  ENDMETHOD.

  METHOD public_static_method.
    " Code...
  ENDMETHOD.

  METHOD _private_method.
    " Private (instanciated) method
    " Code...
  ENDMETHOD.

  METHOD _private_static_method.
    " Code...
  ENDMETHOD.

ENDCLASS. " End of lcl_sample_class IMPLEMENTATION

Create a transaction from SM30

To create a transaction to directly edit a table managed in SM30, go to SE93, create a Transaction with parameters, and follow steps below :

Read More

GMCODE values for BAPI_GOODSMVT_CREATE

Values for GMCODE field can ben found into table T158G.

┌────────┬───────┬──────────────────────────────────────────────────────────┐
│ GMCODE │ TCODE │                       Description                        │
├────────┼───────┼──────────────────────────────────────────────────────────┤
│      1 │ MB01  │ GM_Code 01: Goods receipt for purchase order             │
│      2 │ MB31  │ GM_Code 02: Goods receipt for production order           │
│      3 │ MB1A  │ GM_Code 03: Goods issue                                  │
│      4 │ MB1B  │ GM_Code 04: Transfer posting                             │
│      5 │ MB1C  │ GM_Code 05: Other goods receipts                         │
│      6 │ MB11  │ GM_Code 06: Reversal of goods movements                  │
│      7 │ MB04  │ GM_Code 07: Subsequent adjustment to a subcontract order │
└────────┴───────┴──────────────────────────────────────────────────────────┘

Sapscript – Local class to easily manage data in performs

Local class :


TYPES : ti_itcsy TYPE STANDARD TABLE OF itcsy WITH DEFAULT KEY.

CLASS lcl_sapscript DEFINITION.
*------------------------------
* Public section
*------------------------------
  PUBLIC SECTION.
    CLASS-METHODS init_tables IMPORTING input_table  TYPE ti_itcsy
                                        output_table TYPE ti_itcsy.
    CLASS-METHODS get_input_value IMPORTING iv_field        TYPE tdtprgname
                                  RETURNING VALUE(rv_value) TYPE tdsymvalue.
    CLASS-METHODS get_output_table RETURNING VALUE(rt_output) TYPE ti_itcsy.
    CLASS-METHODS set_output_value IMPORTING iv_field         TYPE tdtprgname
                                             iv_value         TYPE tdsymvalue
                                   RETURNING VALUE(rt_output) TYPE ti_itcsy.
  PRIVATE SECTION.
    CLASS-DATA : it_static_tbl_input  TYPE ti_itcsy,
                 it_static_tbl_output TYPE ti_itcsy.
ENDCLASS.

CLASS lcl_sapscript IMPLEMENTATION.
  METHOD init_tables.
    CLEAR : it_static_tbl_input, it_static_tbl_output.
    it_static_tbl_input = input_table.
    it_static_tbl_output = output_table.
  ENDMETHOD.
  METHOD get_input_value.
    READ TABLE it_static_tbl_input INTO DATA(ls_table) WITH KEY name = iv_field.
    IF sy-subrc = 0.
      rv_value = ls_table-value.
    ENDIF.
  ENDMETHOD.
  METHOD set_output_value.
    READ TABLE it_static_tbl_output ASSIGNING FIELD-SYMBOL(<fs_output>) WITH KEY name = iv_field.
    IF sy-subrc = 0.
      <fs_output>-value = iv_value.
    ENDIF.
    rt_output = it_static_tbl_output.
  ENDMETHOD.
  METHOD get_output_table.
    rt_output = it_static_tbl_output.
  ENDMETHOD.
ENDCLASS.

Example of use :

Read More

Get user default printer

Use FM GET_PRINT_PARAM.
Result will be in field E_USR01-SPLD.

Read More