When I came across the requirement to add business partner PO, I tried to search for solution is SDN and I only found few posts un-answered/Assumed Answered on somewhat similar requirement but there was no definite solution mentioned.
Adding Partner into Purchase Order (ME21N, ME22N)
How to update Partner function tab in ME21N Transaction
Business Requirement: Add Ordering address partner while PR is getting converted to PO via ME21N/ME59N.
Comment: We can add partner except for 'VN' using the below approach via code.
Solution: We can achieve this requirement via creating an implementation of the BADI ME_PROCESS_PO_CUST, Method PROCESS_ITEM. This method is called for every item hence in case we need to add business partners based on PR Item this method will be appropriate otherwise we can use similar code in method PROCESS_HEADER
Sample Code:
DATA: gr_header TYPE REF TO if_purchase_order_mm.
DATA: gs_header TYPE mepoheader.
DATA: gt_mmpa TYPE mmpur_t_mmpa,
gt_ekpa TYPE mmpur_t_ekpa,
gs_mmpa TYPE mmpa,
gs_ekpa TYPE ekpa,
gs_ekpa_temp TYPE ekpa,
gv_atlast_event TYPE flag,
gv_pargr TYPE pargr. "Partner Determination Procedure
CONSTANTS : gc_appli TYPE char1 VALUE 'P',
gc_parvw_ba TYPE parvw VALUE 'BA',
gc_parvw_oa TYPE parvw VALUE 'OA',
gc_flag TYPE char1 VALUE 'X'.
REFRESH: gt_mmpa[].
* Get PO Header Object
CALL METHOD im_item->get_header
RECEIVING
re_header = gr_header.
* Read PO Header Data
CLEAR gs_header.
CALL METHOD gr_header->get_data
RECEIVING
re_data = gs_header.
* Read PO Header Partners from Buffer
REFRESH: gt_ekpa[].
CALL FUNCTION 'MM_READ_PARTNERS'
EXPORTING
application = gc_appli
TABLES
x_ekpa = gt_ekpa.
CLEAR gv_pargr.
SELECT SINGLE pargr
FROM t161
INTO gv_pargr
WHERE bsart EQ gs_header-bsart
AND bstyp EQ gs_header-bstyp.
CHECK NOT gv_pargr IS INITIAL.
* To Ensure that Current Ordering Address Partner is already added to Partners Tab.
CLEAR gs_ekpa_temp.
READ TABLE gt_ekpa INTO gs_ekpa_temp WITH KEY parvw = gc_parvw_ba
lifn2 = <Partner No.>.
IF NOT sy-subrc IS INITIAL.
CLEAR gs_ekpa.
LOOP AT gt_ekpa INTO gs_ekpa.
CLEAR gs_mmpa.
MOVE-CORRESPONDING gs_ekpa TO gs_mmpa.
* We to add all the existing partner which are auto-populated to lt_mmpa[].
APPEND gs_mmpa TO gt_mmpa.
CLEAR gv_atlast_event.
AT LAST.
gv_atlast_event = gc_flag.
ENDAT.
IF gv_atlast_event EQ gc_flag.
MOVE-CORRESPONDING gs_ekpa TO gs_mmpa.
gs_mmpa-parvw = gc_parvw_ba. "Ordering Addres Partner Function
gs_mmpa-lifn2 = <Partner No.>.
CLEAR gs_ekpa_temp.
READ TABLE gt_ekpa INTO gs_ekpa_temp WITH KEY parvw = gc_parvw_oa
lifn2 = gs_ekpa-lifn2.
IF NOT sy-subrc IS INITIAL.
APPEND gs_mmpa TO gt_mmpa.
ENDIF.
ENDIF.
CLEAR gs_ekpa.
ENDLOOP.
IF NOT gt_mmpa[] IS INITIAL.
* Update Partner Tab
CALL FUNCTION 'MM_MAINTAIN_PARTNERS'
EXPORTING
application = gc_appli
ebeln = gs_header-ebeln
bstyp = gs_header-bstyp
ekorg = gs_header-ekorg
lifnr = gs_header-lifnr
pargr = gv_pargr
screen_invisible = gc_flag
TABLES
x_mmpa = gt_mmpa.
ENDIF.
ENDIF.
Please let me know if you have any questions on this.