It is possible to maintain interchangeability groups in APO manually with transaction /INCMD/UI but this is not practical in case we would like to automate some actions like assignment of INCMD groups to models.
For this purpose I created a program that can be used:
- It reads all INCMD groups currently assigned to given range of source models and having status from given status range
- It assigns all selected INCMD groups to target models given also by range.
- In case the INCMD groups is already assigned to target model, this assignment is skipped
- Finally it notifies user about how many new assignments were created.
DATA:
l_model TYPE /incmd/model, "used for selection screen only
l_status TYPE /incmd/istat. "used for selection screen only
SELECTION-SCREEN BEGIN OF BLOCK b01 WITH FRAME TITLE text-001.
SELECT-OPTIONS: src_mod FOR l_model, " Source range of models
src_stat FOR l_status. " Source range of INCMD groups statuses
PARAMETERS: p_ungr AS CHECKBOX DEFAULT ' '. " Process also all unassigned groups
SELECTION-SCREEN END OF BLOCK b01.
SELECTION-SCREEN BEGIN OF BLOCK b02 WITH FRAME TITLE text-002.
SELECT-OPTIONS: dst_mod FOR l_model. " Destination range of models
SELECTION-SCREEN END OF BLOCK b02.
DATA:
lt_header TYPE TABLE OF /incmd/bapigrphdr02,
lt_return TYPE TABLE OF bapiret2,
lt_scr_group_models TYPE TABLE OF /incmd/bapigrpmodel02,
lt_add_models TYPE TABLE OF /sapapo/model,
lt_existing_assignments TYPE /sapapo/modelinc_tab,
lt_newassignments TYPE /sapapo/modelinc_tab,
lt_groups TYPE /sapapo/inc_icno_sel_tab,
l_lines TYPE i.
FIELD-SYMBOLS:
<fs_status> LIKE src_stat,
<fs_group> TYPE /sapapo/inc_icno_sel_str,
<fs_scr_group_model> type /incmd/bapigrpmodel02,
<fs_return> TYPE bapiret2,
<fs_newassignment> TYPE /sapapo/modelinc,
<fs_add_model> TYPE /sapapo/model,
<fs_header> TYPE /incmd/bapigrphdr02.
START-OF-SELECTION.
* Select all statuses in case no manual selection was made
IF src_stat IS INITIAL.
APPEND INITIAL LINE TO src_stat ASSIGNING <fs_status>.
<fs_status>-sign = 'I'.
<fs_status>-option = 'BT'.
<fs_status>-low = ' '.
<fs_status>-high = '4'.
ENDIF.
* Read all the INCMD groups together with models they are assigned to
CALL FUNCTION '/INCMD/BAPI_GROUP_GETLIST'
TABLES
group_status_selection = src_stat
model_selection = src_mod
group_header_data = lt_header
group_models_data = lt_scr_group_models
return = lt_return.
* Check for errors
READ TABLE lt_return ASSIGNING <fs_return> WITH KEY type = 'E' .
IF sy-subrc = 0.
MESSAGE ID <fs_return>-id
TYPE 'E'
NUMBER <fs_return>-number
WITH <fs_return>-message_v1
<fs_return>-message_v2
<fs_return>-message_v3
<fs_return>-message_v4.
ELSE.
* Select target models the selected INCMD groups can be assigned to
SELECT model FROM /sapapo/model
INTO CORRESPONDING FIELDS OF TABLE lt_add_models
WHERE model IN dst_mod.
IF lt_add_models IS NOT INITIAL.
* Get list of all INCMD groups to be assigned to destination range of models
IF p_ungr = 'X'.
* Process all available groups that matches selection conditions
* even if they are not currently assigned to any model
LOOP AT lt_header ASSIGNING <fs_header>.
APPEND INITIAL LINE TO lt_groups ASSIGNING <fs_group>.
<fs_group>-sign = 'I'.
<fs_group>-option = 'EQ'.
<fs_group>-low = <fs_header>-group_number.
ENDLOOP.
ELSE.
"Process only groups assigned to some group from source selection model range
LOOP AT lt_scr_group_models ASSIGNING <fs_scr_group_model>.
APPEND INITIAL LINE TO lt_groups ASSIGNING <fs_group>.
<fs_group>-sign = 'I'.
<fs_group>-option = 'EQ'.
<fs_group>-low = <fs_scr_group_model>-group_number.
ENDLOOP.
sort lt_groups.
delete ADJACENT DUPLICATES FROM lt_groups.
ENDIF.
* Get all currently existing assignments for selected INCMD groups
CALL FUNCTION '/SAPAPO/INC_MODELS_GET'
EXPORTING
it_icno_sel = lt_groups
IMPORTING
et_modelinc = lt_existing_assignments.
* Prepare i-table with new assignments
LOOP AT lt_groups ASSIGNING <fs_group>.
LOOP AT lt_add_models ASSIGNING <fs_add_model>.
* Check if such assignment already exist in database
READ TABLE lt_existing_assignments
WITH KEY icno = <fs_group>-low
model = <fs_add_model>-model
TRANSPORTING NO FIELDS.
* Skip if such assignment already exists
CHECK sy-subrc <> 0.
* Create new assignment between INCMD group and model
APPEND INITIAL LINE TO lt_newassignments ASSIGNING <fs_newassignment>.
<fs_newassignment>-mandt = sy-mandt.
<fs_newassignment>-icno = <fs_group>-low.
<fs_newassignment>-model = <fs_add_model>-model.
ENDLOOP.
ENDLOOP.
* Write new assignments if any
IF lt_newassignments IS NOT INITIAL.
CALL FUNCTION '/SAPAPO/INC_MODELS_SET'
EXPORTING
it_modelinc = lt_newassignments.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ENDIF.
ENDIF.
* Write info message
DESCRIBE TABLE lt_newassignments LINES l_lines.
IF l_lines > 0.
MESSAGE i114(zapo01) WITH 'Number of new INCMD Group-Model assignments: '
l_lines.
ELSE.
MESSAGE i114(zapo01) WITH 'No new INCMD Group-Model assignment'.
ENDIF.
ENDIF.