BAPI_ALM_ORDER_MAINTAIN

I recently had to create some PM order (IW31) based on Equipment Task list (IA01). Luckily, there is a BAPI available which can be used for such a task. Moreover, BAPI_ALM_ORDER_MAINTAIN is your Swiss Army Knife when it comes to ALM. It can be used for mostly everything related to ALM ,beside of making some coffee. SAP also provides some documentation for this BAPI. Sadly, it is a common SAP documentation which means, it describes some points but lacks in others.

After several attempts, I was able to create a PM Order related to a Notification which uses an Equipment task list instead of defining each operation manually.

Below code is an example of how it can be achieved

*&---------------------------------------------------------------------*
*& Report  Z_TEST_PM_ORDER
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT z_test_pm_order.


DATA: lv_notif_header        TYPE bapi2080_nothdri,
      lv_notif_type          TYPE bapi2080-notif_type,
      lv_notif_header_export TYPE bapi2080_nothdre,
      lv_objectkey_notif     TYPE bapi_alm_order_method-objectkey,
      lv_objectkey           TYPE bapi_alm_order_method-objectkey,
      lv_equnr               TYPE equnr,
      lv_plant               TYPE werks_d,
      lv_order_type          TYPE aufart,
      lv_task_list           TYPE bapi_alm_order_tasklists_i-task_list_group,
      lv_task_list_counter   TYPE bapi_alm_order_tasklists_i-group_counter,
      lt_return              TYPE STANDARD TABLE OF bapiret2,
      lt_numbers             TYPE STANDARD TABLE OF bapi_alm_numbers.


" set general data:
lv_equnr = '<enter Equipment No here>'.
lv_plant = '<Plant of task list>'.
lv_task_list = '<PLNNR of task list>'.
lv_task_list_counter = '<PLNAL of task list>'.
lv_order_type = '<order type like PM03>'.


" First of all, we create a PM Notification
lv_notif_header-desstdate = sy-datum.
lv_notif_header-short_text = 'Add Notification to Order'.
lv_notif_header-reportedby = sy-uname.
lv_notif_type = 'M2'.

CALL FUNCTION 'BAPI_ALM_NOTIF_CREATE'
  EXPORTING
    notif_type         = lv_notif_type
    notifheader        = lv_notif_header
  IMPORTING
    notifheader_export = lv_notif_header_export.

CALL FUNCTION 'BAPI_ALM_NOTIF_SAVE'
  EXPORTING
    number      = lv_notif_header_export-notif_no
  IMPORTING
    notifheader = lv_notif_header_export
  TABLES
    return      = lt_return.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.

*------ Create Oder related to notification -------*

lv_objectkey_notif =  '%00000000001' && lv_notif_header_export-notif_no.
lv_objectkey =  '%00000000001'.

DATA(lt_methods) = VALUE bapi_alm_order_method_t(
               ( refnumber = '000001' objecttype = 'HEADER' 
                 method = 'CREATETONOTIF' objectkey = lv_objectkey_notif )
               ( refnumber = '000001' objecttype = 'TASKLIST' 
                 method = 'ADD' objectkey = lv_objectkey )
               ( refnumber = '000001' objecttype = 'HEADER' 
                 method = 'RELEASE' objectkey = lv_objectkey )
               ( refnumber = '000001' objecttype = '' 
                 method = 'SAVE' objectkey = lv_objectkey )
              ).

*--- add equipment task list (IA03)
DATA(lt_tasklist) = VALUE bapi_alm_order_tasklists_i_t(
         ( task_list_type = 'E' task_list_group = lv_task_list 
           group_counter = lv_task_list_counter use_workcenter_from_head = 'X' )
       ).


*--- build header data
DATA(lt_header) = VALUE bapi_alm_order_headers_i_t(
                       ( orderid = lv_objectkey notif_type = lv_notif_type 
                         order_type = lv_order_type 
                         planplant = lv_plant
 equipment = lv_equnr 
                         start_date = sy-datum )
                     ).


DATA(lt_header_up) = VALUE bapi_alm_order_headers_i_ut(
                      ( orderid = lv_objectkey notif_no = 'X' )
                    ).


CALL FUNCTION 'BAPI_ALM_ORDER_MAINTAIN'
  TABLES
    it_methods   = lt_methods
    it_header    = lt_header
    it_header_up = lt_header_up
    it_tasklists = lt_tasklist
    return       = lt_return
    et_numbers   = lt_numbers.

IF line_exists( lt_return[ type = 'E' ] ).
  RETURN.
ENDIF.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.