Currently Being Moderated

This weblog talks about Material Assignment with T-code CA02 - Change Routing. It provides direct solutions for material allocation in CA02 instead of using any BDC program as we all know BDC is not suggested always. This solution is based on Engineering Workbench EWB which is available as of Release 4.6B for the task list and BOM processing.


The EWB is based on an API (Application Programming Interface) which provides good service functions for the task list and BOM processing. Many problems in the area of the automation of processing steps or mass processing which cannot be solved with SAP standard programs can be solved based on the EWB-API with the help of customer-specific programs.

I have developed one program by calling suitable APIs that can allocate material to an existing Group Number.

Like we do it using T-code CA02 -> Material Assignment

The program given below can do the Material Allocation for an existimg task list Number. Hence, we can avoid writing a BDC program for the same purpose.

Sample Code:

  REPORT  zmkp_api_change_routing.

* global fields, declarations
* ================================================
INCLUDE fc27date.
INCLUDE cp_cs_const_message.
TYPE-POOLS:   cc01,
              cpsc,
              cpcl,
              cmcl,
              czcl,
              cssc,
              cscl.

TYPES: BEGIN OF create_parameter_type,
  matnr TYPE mapl-matnr,
  werks TYPE mapl-werks,
  verwe TYPE plko-verwe,
  statu TYPE plko-statu,
  plnme TYPE plko-plnme,
  steus TYPE plpo-steus,
  steuf TYPE plfhd-steuf,
  fhmat TYPE plfhd-matnr,
  sttag TYPE plko-datuv,
  number(4) TYPE n.
TYPES: END OF create_parameter_type.

DATA g_parameter TYPE create_parameter_type.

* selection screen
* ================================================
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-t01.
PARAMETERS: p_plnty TYPE plko-plnty DEFAULT 'N',
            "task list type
            p_plnnr TYPE plko-plnnr,
            "task list group number
            p_plnal TYPE plko-plnal DEFAULT '1',
            "group counter
            p_sttag TYPE plko-datuv DEFAULT sy-datum.
"keydate
SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-t02.
PARAMETERS: p_matnr TYPE mapl-matnr,
            "material
            p_werks TYPE mapl-werks DEFAULT '7112',
            "plant
            p_verwe TYPE plko-verwe DEFAULT '1',
            "usage
            p_statu TYPE plko-statu DEFAULT '4',
            "status
            p_plnme TYPE plko-plnme DEFAULT 'ST'.

SELECTION-SCREEN END OF BLOCK b2.

g_parameter-matnr = p_matnr.
g_parameter-werks = p_werks.
g_parameter-verwe = p_verwe.
g_parameter-statu = p_statu.
g_parameter-plnme = p_plnme.
g_parameter-sttag = p_sttag.

* main program
* ================================================

* 1. load task list into internal EWB-buffer
PERFORM load_task_list
  USING
    p_plnty
    p_plnnr
    p_plnal
    p_sttag.


* 2. change the loaded task list
PERFORM change_task_list
  USING
    p_sttag.

* 3. save changed task list to database
PERFORM save_to_database.

COMMIT WORK.
WRITE: /'program ready.'.

*&---------------------------------------------------------------------*
*&      Form  LOAD_TASK_LIST
*&---------------------------------------------------------------------*
*       load task listr into internal EWB-BUFFER
*----------------------------------------------------------------------*
*      -->I_PLNTY  task list type
*      -->I_PLNNR  task list number = group number
*      -->I_PLNAL  group counter
*      -->I_STTAG  keydate
*----------------------------------------------------------------------*
FORM load_task_list
  USING
    i_plnty LIKE plko-plnty
    i_plnnr LIKE plko-plnnr
    i_plnal LIKE plko-plnal
    i_sttag LIKE plko-datuv.

  DATA: l_classes_in_workarea LIKE classes_in_workarea,
        l_tsk_plnty_selection TYPE cpsc_plnty_type,
        lt_tsk_plnty_selection TYPE cpsc_plnty_type OCCURS 0,
        l_tsk_plnnr_selection TYPE cpsc_plnnr_type,
        lt_tsk_plnnr_selection TYPE cpsc_plnnr_type OCCURS 0,
        l_tsk_plnal_selection TYPE cpsc_plnal_type,
        lt_tsk_plnal_selection TYPE cpsc_plnal_type OCCURS 0,
        l_tsk_selection TYPE cpsc_tsk_sel_type.

* objects for loading
  l_classes_in_workarea-mtk_inarea = 'X'.
  l_classes_in_workarea-tsk_inarea = 'X'.
  l_classes_in_workarea-seq_inarea = 'X'.
  l_classes_in_workarea-opr_inarea = 'X'.
  l_classes_in_workarea-suo_inarea = 'X'.
  l_classes_in_workarea-prt_inarea = 'X'.

* fill selection conditions - PLNTY
  CONCATENATE 'I' 'EQ' i_plnty INTO l_tsk_plnty_selection.
  APPEND l_tsk_plnty_selection TO lt_tsk_plnty_selection.
  l_tsk_selection-plnty = lt_tsk_plnty_selection.

  CONCATENATE 'I' 'EQ' i_plnnr INTO l_tsk_plnnr_selection.
  APPEND l_tsk_plnnr_selection TO lt_tsk_plnnr_selection.
  l_tsk_selection-plnnr = lt_tsk_plnnr_selection.

  CONCATENATE 'I' 'EQ' i_plnal INTO l_tsk_plnal_selection.
  APPEND l_tsk_plnal_selection TO lt_tsk_plnal_selection.
  l_tsk_selection-plnal = lt_tsk_plnal_selection.

* load objects into EWB
  CALL FUNCTION 'CP_CC_S_LOAD_COMPLEX_BY_TSK'
    EXPORTING
      i_class                        = 'P'
      i_classes_in_workarea          = l_classes_in_workarea
      i_cpsc_tsk_sel                 = l_tsk_selection
      i_date_from                    = i_sttag
      i_date_to                      = i_sttag
    EXCEPTIONS
      workarea_not_found             = 1
      workarea_wrong_type            = 2
      class_in_workarea_inconsistent = 3
      workarea_not_specified         = 4
      opr_not_found                  = 5
      no_selection_criteria          = 6
      invalid_selection_period       = 7
      key_date_required_for_ecm      = 8
      OTHERS                         = 9.

ENDFORM.                    " LOAD_TASK_LIST

*&---------------------------------------------------------------------*
*&      Form  create_task_list
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->I_PARAMETER  text
*----------------------------------------------------------------------*
FORM create_task_list
  USING
    i_parameter TYPE create_parameter_type.

  DATA: l_tsk LIKE tsk_class_data,
        l_opr LIKE opr_class_data,
        l_vornr(4) TYPE n,
        l_mtk LIKE mtk_class_data,
        l_max_vornr(4) TYPE n.

* create task list header
* ===============================================

  CLEAR l_tsk.
  l_tsk-plnty = 'N'.                 "task list type

  l_tsk-plnnr = '50009041'.
  l_tsk-plnal = '01'.
  l_tsk-zaehl = '1'.
  l_tsk-verwe = '1'.
  l_tsk-werks = '7112'.
  l_tsk-statu = '4'.
  l_tsk-plnme = i_parameter-plnme.

  CALL FUNCTION 'CP_CL_S_TSK_CHANGE'
   EXPORTING
    i_ecn_s                     = ' '
     i_key_date_s                = i_parameter-sttag
    i_flg_tsk_check             = 'X'
    i_mark_txt_update           = ' '
     i_tsk_class_data            = l_tsk
    i_flg_collect_all_msg       = ' '
* IMPORTING
*   E_ECM_DATA_ERROR_TYPE       =
  EXCEPTIONS
    task_not_consistent         = 1
    no_authority                = 2
    no_valid_task               = 3
    task_not_locked             = 4
    ecm_data_not_suitable       = 5
    OTHERS                      = 6.

* material task list allocation
* ===============================================
  l_mtk-plnty = l_tsk-plnty.        "task list type
  l_mtk-plnnr = l_tsk-plnnr.        "task list group
  l_mtk-plnal = l_tsk-plnal.        "group counter
  l_mtk-matnr = i_parameter-matnr.  "material
  l_mtk-werks = i_parameter-werks.  "plant

  CALL FUNCTION 'CZ_CL_S_MTK_CREATE'
    EXPORTING
      i_key_date_s              = i_parameter-sttag
      i_mtk_class_data          = l_mtk
    EXCEPTIONS
      allocation_not_consistent = 1
      no_authority              = 2
      no_authority_for_material = 3
      task_not_locked           = 4
      no_valid_material         = 5
      no_valid_sales_document   = 6
      counter_overflow          = 7
      ident_already_exists      = 8
      no_valid_task             = 9
      path_incomplete           = 10
      ecm_data_not_suitable     = 11
      OTHERS                    = 12.

  WRITE: 'task list group ', l_tsk-plnnr, 'Updated'.

ENDFORM.                    " CREATE_TASK_LIST

*&---------------------------------------------------------------------*
*&      Form  SAVE_TO_DATABASE
*&---------------------------------------------------------------------*
*       save to database
*----------------------------------------------------------------------*
FORM save_to_database.
  CALL FUNCTION 'CP_CC_S_SAVE'.
ENDFORM.                    " SAVE_TO_DATABASE

*---------------------------------------------------------------------*
*       FORM change_task_list                                         *
*---------------------------------------------------------------------*
*         changes the short text of all operations to
*         'SIMPLE_EWB_TEST'
*---------------------------------------------------------------------*
*         --> i_sttag  keydate                                        *
*---------------------------------------------------------------------*
FORM change_task_list
  USING
    i_sttag LIKE plko-datuv.

  DATA: lt_opr_class_data LIKE tsk_class_data OCCURS 0,
        l_opr_class_data LIKE tsk_class_data,
        lt_opr_ident TYPE cpcl_tsk_tab_type,
*        l_opr_ident TYPE cpcl_tsk_type,
        l_opr_ident LIKE LINE OF lt_opr_ident,
        lt_opr_lock TYPE cpcl_tsk_lock_tab_type.

  CALL FUNCTION 'CP_CL_S_TSK_PROVIDE'
* EXPORTING
*   I_DATE_FROM             = DATE-MIN_GRG
*   I_DATE_TO               = DATE-MAX_GRG
*   I_PLNTY                 =
*   I_PLNNR                 =
*   I_PLNAL                 =
*   I_FLG_PROVIDE_INV       = ' '
* IMPORTING
*   E_TSK_INV               =
    TABLES
      e_tsk_class_data        = lt_opr_class_data
   EXCEPTIONS
     wrong_key               = 1
     OTHERS                  = 2.

* lock the operations and reload operations
  LOOP AT lt_opr_class_data INTO l_opr_class_data.
    MOVE-CORRESPONDING l_opr_class_data TO l_opr_ident.
    APPEND l_opr_ident TO lt_opr_ident.
  ENDLOOP.

  CALL FUNCTION 'CP_CC_S_RELOAD_BY_TSK'
    EXPORTING
      i_flg_set_lock        = 'X'
    IMPORTING
      e_tsk_lock            = lt_opr_lock
    CHANGING
      c_tsk_ident           = lt_opr_ident
    EXCEPTIONS
      reloading_not_allowed = 1
      OTHERS                = 2.

  IF NOT lt_opr_lock[] IS INITIAL.
    WRITE: /'error: some operations are already locked'.
    EXIT.
  ENDIF.

* again: provide all operations from EWB buffer
* reason: between first providing and locking another user can can
* change the operations
  CALL FUNCTION 'CP_CL_S_TSK_PROVIDE'
    TABLES
      e_tsk_class_data = lt_opr_class_data
    EXCEPTIONS
      wrong_key        = 1
      OTHERS           = 2.


* 3. change the task list
  PERFORM create_task_list
    USING
      g_parameter.


ENDFORM.                    " CHANGE_TASK_LIST

Comments

Actions

Filter Blog

By author:
By date:
By tag: