You are on page 1of 7

8/8/23, 11:11 BAPI_ACC_DOCUMENT_POST – Vendor Down payment: Update Purchase order info and PO history | SAP Blogs

Community

 SAP Community Log-in Update 


In a few months, SAP Community will switch to
SAP Universal ID as the only option to login. Get started with SAP Universal ID
Create your SAP Universal ID now! If you have
multiple S- or P- accounts, use the
Consolidation Tool to merge your content.

Ask a Question Write a Blog Post Login

Technical Articles

Gaurav Karkara
August 29, 2020 | 5 minute read

BAPI_ACC_DOCUMENT_POST –
Vendor Down payment: Update
Purchase order info and PO history
Follow
 1  7  4,649

 Like
There usually is a requirement to post accounting documents in S/4HANA through
an interface, program or an enhancement. Irrespective of the trigger point, BAPI
 RSS Feed ‘BAPI_ACC_DOCUMENT_POST’ does its job well and used extensively in custom
developments.

Recently, we got a requirement to post vendor down payments through an interface,


which can be done with above mentioned BAPI except that vendor table
(ACCOUNTPAYABLE) in BAPI interface doesn’t have a ‘Purchase Order’ field. If you
post the vendor down payment through FB01 transaction, you have an option to
enter PO number and item number, which gets saved in BSEG table as well. Also,
when you enter PO number in FB01 transaction, PO history is updated with

https://blogs.sap.com/2020/08/29/bapi_acc_document_post-vendor-down-payment-update-purchase-order-info-and-po-history/ 1/7
8/8/23, 11:11 BAPI_ACC_DOCUMENT_POST – Vendor Down payment: Update Purchase order info and PO history | SAP Blogs

generated document number, which doesn’t happen using BAPI. Mostly, having PO
information and PO history update is an important requirement to achieve for
customer for various reasons and above BAPI doesn’t provide for this requirement.

In this blog post, I am going to explain the solution we implemented to achieve above
requirement.

Update Purchase Order Info


Substitutions Configuration
To have purchase order information in generated accounting document, we will make
use of substitutions. There is lot of information available over the internet regarding
substitutions. So, I will keep steps short:

Transaction GGB1 –> Financial Accounting -> Line Item (since we want to substitute
a line item level information) -> Create a substitution using menu Substitution ->
create or edit the one already existing -> Add a new step(Edit -> Insert Step) ->
Choose the particular field that you want to substitute or just use option as ‘Exit’ as
shown below:

Define an exit number starting with 9. You have the option to set some prerequisites
as well e.g. if you want to trigger the substitution for a particular document type.
While you make all above settings and save, system doesn’t ask for any transport
request.

To transport these substitution settings, choose your substitution and use menu
Substitution -> Transport. On the next screen, uncheck ‘Logical Rules’ and execute
thus save your settings in a TR.

Form Routine Implementation


U901 defined in above setting is actually a subroutine name which we will implement.
Copy program RGGBS000 to a customer defined name ZRGGBS000 (or any other
name). Maintain this custom defined program at below SPRO path:

https://blogs.sap.com/2020/08/29/bapi_acc_document_post-vendor-down-payment-update-purchase-order-info-and-po-history/ 2/7
8/8/23, 11:11 BAPI_ACC_DOCUMENT_POST – Vendor Down payment: Update Purchase order info and PO history | SAP Blogs

and update below entry:

These settings will make substitutions trigger your custom program, instead of
standard program RGGBS000.

In program ZRGGBS000, implement the custom subroutine appending to already


defined form routines.

FORM U901.
*Write your code here
ENDFORM.

In this subroutine, we check the fields ESRRE and ESRNR from BSEG and
accordingly populate the EBELN and EBELP fields. You may include other pre-
conditions here as well before updating these fields. BSEG and BKPF tables contain
runtime data.

https://blogs.sap.com/2020/08/29/bapi_acc_document_post-vendor-down-payment-update-purchase-order-info-and-po-history/ 3/7
8/8/23, 11:11 BAPI_ACC_DOCUMENT_POST – Vendor Down payment: Update Purchase order info and PO history | SAP Blogs

IF bseg-umsks = 'A' AND bseg-ebeln IS INITIAL AND bseg-ebelp IS INIT

bseg-ebeln = COND #( WHEN strlen( bseg-esrre ) GT 10 THEN bseg-es


WHEN strlen( bseg-esrre ) LE 10 THEN bse
bseg-ebelp = COND #( WHEN strlen( bseg-esrnr ) GT 5 THEN bseg-esr
WHEN strlen( bseg-esrnr ) LE 5 THEN bseg-esr
ENDIF.

Above code should update the Purchase order and line item number for a generated
document.

It is also possible that even after writing above logic, required information is not
updated. Then, check below information:

1. Go to SM30 and Open maintenance view ‘VWTYGB01’. Check for BSEG- EBELN
and BSEG-EBELP entries:

 If ‘Exclude’ checkbox is ticked, please uncheck it and save the entries, in a
transport.
2. Execute program ‘RGUGBR00’ to regenerate the substitutions. You should run
this program  in each system also after TR movements.

Above 2 steps should solve the problem.

You can use above steps to update any other field as well, just do the required coding
in routine and make sure fields are not excluded from substitutions.

PO History Update
To update the PO history, we have to implement a BTE:

https://blogs.sap.com/2020/08/29/bapi_acc_document_post-vendor-down-payment-update-purchase-order-info-and-po-history/ 4/7
8/8/23, 11:11 BAPI_ACC_DOCUMENT_POST – Vendor Down payment: Update Purchase order info and PO history | SAP Blogs

This BTE gets triggered just before posting the document and system has completed
all its checks. It is important not to change any field value in this BTE as there won’t
be any further system checks and an undesired value can get updated in the
database.

We will just read the purchase order information and update the PO history.
Implement the BTE and in the BTE function module, mention below code (make sure
that code is triggering only for your interface using any document value or memory
id, we don’t want standard transaction executing our code):

*-- Update the PO Number & PO Item Number to BSEG table


LOOP AT t_bseg ASSIGNING FIELD-SYMBOL(<lfs_bseg>) WHERE umsks = 'A
IF <lfs_bseg>-esrre IS NOT INITIAL AND <lfs_bseg>-esrnr IS NOT
<lfs_bseg>-ebeln = COND #( WHEN strlen( <lfs_bseg>-esrre ) GT
WHEN strlen( <lfs_bseg>-esrre ) LE
<lfs_bseg>-ebelp = COND #( WHEN strlen( <lfs_bseg>-esrnr ) GT
WHEN strlen( <lfs_bseg>-esrnr ) LE

*-- We are updating this local table to track if we have updated any v
*-- reverse the same after updating PO History.
APPEND INITIAL LINE TO lt_bseg ASSIGNING FIELD-SYMBOL(<lfs_bse
<lfs_bseg_elm> = CORRESPONDING #( <lfs_bseg> ).
ENDIF.
ENDLOOP.

*-- Call FM Update Purchasing Document


CALL FUNCTION 'ME_CREATE_HISTORY_FINANCE'
TABLES
t_bkpf = t_bkpf
t_bseg = t_bseg
EXCEPTIONS
error_message = 9.

IF sy-subrc EQ 9. “ Important to have a abend message here


MESSAGE ID sy-msgid TYPE 'A' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

*-- Clear the Po Number and Po Item - If we have updated above, Keepi
LOOP AT lt_bseg ASSIGNING <lfs_bseg_elm>.
READ TABLE t_bseg ASSIGNING <lfs_bseg> WITH KEY bukrs = <lfs_bse
belnr = <lfs_bse
gjahr = <lfs_bse
buzei = <lfs_bse
IF sy-subrc IS INITIAL.
CLEAR: <lfs_bseg>-ebeln,
https://blogs.sap.com/2020/08/29/bapi_acc_document_post-vendor-down-payment-update-purchase-order-info-and-po-history/ 5/7
8/8/23, 11:11 BAPI_ACC_DOCUMENT_POST – Vendor Down payment: Update Purchase order info and PO history | SAP Blogs

<lfs_bseg>-ebelp.
ENDIF.

ENDLOOP.

To conclude, above solutions help us address some BAPI limitations regarding


update of some particular field values. Same solution can be used for other fields as
well.

Alert Moderator

Assigned Tags

SAP S/4HANA

ABAP Development

FIN (Finance)

SAP S/4HANA Finance

BAPI_ACC_DOCUMENT_POST

Similar Blog Posts 


SAP Ariba Supply Chain Collaboration (SCC)
By Shekhar Prasad Jun 04, 2019

Focus is on MIRO performance


By Carla Bussolo Jan 28, 2015

Automatic Creation of PO by Goods Receipts


By Vamsi Kumar Reddy Jun 23, 2023

Related Questions 
BAPI for F-59
By Fatima Mustafayeva Apr 21, 2023

https://blogs.sap.com/2020/08/29/bapi_acc_document_post-vendor-down-payment-update-purchase-order-info-and-po-history/ 6/7
8/8/23, 11:11 BAPI_ACC_DOCUMENT_POST – Vendor Down payment: Update Purchase order info and PO history | SAP Blogs

Net price from the PO is not updated in Info record, while automatic creation of Info record
By Venu Pappula Nov 11, 2022

BRF+ configuration to trigger PO by email to vendor and purchase group in S4H 2021
By Claire Chambers Jan 18, 2023

1 Comment

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

Chris Mills
December 14, 2022 at 8:03 am

Hey Gaurav. Hope you've been doing well? Thanks for the post, was just what I needed! I ended up doing it
slightly different using BADI_ACC_DOCUMENT to pull in the fields from extension2 rather than using
substitution but is same result. The BTE idea was very handy. Thanks - Chris

Like 0 | Share

Find us on

Privacy Terms of Use

Legal Disclosure Copyright

Trademark Preferencias de cookies

Newsletter Support

https://blogs.sap.com/2020/08/29/bapi_acc_document_post-vendor-down-payment-update-purchase-order-info-and-po-history/ 7/7

You might also like