In this blog I am going to talk about how to change an existing PO’s using function modules and also trigger process controlled workflow that is set up for purchase order. The reason why I am specifically mentioning the workflow is the FMs that we are going to use to update POs do not trigger process controlled workflow.. For process controlled workflow, we need to explicitly trigger the workflow by mimicking the ORDER process from WebUI.

Below is the code snippet for changing / adding new items to an existing PO

* Passing the PO and getting header data

  CALL FUNCTION ‘BBP_PD_PO_GETLIST’
EXPORTING
i_object_id                        
= lx_data_tempobject_id
TABLES
e_pdlist                           
= lt_e_pdlist
*   E_STATUS                            =
e_messages                         
= lt_e_messages.
ENDLOOP.
DELETE ADJACENT DUPLICATES FROM lt_e_pdlist COMPARING guid.

*get the required date to update by FM
LOOP AT lt_e_pdlist INTO lx_e_pdlist .
CALL FUNCTION ‘BBP_PD_PO_GETDETAIL’
EXPORTING
i_guid                  
= lx_e_pdlistguid
i_attach_with_doc       
= ‘ ‘
i_with_itemdata         
= ‘X’
i_read_be_data          
= ‘ ‘
IMPORTING
e_header                
= ls_header_d
TABLES
e_item                  
= ls_item_d
*        e_account                = lt_acc
e_longtext              
= lt_longtext_d
e_tax                   
= lt_tax .

*Then we change the data that we want to change. for example changing longtext
IF   lt_longtext_d  IS INITIAL.

* if  lx_longtext_d-guid EQ lx_e_pdlist-guid.
lx_longtext_d
guid = lx_e_pdlistguid .
lx_longtext_d
tdid = ‘NOTE’.
lx_longtext_d
tdspras = ‘E’.
lx_longtext_d
counter = ‘0001’.
lx_longtext_d
tdformat = ‘X’.
lx_longtext_d
tdline = ‘,.’.
MOVE-CORRESPONDING lx_longtext_d TO lx_longtext_c.
APPEND lx_longtext_c TO lt_longtext_c.
ELSE.

LOOP AT lt_longtext_d INTO lx_longtext_d .
IF  lx_longtext_dguid EQ lx_e_pdlistguid.
MOVE-CORRESPONDING lx_longtext_d TO lx_longtext_c.
APPEND lx_longtext_c TO lt_longtext_c.
lv_counter
= lx_longtext_dcounter + 1.
lx_longtext_c
guid = lx_e_pdlistguid .
lx_longtext_c
tdid = ‘NOTE’.
lx_longtext_c
tdspras = ‘E’.
lx_longtext_c
counter = lv_counter .
lx_longtext_c
tdformat = ‘X’.
lx_longtext_c
tdline = ‘,.’.
APPEND lx_longtext_c TO lt_longtext_c.

ELSEIF lx_longtext_dguid NE lx_e_pdlistguid .
MOVE-CORRESPONDING lx_longtext_d TO lx_longtext_c.
APPEND lx_longtext_c TO lt_longtext_c.

lx_longtext_cguid = lx_e_pdlistguid .
lx_longtext_c
tdid = ‘NOTE’.
lx_longtext_c
tdspras = ‘E’.
lx_longtext_c
counter = lv_counter.
lx_longtext_c
tdformat = ‘X’.
lx_longtext_c
tdline = ‘,.’.
APPEND lx_longtext_c TO lt_longtext_c.
ENDIF.
ENDLOOP.
ENDIF.

DATA : lt_tax_c TYPE STANDARD TABLE OF bbp_pds_tax,
lt_message
TYPE STANDARD TABLE OF bbp_pds_messages,
lt_item_c
TYPE STANDARD TABLE OF bbp_pds_po_item_icu.
    DATA : lv_wiid type SWW_WIID,
          lv_user_id  TYPE sy-uname,
         lo_po_instance    TYPE REF TO /sapsrm/cl_pdo_bo_po_adv,
         lo_message_handler  TYPE REF TO /sapsrm/if_pdo_msg_consumer,
        lv_error            TYPE      xfeld.


*After you have everything ready, call BBP_PD_PO_UPDTATE as shown below
MOVE-CORRESPONDING ls_header_d TO ls_header_u.
CALL FUNCTION ‘BBP_PD_PO_UPDATE’
EXPORTING
i_park    
= ‘X’
i_save    
= ‘X’
i_header  
= ls_header_u
IMPORTING
e_changed 
= lv_changed
es_header  
= ls_header_e
TABLES
i_longtext
= lt_longtext_c
e_messages
= lt_message.

*in general, after this we call BBP_PD_PO_SAVE FM and do COMMIT WORK,
*but we don’t use BBP_PD_PO_SAVE here as it does not trigger process controlled workflow.
*So from this point on wards we have to follow SRM7 approach of ordering the PO.

*CALL FUNCTION ‘BBP_PD_PO_SAVE’
* EXPORTING
**   IV_WORKITEM_ID               =
**   IV_USERTYPE                  =
*   IV_HEADER_GUID               = ls_header_e-guid
**   IV_CREATE_HIST_VERSION       =
**   IV_KEEP_OLD_CHANGER          =
**   IV_NEW_CHANGED_BY            =
**   IV_KEEP_OLD_CREATER          =
**   IV_NEW_CREATED_BY            =
*          .
*
*CALL FUNCTION ‘BBP_PROCDOC_RESET_BUFFER’
** EXPORTING
**   I_NO_STATUS       = ‘ ‘
**   I_NO_WFL          = C_ON
*          .
*COMMIT WORK AND WAIT.
lv_user_id
= ‘WF-BATCH’.
*Now order the PO in display mode by using OO based concept.
TRY.
*Get instance of change version PO
CALL METHOD /sapsrm/cl_pdo_bo_po_adv=>get_po_adv_instance
EXPORTING
iv_header_guid 
= ls_header_eguid
iv_mode        
= ‘DISPLAY’
iv_wiid        
= lv_wiid
iv_process_type
= ‘ECPO’
iv_user_id     
= lv_user_id
RECEIVING
ro_instance    
= lo_po_instance.
*Do a check
CALL METHOD lo_po_instance->/sapsrm/if_pdo_base~check
CHANGING
co_message_handler
= lo_message_handler.
*Order the PO which is in HELD status
CALL METHOD lo_po_instance->/sapsrm/if_pdo_bo_po~order
CHANGING
co_message_handler
= lo_message_handler.

*read the messages
CALL METHOD lo_message_handler->get_messages
IMPORTING
et_messages
= lt_messages_class.

COMMIT WORK.

WAIT UP TO 1 SECONDS.
CATCH /sapsrm/cx_pdo_wf_mode_ban
/sapsrm/cx_pdo_wrong_bus_type
/sapsrm/cx_pdo_pd_read_error
/sapsrm/cx_pdo_lock_failed
/sapsrm/cx_pdo_no_authorizatio
/sapsrm/cx_pdo_parameter_error
/sapsrm/cx_pdo_status_error
/sapsrm/cx_pdo_incons_user
/sapsrm/cx_pdo_error
/sapsrm/cx_pdo_unlock_error
/sapsrm/cx_pdo_abort
/sapsrm/cx_wf_abort
/sapsrm/cx_wf_error
INTO lo_root.
* read the exception message
lv_msg
= lo_root->get_text( ).

ENDTRY.
CLEAR : lt_longtext_d , ls_header_d ,lv_wiid ,lt_messages_class .
CLEAR : lo_message_handler , lo_po_instance , ls_header_e.
CLEAR : lv_msg.
ENDLOOP.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply