Create new batch characteristics with values checked and suggested by a function module

SAPIn this example I’ll try to show you how to create new characteristics for a material batch. This will include the following steps:

  1. Creation of a function module to check characteristics value (optional)
  2. Creation of a function module to suggest characteristics value – search help (optional)
  3. Creation of new characteristics (CT04)
  4. Creation/modification of a class (CL02)
  5. Object/Class assignment to a class (CL24N or CL20N and OMS2 if necessary)
  6. Batch modification with our new characteristics (MCS2N)

1. Creation of a function module to check characteristics value

In the following code you can see very basic check which can of course be much more complex for your specific scenario.

FUNCTION Z_CHECK_050.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(CHARACT_NO) TYPE  CABN-ATINN
*"     REFERENCE(CHARACT) TYPE  CABN-ATNAM
*"     REFERENCE(VALUE) TYPE  CAWN-ATWRT
*"  EXCEPTIONS
*"      NOT_FOUND
*"----------------------------------------------------------------------

" Allow empty value
  check value is NOT INITIAL.

" Just for demonstration purposes we check for fixed values
  if value <> 1 and 
     value <> 2 and
     value <> 3.
    RAISE NOT_FOUND.
  endif.
ENDFUNCTION.

2. Creation of a function module to suggest characteristics value

In the following code I’ll create my own field catalog for selection grid to be displayed. We will need a checkbox column (called CHK) which will inform about which values (multiple allowed) are selected

FUNCTION z_check_050_f4.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(CHARACT_NO) TYPE  CABN-ATINN
*"     REFERENCE(CHARACT) TYPE  CABN-ATNAM
*"     REFERENCE(DISPLAY)
*"     REFERENCE(ADDITIONAL_VALUES) TYPE  CABN-ATSON
*"     REFERENCE(MULTIPLE_VALUES) TYPE  RCTMV-ATLIS
*"     REFERENCE(LANGUAGE) TYPE  SY-LANGU
*"     REFERENCE(DISPLAY_WITH_LANGUAGE)
*"  TABLES
*"      VALUES STRUCTURE  RCTVALUES
*"----------------------------------------------------------------------
  TYPE-POOLS : slis.

  CONSTANTS:
      c_werks(50) TYPE c VALUE '(SAPLCHRG)DFBATCH-WERKS'.

  types:
    BEGIN OF ty_data,
      chk(1) type c,
      value type c,      
    END OF ty_data.

  DATA:
    lt_values type TABLE OF ty_data,
    l_werks TYPE  dfbatch-werks,
    lt_fieldcat TYPE slis_t_fieldcat_alv.

  FIELD-SYMBOLS:
    <fs_value> type ty_data,
    <fs_werks>,
    <fs_rctvalue> TYPE rctvalues,
    <fs_fcat> type slis_fieldcat_alv.

  ASSIGN (c_werks) TO <fs_werks>.
  IF sy-subrc EQ 0.
    l_werks  = <fs_werks>.
  ENDIF.

  CHECK l_werks IS NOT INITIAL.

  APPEND INITIAL LINE TO lt_values ASSIGNING <fs_value>.
  <fs_value>-value = '1'.
  APPEND INITIAL LINE TO lt_values ASSIGNING <fs_value>.
  <fs_value>-value = '2'.
  APPEND INITIAL LINE TO lt_values ASSIGNING <fs_value>.
  <fs_value>-value = '3'.      

  APPEND INITIAL LINE TO lt_fieldcat ASSIGNING <fs_fcat>.
  <fs_fcat>-fieldname  = 'CHK'.
  <fs_fcat>-seltext_m  = ''.
  <fs_fcat>-ddictxt(1) = 'M'.

  APPEND INITIAL LINE TO lt_fieldcat ASSIGNING <fs_fcat>.
  <fs_fcat>-fieldname  = 'VALUE'.
  <fs_fcat>-seltext_m  = 'Value'.
  <fs_fcat>-outputlen  = 10.
  <fs_fcat>-ddictxt(1) = 'M'.

  loop at lt_values ASSIGNING <fs_value>.
    READ TABLE values with KEY value = <fs_value>-value TRANSPORTING NO FIELDS.
    if sy-subrc = 0.
      <fs_value>-chk = 'X'.
    ENDIF.
  ENDLOOP.

  CALL FUNCTION 'REUSE_ALV_POPUP_TO_SELECT'
    EXPORTING
      i_title       = 'Allowed values'
      i_selection   = 'X'
      i_zebra       = 'X'
      i_checkbox_fieldname = 'CHK'
      i_tabname     = 'LT_VALUES'
      it_fieldcat   = lt_fieldcat
    TABLES
      t_outtab      = lt_values
    EXCEPTIONS
      program_error = 1
      OTHERS        = 2.

  LOOP AT lt_values ASSIGNING <fs_value> where chk = 'X'.
    READ TABLE values with KEY value = <fs_value>-value TRANSPORTING NO FIELDS.
    if sy-subrc = 0.
*     selected value had already been assigned - no action needed
    else.
*     new value selected -> add it to the list
      APPEND INITIAL LINE TO values ASSIGNING <fs_rctvalue>.
      <fs_rctvalue>-value = <fs_value>-value.
      <fs_rctvalue>-status = 'I'.
    ENDIF.
  endloop.

  LOOP AT values ASSIGNING <fs_rctvalue> where status <> 'I'.
    READ TABLE lt_values with KEY
      value = <fs_rctvalue>-value
      chk      = 'X'
      TRANSPORTING NO FIELDS.
    if sy-subrc <> 0.
*     value was de-selected - we have to delete it
      <fs_rctvalue>-status = 'D'.
    endif.
  ENDLOOP.

ENDFUNCTION.

3. Creation of new characteristics

Run Transaction CT04, enter name of new batch and press CREATE icon

Enter description of the characteristics, set Status to Released, in Format section select your desired format (we’ll create multiple values, character type of length 15)

Now we need to assign our check function module to the characteristics – Go to tab Values, click on Other Value Check and select radio with label Funct. module

When prompted, enter name of our check FM – Z_CHECK_050.

Note: The search help function module Z_CHECK_050_F4 is assigned automatically

4. Creation/modification of a class

Run TCode CL02 and create new or edit existing class for class type 022 (Batch). Enter some description (for new class), go to tab Char. and assign our new characteristics ZBM_0050 to class.

5. Object/Class assignment to a class

Run TCode CL24N select Class B0001, class type 022

  • for new class without existing assignments click on button Only new assignments and select Material

  • for class with existing assignments just press enter

Note: In case you get message like “Article type XXYY  does not support classification”, use TCode OMS2 and make sure material type XXYY has “Classification” option selected in box “User departments”.

6. Batch modification with our new characteristics

Run TCode MSC2N and change a batch for given material. You can see that our new characteristics is available

Try to enter some value different from 1,2 or 3 (values defined in our check FM) and press enter

Press F4 and select some value

 

Press F4 again and check that your previously selected values are already checked.

Now you can just save your batch – we are DONE 🙂

Leave a Reply