Send Idoc file as an email at the time of Payment Run (F110)
Hi
While doing a payment run in FI via F110 an idoc of the type PEXR2001/PEXR2002 is created and a related idoc file is written to the Application Server.
Usually if an EDI link is established with the house bank and the client’s production server, this file is picked up by the bank and vendor payments are carried out (or whatever !).
We had a situation where our client having recently changed its house bank (during a roll out) did not have this connection established and therefore we had to email the idoc file to the client. Here is how we achieved this.
Assumption: The house bank setting has been carried out by FI
1. In FBZP-> Pmnt methods in country ->Corresponding to the relevant country->Choose the appropriate payment method (For me it was V)->Double click and in the next screen under the last block (Payment medium/Use classic payment medium programs). If there is a standard (RFFO) program there on needs to copy it and enhance it else the custom program needs to be enhanced.
2. For me the program was ZSRFFOEDI1, after adding email to the selection screen
In the program you will find function modules ‘FI_EDI_REMADV_PEXR2001_OUT’ or ‘FI_EDI_PAYEXT_PEXR2001_OUT’ or ‘FI_EDI_EUPEXR_IDCREF01_OUT’
depending on the idoc type. We were using idoc type PEXR2002 and the FM FI_EDI_PAYEXT_PEXR2001_OUT (modified into Z) successfully created and returns the idoc number in status 30. I had to add the ‘IMPORTING’ parameter to catch this idoc.
3. Our next step is to proccess this idoc and change it to status 03 so that it gets written to the Application server. For that we have to use the program RSEOUT00. This was done by using SUBMIT
* Process the IDOC, changes status from ’30’ to ’03’
SUBMIT rseout00 WITH docnum-low = ls_edidc-docnum
WITH p_idoctp = ls_edidc-idoctp
WITH upddat-low = sy-datum AND RETURN.
4. The Natural question now is that how does the program determine the exact file path in order to open the dataset for input, for this, we go to the Receiver port that the partner profile (created for the House bank) is using. Under the Outbound file tabstrip we see a function module that is used to determine the path of the EDI file. If this is not maintained it is better to maintain one here. Write a small piece of code here which (The Entire code given later) sets a parameter id (SAP memory) thus saving the full path name. This parameter id is later retrieved in point 3. and the dataset is then read and an attachment is created and mailed.
The relevant code for the Function Moudule
IF control-idoctp = ‘PEXR2002’.
CASE control-mestyp.
WHEN ‘PAYEXT’.
CALL FUNCTION ‘IDOC_READ_COMPLETELY’
EXPORTING
document_number = control-docnum
TABLES
INT_EDIDD = gt_edidd
EXCEPTIONS
DOCUMENT_NOT_EXIST = 1
DOCUMENT_NUMBER_INVALID = 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.
ELSE.
LOOP AT gt_edidd INTO gs_edidd WHERE segnam = ‘E1IDKU3’.
s_e1idku3 = gs_edidd-sdata.
lv_bukrs = s_e1idku3-payruncc.
* Get all GVEU company code (TVARVC)
CALL FUNCTION ‘Z_GVEUXXIN01_CHECK_BUKRS’
EXPORTING
VARIANT = ‘Z_GVEU_BUKRS’
IMPORTING
GVEU_BUKRS = lt_bukrs
EXCEPTIONS
NO_DATA_TVARVC = 1 .
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
* Pathname to be changed only for GVEU Comp.codes payment runs [v2]
* For other comp.codes follow normal proccess.
ELSE.
IF lv_bukrs NOT IN lt_bukrs.
EXIT.
ENDIF.
ENDIF.
* GVEU Company code found !
lv_bukrs_ok = ‘X’.
*
lv_datum = s_e1idku3-payrundt.
WRITE lv_datum TO l_file5-field1.
l_file5-field2 = s_e1idku3-payrunid.
l_file5-field3 = control-mestyp.
l_file5-field4 = file4-field4. “Sy.Date
l_file5-field5 = file4-field5. “Sy.Time
IF s_e1idku3-payrundt IS NOT INITIAL AND
s_e1idku3-payrunid IS NOT INITIAL.
CLEAR pathname.
CONCATENATE directory
l_file5
INTO pathname.
CONDENSE pathname NO-GAPS.
ENDIF.
ENDLOOP.
ENDIF.
* Parameter ID set here is read in INCLUDE ZSRFFORI14 to
* read from A/S & email the idoc file
IF lv_bukrs_ok = ‘X’.
SET PARAMETER ID ‘BOF’ FIELD pathname.
ENDIF.
WHEN OTHERS.
* Do Nothing
ENDCASE.
ENDIF.
The relevant code for the payment program (DME program)
CALL FUNCTION ‘ZS_EDI_PAYEXT_PEXR2001_OUT’
EXPORTING
REGUH_IN = REGUH
REGUD_IN = REGUD
XEINZ_IN = REGUD-XEINZ
*{ INSERT DS3K916005 2
IMPORTING
DOCNUM_OUT = ls_edidc-docnum
*} INSERT
TABLES
TAB_REGUP = TAB_REGUP
CHANGING
XAVIS_IO = EDI_XAVIS_REQ
EXCEPTIONS
OTHERS = 4.
*———————————————————-*
IF SY-SUBRC NE 0.
REGUH-EDIBN = ‘E’.
MOVE-CORRESPONDING REGUH TO: TAB_KEIN_AVIS, ERR_EDI.
APPEND: TAB_KEIN_AVIS, ERR_EDI.
*{ INSERT DS3K916005 1
ELSE.
CALL FUNCTION ‘Z_GVEUXXIN01_CHECK_BUKRS’
EXPORTING
variant = ‘Z_GVEU_BUKRS’
IMPORTING
gveu_bukrs = lt_bukrs
EXCEPTIONS
no_data_tvarvc = 1
OTHERS = 2.
IF sy-subrc = 0.
IF reguh-zbukr IN lt_bukrs.
SELECT SINGLE * FROM edidc
INTO ls_edidc
WHERE docnum = ls_edidc-docnum.
IF sy-subrc = 0.
IF ls_edidc-idoctp = ‘PEXR2002’.
* Process the IDOC, changes status from ’30’ to ’03’/AHMEDS
SUBMIT rseout00 WITH docnum-low = ls_edidc-docnum
WITH p_idoctp = ls_edidc-idoctp
WITH upddat-low = sy-datum AND RETURN.
IF sy-subrc = 0.
* Set in FM ‘Z_EDI_PATH_CREATE_PARTNER_DTTM’ (See partner-profile)
GET PARAMETER ID ‘BOF’ FIELD lv_pathname.
IF sy-subrc = 0.
OPEN DATASET lv_pathname FOR INPUT IN TEXT MODE
ENCODING DEFAULT.
* Read Idoc File from A/S & make email attachement (The Idoc File)
IF sy-subrc = 0.
DO.
READ DATASET lv_pathname INTO l_wa_file.
IF sy-subrc <> 0.
EXIT.
ENDIF.
MOVE l_wa_file TO it_attach-line.
APPEND it_attach. CLEAR l_wa_file.
ENDDO.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
* Email message (body)
* Convert payment run date into user format
WRITE zw_laufd TO lv_date.
IF it_attach IS NOT INITIAL.
CONCATENATE text-t01
lv_date
‘& ID’
zw_laufi
tab_message
INTO tab_message SEPARATED BY space.
ELSE. “Attch. empty
tab_message = ‘No data found’.
ENDIF.
APPEND tab_message.
* Create Subject Title for email
CONCATENATE ‘IDOC file for’
lv_date
‘&’
zw_laufi
INTO lv_mtitle SEPARATED BY space.
* Send file by email
PERFORM send_file_as_email_attachment
TABLES tab_message
it_attach
USING s_email[]
lv_mtitle
‘txt’
‘BOA EDI File’
‘BOA EDI File’
‘ ‘
‘ ‘
CHANGING gd_error.
ELSE.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF.
*} INSERT
FORM
send_file_as_email_attachment
TABLES pit_message
pit_attach
USING p_email TYPE any “#EC *
p_mtitle TYPE any
p_format TYPE any
p_filename TYPE any
p_attdescription TYPE any
p_sender_address TYPE any
p_sender_addres_type TYPE any
CHANGING p_error TYPE any.”#EC *
* DATA Declarations
DATA: ld_mtitle LIKE sodocchgi1-obj_descr,
ld_email LIKE somlreci1-receiver, “#EC *
ld_format TYPE so_obj_tp ,
ld_attdescription TYPE so_obj_nam ,
ld_attfilename TYPE so_obj_des ,
ld_sender_address LIKE soextreci1-receiver,
ld_sender_address_type LIKE soextreci1-adr_typ.
DATA lwa_email LIKE s_email.
ld_mtitle = p_mtitle.
ld_format = p_format.
ld_attdescription = p_attdescription.
ld_attfilename = p_filename.
ld_sender_address = p_sender_address.
ld_sender_address_type = p_sender_addres_type.
* Fill the document data.
w_doc_data-doc_size = 1.
* Populate the subject/generic message attributes
w_doc_data-obj_langu = sy-langu.
w_doc_data-obj_name = ‘SAPRPT’.
w_doc_data-obj_descr = ld_mtitle .
w_doc_data-sensitivty = k_f.
* Fill the document data and get size of attachment
CLEAR w_doc_data.
READ TABLE it_attach INDEX w_cnt.
w_doc_data-doc_size =
( w_cnt – 1 ) * 255 + STRLEN( it_attach ).
w_doc_data-obj_langu = sy-langu.
w_doc_data-obj_name = ‘SAPRPT’.
w_doc_data-obj_descr = ld_mtitle.
w_doc_data-sensitivty = k_f.
CLEAR tab_attachment.
REFRESH tab_attachment.
tab_attachment[] = pit_attach[].
* Describe the body of the message
CLEAR tab_packing_list.
REFRESH tab_packing_list.
tab_packing_list-transf_bin = space.
tab_packing_list-head_start = 1.
tab_packing_list-head_num = 0.
tab_packing_list-body_start = 1.
DESCRIBE TABLE tab_message LINES tab_packing_list-body_num.
tab_packing_list-doc_type = ‘RAW’.
APPEND tab_packing_list.
* Create attachment notification
tab_packing_list-transf_bin = k_x.
tab_packing_list-head_start = 1.
tab_packing_list-head_num = 0.
tab_packing_list-body_start = 1.
DESCRIBE TABLE tab_attachment LINES tab_packing_list-body_num.
tab_packing_list-doc_type = ‘RAW’.
tab_packing_list-obj_descr = ld_attdescription.
tab_packing_list-obj_name = ld_attfilename.
tab_packing_list-doc_size = tab_packing_list-body_num * 255.
APPEND tab_packing_list.
* Add the recipients email address
CLEAR tab_receivers.
REFRESH tab_receivers.
*&—Create recipient with e-mail addresses of TO owner/approver
* Get the user email id.
IF not s_mail[] is initial.
CLEAR : tab_receivers.
LOOP AT s_mail.
tab_receivers-receiver = s_mail-low.
tab_receivers-rec_type = k_u.
tab_receivers-com_type = k_int.
tab_receivers-notif_del = k_x.
tab_receivers-notif_ndel = k_x.
APPEND tab_receivers.
clear : tab_receivers,
s_mail.
ENDLOOP.
IF NOT tab_receivers[] IS INITIAL.
CALL FUNCTION ‘SO_DOCUMENT_SEND_API1’
EXPORTING
document_data = w_doc_data
put_in_outbox = ‘X’
sender_address = ld_sender_address
sender_address_type = ld_sender_address_type
commit_work = ‘X’
IMPORTING
sent_to_all = w_sent_all
TABLES
packing_list = tab_packing_list
contents_bin = tab_attachment
contents_txt = tab_message
receivers = tab_receivers
EXCEPTIONS
too_many_receivers = 1
document_not_sent = 2
document_type_not_exist = 3
operation_no_authorization = 4
parameter_error = 5
x_error = 6
enqueue_error = 7
OTHERS = 8.
CASE sy-subrc.
WHEN 1.
ENDCASE.
ENDIF.
ENDIF.
ENDFORM. “send_file_as_email_attachment
New NetWeaver Information at SAP.com
Very Helpfull