Tuesday, June 11, 2013

Update Partner details in a Purchase Order

This is a very simple code sample to update the purchase order partner details.  The following function modules are used:
 CRMD_PARTNER table contains partner details .
BBP_PD_PO_GETDETAIL : Read the details of a Purchase Order
BBP_PD_PO_UPDATE : Updates the details of a Purchase Order
BBP_PD_PO_SAVE : Finally called to save the details of a Purchase Order
This is just an illustrative example and can be enhanced to update any existing detail of a purchase order. 

REPORT  zkb_partner_chg.

* Declaration - Internal Tables
DATA: lt_e_item     TYPE TABLE OF bbp_pds_po_item_d.
DATA: lt_e_partner  TYPE TABLE OF bbp_pds_partner.
DATA: lt_e_messages TYPE TABLE OF bbp_pds_messages.
DATA: lt_i_item     TYPE TABLE OF bbp_pds_po_item_icu.
DATA: lt_i_partner  TYPE TABLE OF bbp_pds_partner.

* Declaration - Work Areas
DATA: ls_e_header   TYPE bbp_pds_po_header_d.
DATA: ls_e_item     TYPE bbp_pds_po_item_d.
DATA: ls_e_partner  TYPE bbp_pds_partner.
DATA: ls_i_item     TYPE bbp_pds_po_item_icu.
DATA: ls_i_header   TYPE bbp_pds_po_header_u .
DATA: ls_but000     TYPE but000.

* Declaration - Variables
DATA: lv_e_changed TYPE  xfeld.

* Declaration - Field Symbols
FIELD-SYMBOLS: <fs_partner> TYPE bbp_pds_partner.

* Read an existing PO
CALL FUNCTION 'BBP_PD_PO_GETDETAIL'
  EXPORTING
    i_object_id     = '3200000576'
    i_with_itemdata = 'X'
  IMPORTING
    e_header        = ls_e_header
  TABLES
    e_item          = lt_e_item
    e_partner       = lt_e_partner
    e_messages      = lt_e_messages.

* Populate Header Data
MOVE-CORRESPONDING ls_e_header TO ls_i_header.

* Populate Item data
LOOP AT lt_e_item INTO ls_e_item .
  MOVE-CORRESPONDING ls_e_item TO ls_i_item.
  APPEND ls_i_item TO lt_i_item.
ENDLOOP.

* Populate all the existing partner data first
LOOP AT lt_e_partner INTO ls_e_partner.
  APPEND ls_e_partner TO lt_i_partner.
ENDLOOP.

** Delete Partner (vendor)
READ TABLE lt_i_partner ASSIGNING <fs_partner>
     WITH KEY partner_fct = '00000019'
              p_guid = ls_i_header-guid.
IF sy-subrc EQ 0.
  <fs_partner>-del_ind = 'X'.
ENDIF.
UNASSIGN <fs_partner>.

** Add Partner (vendor)
SELECT SINGLE * FROM but000 INTO ls_but000 WHERE partner = '0087000004'.
CLEAR ls_e_partner.
ls_e_partner-partner_guid = 1.
ls_e_partner-partner_no = ls_but000-partner_guid.
ls_e_partner-partner_fct = '00000019'.
ls_e_partner-p_guid = ls_i_header-guid.
APPEND ls_e_partner TO lt_i_partner.

* Update Doc
CALL FUNCTION 'BBP_PD_PO_UPDATE'
  EXPORTING
    i_header                = ls_i_header
    i_save                  = 'X'
    iv_with_change_approval = ' '
  IMPORTING
    e_changed               = lv_e_changed
  TABLES
    i_item                  = lt_i_item
    i_partner               = lt_i_partner
    e_messages              = lt_e_messages.

* Save Doc
IF NOT lv_e_changed IS INITIAL.
  CALL FUNCTION 'BBP_PD_PO_SAVE'
    EXPORTING
      iv_header_guid = ls_i_header-guid.
  COMMIT WORK AND WAIT.
ENDIF.

No comments:

Post a Comment