Monday, May 6, 2019

Create a Freight Order


SAP TM is based on a set of Framework which helps to realize different aspect of the Application.
These Business Objects are modeled and implemented using Business Object Processing Framework ( BOPF ). 
Business objects are the basis of the TM Application. Each of this Business Object represents a type of a uniquely identifiable business entity. One of the example of TM Business Object are the Freight Order.

Freight Orders - These are created as result of planning , especially in Land Transportation. In a subcontracting process the Freight Order is the Transportation document which is sent to carrier once he is assigned via carrier selection.


Below is the code to create a blank Freight Order using BOPF Approach.




*&---------------------------------------------------------------------*
*& Report ZTM_FO_CREATE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*


REPORT ztm_fo_create.

DATA:   lo_srv_mgr      TYPE  REF TO  /bobf/if_tra_service_manager,
            lo_location       TYPE  REF TO  /bobf/if_tra_service_manager,
            lo_message      TYPE  REF TO /bobf/if_frw_message,
            lt_return           TYPE  STANDARD TABLE OF bapiret2,
            lt_fo_root         TYPE  STANDARD TABLE OF /scmtms/s_tor_root_k.
            lt_mod             TYPE   /bobf/t_frw_modification,
            lt_location_id    TYPE   /scmtms/t_loc_alt_id,
            ls_location_id    TYPE   /scmtms/location_id,
            lt_key_index     TYPE   /bobf/t_frw_keyindex,
            lt_location_key  TYPE  /bobf/t_frw_key,
            ls_fo_info          TYPE   /scmtms/s_tor_fo_info.



"//Get an instance of a service manager for Transportation Order -
lo_srv_mgr = /bobf/cl_tra_serv_mgr_factory=>get_service_manager( /scmtms/if_tor_c=>sc_bo_key ).


"//Create Object for Location


lo_location = /bobf/cl_tra_serv_mgr_factory=>get_service_manager( /scmtms/if_location_c=>sc_bo_key ).



"//Get Source Location , For E.g Source Plant


ls_location_id = '1000000001_01'.


INSERT ls_location_id INTO TABLE lt_location_id.



"//Get Destination Location , For E.g Destination Plant


ls_location_id = 'DC05_01'.


INSERT ls_location_id INTO TABLE lt_location_id.



"//Convert Location IDs to Technical Keys Using Alternate Key call to of Location Instance -


lo_location->convert_altern_key(

      EXPORTING
        iv_node_key          = /scmtms/if_location_c=>sc_node-root            "//Location Root Node
        iv_altkey_key        = /scmtms/if_location_c=>sc_alternative_key-root-location_id  "// Alternate Key - Location ID
        it_key               = lt_location_id                                                     "// Actual Locations Table
      IMPORTING
        et_result            = lt_key_index                                                    "// Key table with explicit index
        et_key               = lt_location_key ).                                             "// Location Technical keys 

"//Populate Locations Keys into Freight Order Info Structure


"//Source Location key at Index = 1

READ TABLE lt_key_index INTO DATA(ls_key_index1) WITH TABLE KEY index = 1.
IF sy-subrc = 0.
  ls_fo_info-loc_src_key =  ls_key_index1-key.
ENDIF.

"//Destination Location key at Index = 2

READ TABLE lt_key_index INTO DATA(ls_key_index2) WITH TABLE KEY index = 2.
IF sy-subrc = 0.
  ls_fo_info-loc_dst_key =  ls_key_index2-key.
ENDIF.


"//Call Method of Factory Class for Creating TO Objects -


/scmtms/cl_tor_factory=>create_tor_tour(

    EXPORTING
      iv_tor_type                   = '2000'                    "//Freight Order Type - Rail Freight Order
      iv_create_initial_stage = abap_true               "// Crate Transportation Order Stops with Initial Stage
      is_fo_info                     = ls_fo_info              "// Freight Order Attributes
      iv_creation_type          = /scmtms/if_tor_const=>sc_creation_type-manual "// Creation Type - Manual
    IMPORTING
      es_tor_root             = DATA(ls_fo_root)       "// Freight order - Root Structure
      et_tor_item             = DATA(lt_fo_item)       "// Freight Order - Item Table
      et_tor_stop             = DATA(lt_fo_stop)        "// Freight Order - Stops Table
    CHANGING
      co_message              = lo_message ).

"// Convert lo_message instance to Retrun table -

IF lo_message IS BOUND .
  /scmtms/cl_common_helper=>msg_convert_bopf_2_bapiret2(
  EXPORTING
  io_message  = lo_message
  CHANGING
  ct_bapiret2 = lt_return ).
ENDIF.

"//Root Modify

ls_fo_root-created_by  = sy-uname.
INSERT ls_fo_root INTO TABLE lt_fo_root.

"// Call Modify method Of Transaction Manager  to get MOD table -

CALL METHOD /scmtms/cl_mod_helper=>mod_create_multi
  EXPORTING
    it_data   = lt_fo_root
    iv_node = /scmtms/if_tor_c=>sc_node-root
  CHANGING
    ct_mod  = lt_mod.


"// Call Modify method Of Transaction Manager -

/bobf/cl_tra_serv_mgr_factory=>get_service_manager( /scmtms/if_tor_c=>sc_bo_key )->modify(
  EXPORTING
    it_modification = lt_mod
  IMPORTING
    eo_message = lo_message ).


"// Convert lo_message instance to Retrun table -

IF lo_message IS BOUND .
  /scmtms/cl_common_helper=>msg_convert_bopf_2_bapiret2(
  EXPORTING
  io_message  = lo_message
  CHANGING
  ct_bapiret2 = lt_return ).
ENDIF.


"// Call Save method Of Transaction Manager -

/bobf/cl_tra_trans_mgr_factory=>get_transaction_manager( )->save(
  IMPORTING
    eo_message = lo_message ) .

"// Convert lo_message instance to Retrun table -

IF lo_message IS BOUND .
  /scmtms/cl_common_helper=>msg_convert_bopf_2_bapiret2(
  EXPORTING
  io_message  = lo_message
  CHANGING
  ct_bapiret2  = lt_return ).
ENDIF.

1 comment:

  1. Hello, Tried the same but i dont get a FO number created . I dont get any error as well

    ReplyDelete