Create confirmations entries with a Badi
I got the problem that I have to add a line for confirmation to migrated purchase order. So if you want to try this, you need a PO. Let us assume you got a PO (EL00000001) with one position (0010). As you can see there is no confirmation. We used material 990003038 and the standard plant 1000.
Now, lets have a look into transaction MD04, using material 990003038 and the plant 1000. You should see something like this.
Now we want to add the confirmation. If you add it by hand:
ATTENTION: In this tutorial a table will be updated with the MODIFY command. Please stop here if the command is not clear and read this. You may damage the system if you do something wrong.
First of all I’ll present you the tables we need.
EKKO – EinKauf KOpf – Header PO (yes, German is my mother tongue)
EKPO – EinKauf POsition – Position PO
EKES - Vendor Confirmations
EKET - Scheduling Agreement Schedule Lines
First I’ll create another PO (EL00000002) with the same position (0010).
So here is the program I used:
DATA: lt_uekes TYPE TABLE OF uekes,
ls_uekes LIKE LINE OF lt_uekes,
lt_uekes_out TYPE TABLE OF uekes,
lv_etens TYPE etens.
DATA: lv_date TYPE eindt.
DATA: lv_ebelp TYPE ebelp VALUE '10'.
DATA: lv_ebeln TYPE ebeln VALUE 'EL00000002'.
DATA: lv_xblnr TYPE ekes-xblnr VALUE 'REFERENCE'.
DATA: lv_data_char10 TYPE char10 VALUE '01.07.2015'.
DATA: lv_menge TYPE bbmng VALUE '5'.
DATA: lv_ekpomeins TYPE bstme VALUE 'PC'.
DATA: ls_return TYPE bapiret2.
DATA: ls_eket TYPE eket.
DATA: lt_eket TYPE STANDARD TABLE OF eket.
"Just make sure the the format is right.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_ebeln
IMPORTING
output = lv_ebeln.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_ebelp
IMPORTING
output = lv_ebelp.
"Change the date to internal format
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
EXPORTING
date_external = lv_data_char10
IMPORTING
date_internal = lv_date
EXCEPTIONS
date_external_is_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
"Check if there is already a ekes entry. If so you have to increase ETENS by one.
"ETENS is an incremental counter in the table.
SELECT MAX( etens ) INTO lv_etens
FROM ekes
WHERE ebeln = lv_ebeln
AND ebelp = lv_ebelp.
IF sy-subrc = 0 AND lv_etens <> 0.
ADD 1 TO lv_etens.
ENDIF.
ls_uekes-ebeln = lv_ebeln. "Purchase order number you want to update 'EL00000002'
ls_uekes-ebelp = lv_ebelp. "Position you want to update '00010' --> beware 5 digits
ls_uekes-etens = lv_etens. "Counter in EKES
ls_uekes-ebtyp = 'AB'. "Short form of the confirm category
ls_uekes-xblnr = lv_xblnr. "The referenc sting
ls_uekes-eindt = lv_date. "Delivery date of the confirmation
ls_uekes-menge = lv_menge. "Quantety of the confirmation
ls_uekes-ekpomeins = lv_ekpomeins. "ISO UNIT
ls_uekes-lpein = '1'.
ls_uekes-erdat = sy-datum. "Date when it was added
ls_uekes-ezeit = sy-uzeit. "time when it was added
ls_uekes-estkz = '1'.
ls_uekes-kzdis = abap_true.
ls_uekes-kz = 'I'. "I is for Insert, U for Update
APPEND ls_uekes TO lt_uekes.
CALL FUNCTION 'ME_CONFIRMATION_UPDATE'
EXPORTING
i_ebeln = lv_ebeln "Purchase order number is the key for the FuMo(FUBA)
TABLES
xekes = lt_uekes.
IF sy-subrc = 0.
"You have to call the commit, because you want to find an EKET entry
"I you don't do this the next select will fail
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'
IMPORTING
return = ls_return.
ENDIF.
"Okay now EKES is ready, but the will be no update to md04 if you dont update EKET.
"I couldn't find a FuMo(FUBA) which will do the update for me.
"But beware and think about what you do here.
SELECT SINGLE *
FROM eket
INTO ls_eket
WHERE ebeln = lv_ebeln AND
ebelp = lv_ebelp.
ls_eket-dabmg = lv_menge.
APPEND ls_eket TO lt_eket.
MODIFY eket FROM TABLE lt_eket.
IF sy-subrc = 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'
IMPORTING
return = ls_return.
ENDIF.