1 案例概述
實(shí)現(xiàn)輸入product的quantity,price后計(jì)算amount,驗(yàn)證product id是否為空。完成prodcut編輯后自動(dòng)計(jì)算order total amount末融,以及計(jì)算create by,create on 等信息暇韧。
Data
- ROOT(ORDER_ID, TOTAL_AMOUNT_VAL, TOTAL_AMOUNT_CUR, include admin structure)
- ITEM(PRODUCT_ID, AMOUNT_VAL, AMOUNT_CUR, QUANTITY_VAL, QUANTITY_UNI,PRICE_VAL, PRICE_CUR)
Determination
- ROOT
DET_ADMIN_DATA (ROOT has Admin Data and related determination)
DET_ROOT_AM (call action CALC_TOTAL_AMOUNT) - ITEM
CALC_AMOUNT (after modify)
Validation
- ITEM
VAL_BO_CONSISTENCY (validate product exist or not, if not, raise a warning message)
Action
- ITEM
CALC_TOTAL_AMOUNT
2 Determination
ROOT
- DET_ADMIN_DATA
用來記錄create on勾习,create by,changed on懈玻,changed by相關(guān)信息巧婶,這里不需要我們自己實(shí)現(xiàn),技術(shù)也沒達(dá)到涂乌,套用就行粹舵。 - DET_ROOT_AM
在子節(jié)點(diǎn)ITEM編輯完成后,根據(jù)ITEM的AMOUNT計(jì)算ROOT節(jié)點(diǎn)TOTAL AMOUNT骂倘。調(diào)用Action CALC_TOTAL_AMOUNT實(shí)現(xiàn)。調(diào)用action一般采用如下方法巴席,其中历涝,iv_act_key
表示所要調(diào)用的action key,it_key
表示節(jié)點(diǎn)下每一條數(shù)據(jù)的key漾唉。
觸發(fā)條件是在ROOT的子節(jié)點(diǎn)ITEM Create和Update 荧库,After Modify,前提條件是
io_modify->do_action(
EXPORTING
iv_act_key = ZIF_STE_ORDER_C=>SC_ACTION-ROOT-CALC_TOTAL_AMOUNT " Action
it_key = it_key " Key Table
* is_parameters =
* IMPORTING
* eo_message = " Message Object
* et_failed_key = " Key Table
* eo_change = " Interface of Change Object
* et_data =
).
ITEM
- CALC_AMOUNT
觸發(fā)條件是After Modify赵刑。
在輸入ITEM相關(guān)數(shù)據(jù)后分衫,根據(jù)輸入的PRICE_VAL, QUANTITY_VAL計(jì)算AMOUNT_VAL并將結(jié)果綁定。難點(diǎn)在于獲取輸入數(shù)據(jù)以及綁定結(jié)果般此。具體操作如下:
io_read->retrieve
是獲取輸入值蚪战,iv_node
是當(dāng)前node的key值牵现,it_key
表示當(dāng)前節(jié)點(diǎn)下每一條數(shù)據(jù)key,iv_fill_data
與et_data
綁定使用表示填充值,et_data
表示結(jié)果集,iv_before_image
為true表示讀取修改前的值
/scmtms/cl_mod_helper=>mod_update_single
是將結(jié)果綁定到字段邀桑,is_data
是要綁定的值瞎疼,iv_node
是當(dāng)前node的key值,iv_key
是每一行數(shù)據(jù)的key值壁畸,it_changed_fields
是要被綁定值的key值贼急。
對(duì)于綁定值有兩種方法,注釋掉的部分為方法二捏萍。
DATA: lt_item TYPE zste_t_ord_item,
ls_item TYPE zste_s_ord_item,
lt_mod TYPE /bobf/t_frw_modification,
lr_item TYPE REF TO zste_s_ord_item.
FIELD-SYMBOLS: <ls_mod> LIKE LINE OF lt_mod.
WRITE: ls_item-product_id.
io_read->retrieve(
EXPORTING
iv_node = zif_ste_order_c=>sc_node-item " Node Name
it_key = it_key " Key Table
* iv_before_image = ABAP_FALSE " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ")
* iv_fill_data = ABAP_TRUE " Data element for domain BOOLE: TRUE (='X') and FALSE (=' ')
* it_requested_attributes = " List of Names (e.g. Fieldnames)
IMPORTING
* eo_message = " Message Object
et_data = lt_item " Data Return Structure
* et_failed_key = " Key Table
* et_node_cat = " Node Category Assignment
).
LOOP AT lt_item INTO ls_item.
ls_item-amount_val = ls_item-price_val * ls_item-quantity_val.
ls_item-amount_cur = ls_item-price_cur.
/scmtms/cl_mod_helper=>mod_update_single(
EXPORTING
is_data = ls_item
iv_node = zif_ste_order_c=>sc_node-item " Node
iv_key = ls_item-key " NodeID
it_changed_fields = VALUE #( ( zif_ste_order_c=>sc_node_attribute-item-amount_val )
( zif_ste_order_c=>sc_node_attribute-item-amount_cur ) ) " List of Names (e.g. Fieldnames)
* iv_autofill_fields = 'X' " 'X' -> all not readonly fields will be determined
* iv_bo_key = " Business Object
* importing
* es_mod = " Change
CHANGING
ct_mod = lt_mod " Changes
).
ENDLOOP.
* LOOP AT lt_item INTO ls_item.
* ls_item-amount_val = ls_item-price_val * ls_item-quantity_val.
* ls_item-amount_cur = ls_item-price_cur.
*
* APPEND INITIAL LINE TO lt_mod ASSIGNING <ls_mod>.
* CREATE DATA lr_item.
* lr_item->* = ls_item.
* <ls_mod>-change_mode = /bobf/if_frw_c=>sc_modify_update.
* <ls_mod>-association = zif_ste_order_c=>sc_association-root-item.
* <ls_mod>-source_node = zif_ste_order_c=>sc_node-root.
* <ls_mod>-node = zif_ste_order_c=>sc_node-item.
* <ls_mod>-key = lr_item->key.
* <ls_mod>-data = lr_item.
* ENDLOOP.
IF lt_mod IS NOT INITIAL.
io_modify->do_modify( it_modification = lt_mod ).
ENDIF.
3 Validation
ITEM
-
VAL_BO_CONSISTENCY
觸發(fā)條件
驗(yàn)證ITEM輸入是否存在PRODUCT ID太抓。主要難點(diǎn)在于錯(cuò)誤信息顯示,實(shí)現(xiàn)如下
首先要在T-Code:se91
種創(chuàng)建message令杈,/scmtms/cl_msg_helper=>add_message
用于顯示提示信息
DATA: ls_item TYPE zste_s_ord_item,
lt_item TYPE zste_t_ord_item,
lv_msg TYPE string,
ls_msg TYPE symsg.
CLEAR eo_message.
io_read->retrieve(
EXPORTING
iv_node = zif_ste_order_c=>sc_node-item " Node Name
it_key = it_key " Key Table
* iv_before_image = ABAP_FALSE " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ")
* iv_fill_data = ABAP_TRUE " Data element for domain BOOLE: TRUE (='X') and FALSE (=' ')
* it_requested_attributes = " List of Names (e.g. Fieldnames)
IMPORTING
* eo_message = " Message Object
et_data = lt_item " Data Return Structure
* et_failed_key = " Key Table
* et_node_cat = " Node Category Assignment
).
LOOP AT lt_item INTO ls_item.
IF ls_item-product_id IS INITIAL.
MESSAGE e001(zste_order) INTO lv_msg.
MOVE-CORRESPONDING sy TO ls_msg.
/scmtms/cl_msg_helper=>add_message(
EXPORTING
* iv_bopf_location_key = " Det/Val execution node instance key
is_msg = ls_msg " Structure of Message Variables
iv_bo_key = is_ctx-bo_key " Business Object
iv_node = is_ctx-node_key " Node Name
iv_key = ls_item-key " Key
iv_attribute = zif_ste_order_c=>sc_node_attribute-item-product_id " Field Name
iv_detlevel = /scmtms/cl_applog_helper=>sc_al_detlev_default " Application Log: Level of detail
iv_probclass = /scmtms/cl_applog_helper=>sc_al_probclass_important " Application log: Message problem class
* iv_add_context_info = ABAP_FALSE " Generate BO instance specific message context
CHANGING
co_message = eo_message " Interface of Message Object
).
/scmtms/cl_common_helper=>insert_key(
EXPORTING
iv_key = ls_item-key " NodeID
* is_key = " Node Key
CHANGING
ct_key = et_failed_key " Key Table
).
ENDIF.
ENDLOOP.
4 Action
ROOT
- CALC_TOTAL_AMOUNT
難點(diǎn)主要在于ROOT節(jié)點(diǎn)獲取子節(jié)點(diǎn)ITEM的值走敌,以及將計(jì)算結(jié)果綁定到對(duì)應(yīng)字段。
ROOT節(jié)點(diǎn)獲取子節(jié)點(diǎn)ITEM的值使用io_read->retrieve_by_association
这揣,此方法中參數(shù)iv_fill_data
的默認(rèn)值為false悔常;在將結(jié)果綁定到對(duì)應(yīng)字段時(shí),應(yīng)注意先讀取數(shù)據(jù)值
DATA: lt_item TYPE zste_t_ord_item,
ls_item TYPE zste_s_ord_item,
ls_root TYPE zste_s_ord_root_k,
lt_root TYPE zste_t_ord_root_k,
lt_mod TYPE /bobf/t_frw_modification.
io_read->retrieve_by_association(
EXPORTING
iv_node = zif_ste_order_c=>sc_node-root " Node Name
it_key = it_key " Key Table
iv_association = zif_ste_order_c=>sc_association-root-item " Name of Association
* is_parameters =
* it_filtered_attributes = " List of Names (e.g. Fieldnames)
iv_fill_data = abap_true " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ")
* iv_before_image = ABAP_FALSE " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ")
* it_requested_attributes = " List of Names (e.g. Fieldnames)
IMPORTING
* eo_message = " Message Object
et_data = lt_item " Data Return Structure
* et_key_link = " Key Link
* et_target_key = " Key Table
* et_failed_key = " Key Table
).
io_read->retrieve(
EXPORTING
iv_node = zif_ste_order_c=>sc_node-root " Node Name
it_key = it_key " Key Table
* iv_before_image = ABAP_FALSE " Data Element for Domain BOOLE: TRUE (="X") and FALSE (=" ")
* iv_fill_data = ABAP_TRUE " Data element for domain BOOLE: TRUE (='X') and FALSE (=' ')
* it_requested_attributes = " List of Names (e.g. Fieldnames)
IMPORTING
* eo_message = " Message Object
et_data = lt_root " Data Return Structure
* et_failed_key = " Key Table
* et_node_cat = " Node Category Assignment
).
LOOP AT lt_root INTO ls_root.
LOOP AT lt_item INTO ls_item.
ls_root-total_amount_val = ls_root-total_amount_val + ls_item-amount_val.
ls_root-total_amount_cur = ls_item-amount_cur.
ENDLOOP.
/scmtms/cl_mod_helper=>mod_update_single(
EXPORTING
is_data = ls_root
iv_node = zif_ste_order_c=>sc_node-root " Node
iv_key = ls_root-key " NodeID
it_changed_fields = VALUE #( ( zif_ste_order_c=>sc_node_attribute-root-total_amount_val )
( zif_ste_order_c=>sc_node_attribute-root-total_amount_cur ) ) " List of Names (e.g. Fieldnames)
* iv_autofill_fields = 'X' " 'X' -> all not readonly fields will be determined
* iv_bo_key = " Business Object
* IMPORTING
* es_mod = " Change
CHANGING
ct_mod = lt_mod " Changes
).
ENDLOOP.
IF lt_mod IS NOT INITIAL.
io_modify->do_modify( it_modification = lt_mod ).
ENDIF.