Skip to Content
avatar image
Former Member

How to Simulate a Freight Order in SAP TM 9.5 from a Z program and get Transport charge values?

Hello all,

I would like to thanks for any reply in advance, it will be really helpful.

There is this requirement to simulate a quick quotation and show the calculated charges for given transport, origin, destination, carrier, purchase org, vehicle and "subitem" of this vehicle, all of this done inside of a Z code, not using the NWBC cockpit to do so.

I have already done the following code:

* TOR*
*-- Business partner declarations
DATA: lo_srv_mgr_bp TYPE REF TO /bobf/if_tra_service_manager.
DATA: lo_srv_mgr_sp TYPE REF TO /bobf/if_tra_service_manager.
DATA: lo_srv_mgr_sf TYPE REF TO /bobf/if_tra_service_manager.
DATA: lt_param TYPE /bobf/t_frw_query_selparam.
DATA: ls_param TYPE /bobf/s_frw_query_selparam.
DATA: ls_query_info TYPE /bobf/s_frw_query_info.
DATA: lt_key_bp TYPE /bobf/t_frw_key.
DATA: ls_key_bp TYPE /bobf/s_frw_key.
DATA: lo_message_bp TYPE REF TO /bobf/if_frw_message.
DATA: lt_key_sp TYPE /bobf/t_frw_key.
DATA: ls_key_sp TYPE /bobf/s_frw_key.
DATA: lo_message_sp TYPE REF TO /bobf/if_frw_message.
DATA: lt_key_sf TYPE /bobf/t_frw_key.
DATA: ls_key_sf TYPE /bobf/s_frw_key.
DATA: lo_message_sf TYPE REF TO /bobf/if_frw_message.

*-- TOR declarations
DATA: lo_srv_mgr TYPE REF TO /bobf/if_tra_service_manager.
DATA: ls_root TYPE /scmtms/s_tor_root_k.
DATA: lt_tor_root TYPE /scmtms/t_tor_root_k.
DATA: lt_item TYPE /scmtms/t_tor_item_tr_k.
DATA: ls_item TYPE /scmtms/s_tor_item_tr_k.
DATA: lt_stop TYPE /scmtms/t_tor_stop_k.
DATA: lt_succ TYPE /scmtms/t_tor_stop_succ_k.
DATA: ls_fo_info TYPE /scmtms/s_tor_fo_info.
DATA: lt_mod TYPE /bobf/t_frw_modification.
DATA: lo_message TYPE REF TO /bobf/if_frw_message.
DATA: lr_s_item_tr TYPE REF TO /scmtms/s_tor_item_tr_k.
DATA: lt_key_link TYPE /bobf/t_frw_key_link.
DATA: lt_target_key TYPE /bobf/t_frw_key.

FIELD-SYMBOLS: <fs_mod> LIKE LINE OF lt_mod.

DATA: lv_main_item_key TYPE /bobf/conf_key.

" Initialize service manager for Business partner
lo_srv_mgr_bp = /bobf/cl_tra_serv_mgr_factory=>get_service_manager( /bofu/if_bupa_constants=>sc_bo_key ).

*-- Initialize service manager for TOR
lo_srv_mgr = /bobf/cl_tra_serv_mgr_factory=>get_service_manager( /scmtms/if_tor_c=>sc_bo_key ).

*-- Get key for Carrier
CLEAR ls_param.
CLEAR lt_param.

ls_param-attribute_name = /bofu/if_bupa_constants=>sc_query_attribute-root-query_by_identification-idnumber.
ls_param-option = 'EQ'.
ls_param-sign = 'I'.
ls_param-low = '0000500040'.
APPEND ls_param TO lt_param.

CALL METHOD lo_srv_mgr_bp->query
EXPORTING
iv_query_key = /bofu/if_bupa_constants=>sc_query-root-query_by_identification
it_selection_parameters = lt_param
IMPORTING
eo_message = lo_message_bp
es_query_info = ls_query_info
et_key = lt_key_bp.

CLEAR ls_key_bp.
IF lo_message_bp IS BOUND AND lo_message_bp->check( ) EQ abap_true.
" Error handling
ELSE.
READ TABLE lt_key_bp INTO ls_key_bp INDEX 1.
ENDIF.

*-- Get key for Ship From
lo_srv_mgr_sf = /bobf/cl_tra_serv_mgr_factory=>get_service_manager( /bofu/if_bupa_constants=>sc_bo_key ).

CLEAR ls_param.
CLEAR lt_param.

ls_param-attribute_name = /bofu/if_bupa_constants=>sc_query_attribute-root-query_by_identification-partner.
ls_param-option = 'EQ'.
ls_param-sign = 'I'.
ls_param-low = '101A'.
APPEND ls_param TO lt_param.

CALL METHOD lo_srv_mgr_sf->query
EXPORTING
iv_query_key = /bofu/if_bupa_constants=>sc_query-root-query_by_identification
it_selection_parameters = lt_param
IMPORTING
eo_message = lo_message_sf
es_query_info = ls_query_info
et_key = lt_key_sf.

CLEAR ls_key_sp.
IF lo_message_sf IS BOUND AND lo_message_sf->check( ) EQ abap_true.
" Error handling
ELSE.
READ TABLE lt_key_sf INTO ls_key_sf INDEX 1.
ENDIF.

*-- Get key for Ship-to
lo_srv_mgr_sp = /bobf/cl_tra_serv_mgr_factory=>get_service_manager( /bofu/if_bupa_constants=>sc_bo_key ).

CLEAR ls_param.
CLEAR lt_param.

ls_param-attribute_name = /bofu/if_bupa_constants=>sc_query_attribute-root-query_by_identification-idnumber.
ls_param-option = 'EQ'.
ls_param-sign = 'I'.
ls_param-low = '0000150101'.
APPEND ls_param TO lt_param.

CALL METHOD lo_srv_mgr_sp->query
EXPORTING
iv_query_key = /bofu/if_bupa_constants=>sc_query-root-query_by_identification
it_selection_parameters = lt_param
IMPORTING
eo_message = lo_message_sp
es_query_info = ls_query_info
et_key = lt_key_sp.

CLEAR ls_key_sp.
IF lo_message_sp IS BOUND AND lo_message_sp->check( ) EQ abap_true.
" Error handling
ELSE.
READ TABLE lt_key_sp INTO ls_key_sp INDEX 1.
ENDIF.

ls_fo_info-loc_src_key = ls_key_sp-key.
ls_fo_info-loc_dst_key = ls_key_sf-key.

CALL METHOD /scmtms/cl_tor_factory=>create_tor_tour
EXPORTING
iv_tor_type = '0001'
iv_create_initial_stage = abap_true
iv_creation_type = /scmtms/if_tor_const=>sc_creation_type-initial_fu
is_fo_info = ls_fo_info
IMPORTING
es_tor_root = ls_root
et_tor_item = lt_item
et_tor_stop = lt_stop
et_tor_stop_succ = lt_succ
CHANGING
co_message = lo_message.

*-- Get key of Carrier
ls_root-tor_cat = 'FU'.
ls_root-tsp = ls_key_bp-key.
ls_root-tspid = '0000500040'. " Carrier ID
* ls_root-exec_org_id = '0000500040'. " Carrier ID
ls_root-purch_org_id = '00001011'. " Purchase Organization
ls_root-mtr = 'YT_FCL'. " Means of transport

INSERT ls_root INTO TABLE lt_tor_root.
CALL METHOD /scmtms/cl_mod_helper=>mod_create_multi
EXPORTING
it_data = lt_tor_root
iv_node = /scmtms/if_tor_c=>sc_node-root
CHANGING
ct_mod = lt_mod.

READ TABLE lt_tor_root INTO ls_root INDEX 1.

*-- Add TOR Item under the root
READ TABLE lt_item INTO ls_item INDEX 1.
ls_item-main_cargo_item = abap_true.
MODIFY lt_item FROM ls_item INDEX 1.
lv_main_item_key = ls_item-key.

CLEAR ls_item.
ls_item-key = /bobf/cl_frw_factory=>get_new_key( ).
ls_item-root_key = ls_root-key.
ls_item-item_parent_key = lv_main_item_key.
ls_item-parent_key = ls_root-key.
ls_item-item_type = 'CONT'. " Item type
ls_item-gro_wei_val = '5.000'. " Gross Weight
ls_item-gro_wei_uni = 'LB'. " Gross Unit
ls_item-tures_tco = 'Y42G0'. " Equipment Type
ls_item-tures_cat = 'CN'. " Equipment Group
APPEND ls_item TO lt_item.

/scmtms/cl_mod_helper=>mod_create_multi(
EXPORTING
it_data = lt_item
iv_node = /scmtms/if_tor_c=>sc_node-item_tr
iv_source_node = /scmtms/if_tor_c=>sc_node-root
iv_association = /scmtms/if_tor_c=>sc_association-root-item_tr
CHANGING
ct_mod = lt_mod ).

/scmtms/cl_mod_helper=>mod_create_multi(
EXPORTING
it_data = lt_stop
iv_node = /scmtms/if_tor_c=>sc_node-stop
iv_source_node = /scmtms/if_tor_c=>sc_node-root
iv_association = /scmtms/if_tor_c=>sc_association-root-stop
CHANGING
ct_mod = lt_mod ).

/scmtms/cl_mod_helper=>mod_create_multi(
EXPORTING
it_data = lt_succ
iv_node = /scmtms/if_tor_c=>sc_node-stop_successor
iv_source_node = /scmtms/if_tor_c=>sc_node-stop
iv_association = /scmtms/if_tor_c=>sc_association-stop-stop_successor
iv_do_sorting = abap_true
CHANGING
ct_mod = lt_mod ).

DATA: ls_key TYPE /bobf/s_frw_key.
DATA: lt_key TYPE /bobf/t_frw_key.
DATA: lt_failed_key TYPE /bobf/t_frw_key.
DATA: lt_failed_act_key TYPE /bobf/t_frw_key.
DATA: lo_change TYPE REF TO /bobf/if_tra_change.
DATA: lt_tcc_root TYPE /scmtms/t_tcc_root_k.
DATA: lo_parameters TYPE REF TO /scmtms/s_tcc_calc_base_param.

*-- Add all modified data
CALL METHOD lo_srv_mgr->modify(
EXPORTING
it_modification = lt_mod
IMPORTING
eo_change = lo_change
eo_message = lo_message ).

*-- Call action to calculate freight
ls_key-key = ls_root-key.
APPEND ls_key TO lt_key.

CREATE DATA lo_parameters.
lo_parameters->do_modifications_only = abap_true.
* lo_parameters->calc_context = 'CQ'.

CALL METHOD lo_srv_mgr->do_action
EXPORTING
iv_act_key = /scmtms/if_tor_c=>sc_action-root-calc_transportation_charges
it_key = lt_key
is_parameters = lo_parameters
IMPORTING
eo_change = lo_change
eo_message = lo_message
et_failed_key = lt_failed_key
et_failed_action_key = lt_failed_act_key
* et_data =
.
IF lo_message IS BOUND AND lo_message->check( ) EQ abap_true.
" Error
ENDIF.

CALL METHOD lo_srv_mgr->retrieve
EXPORTING
iv_node_key = /scmtms/if_tor_c=>sc_node-transportcharges
it_key = lt_key
* iv_before_image = ABAP_FALSE
* iv_edit_mode = /BOBF/IF_CONF_C=>SC_EDIT_READ_ONLY
iv_fill_data = abap_true
* iv_invalidate_cache = ABAP_FALSE
iv_invalidate_cache = abap_true
* it_requested_attributes =
IMPORTING
eo_message = lo_message
eo_change = lo_change
et_data = lt_tcc_root
et_failed_key = lt_failed_key.

* IF ( lo_message IS BOUND AND lo_message->check( ) EQ abap_true ) OR
* lt_failed_key IS NOT INITIAL.
" Error handling
CALL METHOD lo_srv_mgr->retrieve_by_association
EXPORTING
iv_node_key = /scmtms/if_tor_c=>sc_node-root
* iv_node_key = /scmtms/if_tor_c=>sc_node-root
it_key = lt_key
* iv_association = /scmtms/if_tor_c=>sc_association-item_tr-to_root
iv_association = /scmtms/if_tor_c=>sc_association-root-transportcharges
* is_parameters =
* it_filtered_attributes =
* iv_fill_data = abap_true
* iv_before_image = ABAP_FALSE
* iv_invalidate_cache = abap_true
* iv_edit_mode = /bobf/if_conf_c=>sc_edit_read_only
* it_requested_attributes =
IMPORTING
eo_message = lo_message
eo_change = lo_change
* et_data = lt_item_tr
* et_data = lt_tcc_root
et_key_link = lt_key_link
et_target_key = lt_target_key
et_failed_key = lt_failed_key.
* ENDIF.
* ELSE.
** READ TABLE lt_key_sp INTO ls_key_sp INDEX 1.* ENDIF.

After executing action CALC_TRANSPORT_CHARGES the LT_FAILED_KEY is initial and message is initial too, so I imagine it is all good so far.

After the calculation, I need to get those charges and work on it later.

With Retrieve or Retrieve by association I do not have any return.

I know @dragosmihai.florescu already replied with the first piece of this work above ( https://archive.sap.com/discussions/thread/3670872 ) but there is nothing about getting the info back during runtime.

Can anyone help me?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

0 Answers