Requirement: Create a new HU and pack it into the shipment. Basically, it is needed for intercompany stock transfer.
Below listed FMs will be required for HU packing.
* Clear handling unit management memory.
CALL FUNCTION 'HU_PACKING_REFRESH'.
* Clear serial number management memory.
CALL FUNCTION 'SERIAL_INTTAB_REFRESH'.
* Clear status management memory.
CALL FUNCTION 'STATUS_BUFFER_REFRESH'.
* Call SAP to read the shipment.
CALL FUNCTION 'RV_SHIPMENT_VIEW'.
* Call SAP to read Delivery Data.
CALL FUNCTION 'DG56_LIPS_READ'.
* Call SAP to create the new HU in the database.
CALL FUNCTION 'SD_SHIPMENT_PROCESS_HU'.
* Call FM to end the shipment process.
CALL FUNCTION 'SD_SHIPMENT_PROCESS_END'
******************************************************************************************************************
Main part.
DATA: lv_no_del TYPE rv56a-selkz.
DATA: lv_activity TYPE t180-trtyp VALUE ' '.
* Shipment header
DATA: t_vttkvb TYPE vttkvb.
* HU contents - pack on create
DATA: hu_content_data TYPE verpo.
DATA: i_logfile TYPE sprot_u,
lt_logfile TYPE v56e_logfile,
lt_activities TYPE v56e_shipment_activities,
lt_shipunit TYPE v56e_shipping_unit,
t_shipment TYPE v56e_shipment.
DATA: i_vbplk TYPE vbplk,
t_vbplk LIKE TABLE OF i_vbplk,
i_vbplp TYPE vbplp,
t_vbplp LIKE TABLE OF i_vbplp,
i_vbpls TYPE vbpls,
t_vbpls LIKE TABLE OF i_vbpls.
DATA: i_vttp TYPE vttpvb,
t_vttp LIKE TABLE OF i_vttp.
DATA: lwa_lips TYPE lips,
lit_lips TYPE STANDARD TABLE OF lips,
lit_tab TYPE STANDARD TABLE OF lips.
DATA: lwa_data TYPE verpo,
lit_update TYPE hum_update_header_t,
lwa_update TYPE hum_update_header.
FIELD-SYMBOLS:<lfs_lips> TYPE lips.
CALL FUNCTION 'HU_PACKING_REFRESH'.
* Clear serial number management memory.
CALL FUNCTION 'SERIAL_INTTAB_REFRESH'.
* Clear status management memory.
CALL FUNCTION 'STATUS_BUFFER_REFRESH'.
* activitiy type for the shipment
* A = display
* H = add
lv_activity = 'A'.
* Call SAP to read the shipment.
CALL FUNCTION 'RV_SHIPMENT_VIEW'
EXPORTING
shipment_number = lv_tknum
option_tvtk = 'X'
option_ttds = 'X'
language = sy-langu
option_items = 'X'
option_packages = 'X'
option_package_dialog = 'X'
option_flow = 'X'
option_authority_check = 'X'
activity = lv_activity
option_no_refresh = ' '
option_ignore_missing_deliv = 'X'
i_filter_type = 'F'
IMPORTING
f_vttkvb = t_vttkvb
f_tvtk = t_shipment-tvtk
f_ttds = t_shipment-ttds
e_delivery_missing = lv_no_del
TABLES
f_vttp = t_shipment-xvttp
f_trlk = t_shipment-vtrlk
f_trlp = t_shipment-vtrlp
f_vtts = t_shipment-xvtts
f_vtsp = t_shipment-xvtsp
f_vbpa = t_shipment-xvbpa
f_vbadr = t_shipment-xsadr
f_vtfa = t_shipment-xvtfa
f_vbplk = t_vbplk
f_vbplp = t_vbplp
f_vbpls = t_vbpls
f_yvtfa = t_shipment-yvtfa
EXCEPTIONS
not_found = 1
no_authority = 2
delivery_missing = 3
delivery_lock = 4
OTHERS = 5.
IF sy-subrc EQ 0.
*fetch the deliveries from shipment
LOOP AT it_delivery INTO lwa_delivery.
CALL FUNCTION 'DG56_LIPS_READ'
EXPORTING
i_delivery_id = lwa_delivery-vbeln
TABLES
e_lips_tab = lit_tab.
IF sy-subrc = 0.
APPEND LINES OF lit_tab TO lit_lips.
CLEAR:lit_tab.
ELSE.
"error message
ENDIF.
CLEAR:lwa_delivery.
ENDLOOP.
* reset the table for the updates
CLEAR: t_shipment-xvttk[].
APPEND t_vttkvb TO t_shipment-xvttk.
* Activities structure: change an existing shipment.
CLEAR: lt_activities.
lt_activities-change-execute = 'X'.
lt_activities-change-shipment_number = v7_tknum.
* Handling unit structure: create a new handling unit.
CLEAR: lt_shipunit.
lt_shipunit-create-execute = 'X'.
lt_shipunit-data-vhilm = " packing material.
lt_shipunit-data-exidv = "HU name (optional)|.
lt_shipunit-content-execute = 'X'.
LOOP AT lit_lips ASSIGNING <lfs_lips>.
* contents of the HU to be created on the shipment
hu_content_data-exidv_ob = "HU name (optional)| Same like given above.
hu_content_data-velin = 1.
hu_content_data-vbeln = <lfs_lips>-vbeln.
hu_content_data-posnr = <lfs_lips>-posnr.
hu_content_data-vrkme = <lfs_lips>-meins.
hu_content_data-matnr = <lfs_lips>-matnr.
hu_content_data-werks = <lfs_lips>-werks.
hu_content_data-lgort = <lfs_lips>-lgort.
APPEND hu_content_data TO lt_shipunit-content-data.
CLEAR:hu_content_data.
ENDLOOP.
* Add handling unit structure to activity structure.
APPEND lt_shipunit TO lt_activities-shipping_units.
CLEAR:lt_shipunit.
* Call SAP to create the new HU in the database.
CLEAR: lt_logfile[].
CALL FUNCTION 'SD_SHIPMENT_PROCESS_HU'
IMPORTING
e_logfile = lt_logfile
CHANGING
c_activities = lt_activities
c_shipment = t_shipment
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc EQ 0.
CLEAR:lt_logfile.
CALL FUNCTION 'SD_SHIPMENT_PROCESS_END'
IMPORTING
e_logfile = lt_logfile
CHANGING
c_activities = lt_activities
c_shipment = t_shipment
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc NE 0.
"error message
endif.
endif.
endif.
Conclusion: Handling units will be automatically packed into Shipment with the material rather creating HU into delivery, FM will reduce the program line like creation of HU, assigning it to shipment and packing materials.