Business Requirement:
Need to delete the ship-to-party segment data from the inbound IDOC based on some conditions.
Analysis:
Standard inbound function module IDOC_INPUT_ORDERS is being used to post the IDOC into system.
To fulfill this requirement, we need to enhance the function IDOC_INPUT_ORDERS
Resolution:
CODE SNIPPET:
FORM ZUORDNEN_ORDERS_E1EDKA1.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""$"$\SE:(1 ) Form ZUORDNEN_ORDERS_E1EDKA1, Start D
*$*$-Start: (1 )--------------------------------------------------------------------------------$*$*
ENHANCEMENT 18 ZLVEDAF5T_ORDERS_IDOC_V1. "active version
*
DATA: WE_E1EDKA1 TYPE E1EDKA1,
wa_zvari TYPE zvari,
it_zvari TYPE STANDARD TABLE OF zvari,
fval TYPE FIELDVALUE.
DATA: l_t_edidd like edidd occurs 0,
l_t_edidd_chg LIKE edidd occurs 0,
l_w_edidd_chg LIKE edidd.
* DATA: begin of idoc_contrl_new OCCURS 0.
* INCLUDE STRUCTURE edidc.
* DATA: END OF IDOC_CONTRL_NEW.
DATA: idoc_contrl_new TYPE edidc,
idoc_contrl_new1 TYPE edidc.
* idoc_status_new TYPE BDIDOCSTAT OCCURS 0 with header line.
DATA: BEGIN OF IDOC_STATUS_NEW OCCURS 0.
INCLUDE STRUCTURE BDIDOCSTAT.
DATA: END OF IDOC_STATUS_NEW.
data: L_FLAG type c.
DATA:
* table of status values
L_STATUS_RECORD LIKE EDI_DS.
FIELD-SYMBOLS: <fs_idoc_data> TYPE edidd.
CASE E1EDKA1-PARVW.
WHEN 'WE'.
IF e1edka1-name1 IS INITIAL AND e1edka1-name2 IS INITIAL
AND e1edka1-name3 IS INITIAL AND e1edka1-name4 IS INITIAL
AND e1edka1-stras IS INITIAL AND e1edka1-strs2 IS INITIAL
AND e1edka1-pfach IS INITIAL AND e1edka1-ort01 IS INITIAL
AND e1edka1-counc IS INITIAL AND e1edka1-pstlz IS INITIAL.
***Do nothing...
ELSE.
IF NOT E1EDKA1-LIFNR IS INITIAL.
REFRESH: it_zvari.
fval = E1EDKA1-LIFNR.
SELECT * FROM zvari INTO TABLE it_zvari
WHERE programname EQ 'EDI_ORDERS'
AND fieldname EQ 'MISC_SHIPTO'
AND begda LE sy-datum
AND endda GE sy-datum
AND fieldvalue EQ fval.
IF it_zvari IS INITIAL.
MOVE-CORRESPONDING e1edka1 TO we_e1edka1.
READ TABLE IDOC_DATA ASSIGNING <fs_idoc_data> WITH KEY
segnum = idoc_data-segnum.
IF sy-subrc IS INITIAL.
***Change the IDOC SEGMENT DATA
***Open IDOC for edit
REFRESH: l_t_edidd[].
CALL FUNCTION 'EDI_DOCUMENT_OPEN_FOR_EDIT'
EXPORTING
document_number = IDOC_CONTRL-DOCNUM
* ALREADY_OPEN = 'N'
* IMPORTING
* IDOC_CONTROL =
tables
idoc_data = L_T_EDIDD[]
EXCEPTIONS
DOCUMENT_FOREIGN_LOCK = 1
DOCUMENT_NOT_EXIST = 2
DOCUMENT_NOT_OPEN = 3
STATUS_IS_UNABLE_FOR_CHANGING = 4
OTHERS = 5
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
CLEAR e1edka1.
MOVE: we_e1edka1-parvw TO e1edka1-parvw,
we_e1edka1-lifnr TO e1edka1-lifnr.
<fs_idoc_data>-sdata = e1edka1.
CLEAR idoc_data-sdata.
IDOC_DATA-SDATA = E1EDKA1.
l_w_edidd_chg = IDOC_DATA.
***Change segment here
CALL FUNCTION 'EDI_CHANGE_DATA_SEGMENT'
EXPORTING
idoc_changed_data_record = L_W_EDIDD_CHG
EXCEPTIONS
IDOC_NOT_OPEN = 1
DATA_RECORD_NOT_EXIST = 2
OTHERS = 3
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
***Close the IDOC
CALL FUNCTION 'EDI_DOCUMENT_CLOSE_EDIT'
EXPORTING
document_number = IDOC_CONTRL-DOCNUM
* DO_COMMIT = 'X'
* DO_UPDATE = 'X'
* WRITE_ALL_STATUS = 'X'
* STATUS_75 = ' '
* TABLES
* STATUS_RECORDS =
EXCEPTIONS
IDOC_NOT_OPEN = 1
DB_ERROR = 2
OTHERS = 3
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* Write status records..................................................
* create status record
* L_STATUS_RECORD-DOCNUM = IDOC_CONTRL-DOCNUM.
* L_STATUS_RECORD-LOGDAT = SY-DATUM.
* L_STATUS_RECORD-LOGTIM = SY-UZEIT.
* L_STATUS_RECORD-REPID = SY-REPID.
* L_STATUS_RECORD-ROUTID = 'IDOC_STATUS_64OR65_SET'.
* L_STATUS_RECORD-STATUS = '64'.
* L_STATUS_RECORD-UNAME = SY-UNAME.
** fill status fields according to ale service information
* L_STATUS_RECORD-STAMQU = 'SAP'.
** L_STATUS_RECORD-STAMID = ALE_MESSAGE_IN-MSGID.
** L_STATUS_RECORD-STATYP = ALE_MESSAGE_IN-MSGTY.
** L_STATUS_RECORD-STAMNO = ALE_MESSAGE_IN-MSGNO.
** L_STATUS_RECORD-STAPA1 = ALE_MESSAGE_IN-MSGV1.
** L_STATUS_RECORD-STAPA2 = ALE_MESSAGE_IN-MSGV2.
** L_STATUS_RECORD-STAPA3 = ALE_MESSAGE_IN-MSGV3.
** L_STATUS_RECORD-STAPA4 = ALE_MESSAGE_IN-MSGV4.
*
*
** write status record
* CALL FUNCTION 'EDI_DOCUMENT_STATUS_SET'
* EXPORTING
* DOCUMENT_NUMBER = IDOC_CONTRL-DOCNUM
* IDOC_STATUS = L_STATUS_RECORD
* EXCEPTIONS
* DOCUMENT_NUMBER_INVALID = 1
* OTHER_FIELDS_INVALID = 2
* STATUS_INVALID = 3
* OTHERS = 4.
data: L_NO_DB type c value 'N',
SYNC TYPE C VALUE 'S'.
CALL FUNCTION 'EDI_DOCUMENT_OPEN_FOR_PROCESS'
EXPORTING
DB_READ_OPTION = L_NO_DB
DOCUMENT_NUMBER = idoc_contrl-docnum
ENQUEUE_OPTION = SYNC
IMPORTING
IDOC_CONTROL = IDOC_CONTRL_NEW
EXCEPTIONS
DOCUMENT_FOREIGN_LOCK = 01
DOCUMENT_NOT_EXIST = 02
DOCUMENT_NUMBER_INVALID = 03
ERROR_MESSAGE = 04
OTHERS = 05.
CLEAR idoc_contrl_new.
REFRESH: idoc_status_new.
idoc_status_new-docnum = idoc_contrl-docnum.
idoc_status_new-status = '64'.
APPEND idoc_status_new.
L_FLAG = 'X'.
CALL FUNCTION 'IDOC_STATUS_WRITE_TO_DATABASE'
EXPORTING
IDOC_NUMBER = IDOC_CONTRL-DOCNUM
IDOC_OPENED_FLAG = l_flag
IMPORTING
IDOC_CONTROL = IDOC_CONTRL_NEW
TABLES
IDOC_STATUS = IDOC_STATUS_NEW
EXCEPTIONS
IDOC_FOREIGN_LOCK = 1
IDOC_NOT_FOUND = 2
IDOC_STATUS_RECORDS_EMPTY = 3
IDOC_STATUS_INVALID = 4
DB_ERROR = 5
OTHERS = 6 .
IF sy-subrc EQ 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
clear l_flag.
CALL FUNCTION 'EDI_DOCUMENT_CLOSE_PROCESS'
EXPORTING
DOCUMENT_NUMBER = IDOC_CONTRL-DOCNUM
NO_DEQUEUE = l_flag
IMPORTING
IDOC_CONTROL = IDOC_CONTRL_NEW
EXCEPTIONS
DOCUMENT_NOT_OPEN = 01
FAILURE_IN_DB_WRITE = 02
PARAMETER_ERROR = 03
STATUS_SET_MISSING = 04.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
WHEN OTHERS.
***Do nothing...
ENDCASE.
ENDENHANCEMENT.
CONCLUSION:
IDOC Edit Process:
EDI_DOCUMENT_OPEN_FOR_EDIT
This function is used to open IDOC segment data in edit mode.
EDI_CHANGE_DATA_SEGMENT
We can pass the changed segment data to this function.
EDI_DOCUMENT_CLOSE_EDIT
We need to close the editing process of the IDOC segments.
After performing above three steps, IDOC status would be 69 (IDOC Was Edited).
IDOC Status Change:
To post the IDOC in the same LUW, we need to change the IDOC status from 69 to 64. To do this, we should use the below function modules.
EDI_DOCUMENT_OPEN_FOR_PROCESS
IDOC_STATUS_WRITE_TO_DATABASE
EDI_DOCUMENT_CLOSE_PROCESS
All the Statuses that IDOC would go through during this process:
Observation:
Status was changed to 69 from 64. This is because we changed the segments data.
Status was changed from 69 to 64. We should change the status to post the IDOC in the same LUW.
Hope You have enjoyed this presentation ...
Thanks...
Vijaymadhur Papabathini