How to send mail with attachment of SAP Script in Workflow
How to send mail with attachment of SAP Script in Workflow: Once the release of the PO is completed ( after released all level ) the script output of the particular PO has to send the initiator as attachment.
Step 1 : Create step ( activity ) in workflow for getting the spool request of the release PO .
The code for getting the spool request is as follows
*fetching the spool request.
select single rqident
from TSP01
into l_rqident
where rqtitle = object-key-purchaseorder.
if sy-subrc = 0.
spool_generate = ‘X’.
endif.
SWC_SET_ELEMENT CONTAINER ‘spool_generate’ SPOOL_GENERATE.
Step 2 : As the spool request generation will take some time , put the wait step with condition that until spool is getting generated ( &spool_generate& = ‘X’. )
Step 3 : Create step ( activity ) for attaching the PO.
The code is as below
BEGIN_METHOD POMAIL_ATTCHMNT CHANGING CONTAINER.
DATA:
MAIL_ID TYPE ADR6-SMTP_ADDR,
PO_NUMBER TYPE EKKO-EBELN.
SWC_GET_ELEMENT CONTAINER ‘mail_id’ MAIL_ID.
SWC_GET_ELEMENT CONTAINER ‘PO_Number’ PO_NUMBER.
” calling FM for Mail attachment .
CALL FUNCTION ‘ZMM_PO_EMAIL_ATTACHMENT’
EXPORTING
I_EBELN = PO_NUMBER ” passing the PO
I_EMAIL = MAIL_ID. ” passing the mail id of initiator.
END_METHOD.
In the above function module , I am writing the code for getting PDF output from the spool number and make the bin file and passing the same to the SO_NEW_DOCUMENT_ATT_SEND_API1. and sending the attachment with the notification. Find the detailed code below.
“————————————————————
” Constants Declarations
“————————————————————
CONSTANTS: lc_pdf TYPE char3 VALUE ‘PDF’,
lc_obj_name TYPE so_obj_nam VALUE ‘SCRIPT’,
lc_express TYPE char1 VALUE ‘X’,
lc_rec_type TYPE char1 VALUE ‘U’,
lc_sensitivty TYPE so_obj_sns VALUE ‘F’,
lc_doc_type TYPE so_obj_tp VALUE ‘RAW’,
lc_in_out TYPE sonv–flag VALUE ‘X’,
lc_no(1) TYPE c VALUE ‘ ‘,
lc_device(4) TYPE c VALUE ‘LOCL’.
“————————————————————
” Internal Tables Declarations
“————————————————————
DATA : lint_objpack TYPE TABLE OF sopcklsti1,
lint_objbin TYPE TABLE OF solisti1,
lint_objtxt TYPE TABLE OF solisti1,
lint_reclist TYPE TABLE OF somlreci1,
lint_pdf_output TYPE TABLE OF tline,
lint_mess_att TYPE TABLE OF solisti1.
“————————————————————
” Work Area Declarations
“————————————————————
DATA : lfs_objpack TYPE sopcklsti1,
lfs_objbin TYPE solisti1,
lfs_objtxt TYPE solisti1,
lfs_reclist TYPE somlreci1,
lfs_doc_chng TYPE sodocchgi1,
lfs_tsp01 TYPE tsp01,
lfs_pdf_output TYPE tline,
lfs_mess_att TYPE solisti1.
“————————————————————
” Local Varaibles Declarations
“————————————————————
DATA : lwf_lines_txt TYPE i,
lwf_lines_bin TYPE i,
lwf_buffer TYPE string.
“————————————————————
” Logic started..
“————————————————————
IF i_ebeln IS NOT INITIAL
AND i_email IS NOT INITIAL.
** To get the spool no from the purchase document number..
SELECT SINGLE *
FROM tsp01
INTO lfs_tsp01
WHERE rqtitle = i_ebeln.
IF sy–subrc = 0.
** To set the local language..
SET LOCALE LANGUAGE sy–langu.
** To get the PDF output from the spool no..
CALL FUNCTION ‘CONVERT_OTFSPOOLJOB_2_PDF’
EXPORTING
src_spoolid = lfs_tsp01–rqident
no_dialog = lc_no
dst_device = lc_device
TABLES
pdf = lint_pdf_output
EXCEPTIONS
err_no_otf_spooljob = 1
err_no_spooljob = 2
err_no_permission = 3
err_conv_not_possible = 4
err_bad_dstdevice = 5
user_cancelled = 6
err_spoolerror = 7
err_temseerror = 8
err_btcjob_open_failed = 9
err_btcjob_submit_failed = 10
err_btcjob_close_failed = 11
OTHERS = 12.
IF sy–subrc = 0.
“————————————————————
” Transfer the 132-long strings to 255-long strings
“————————————————————
LOOP AT lint_pdf_output INTO lfs_pdf_output.
TRANSLATE lfs_pdf_output USING ‘ ~’.
CONCATENATE lwf_buffer lfs_pdf_output INTO lwf_buffer.
ENDLOOP.
**
TRANSLATE lwf_buffer USING ‘~ ‘.
DO.
lfs_mess_att = lwf_buffer.
APPEND lfs_mess_att TO lint_mess_att.
SHIFT lwf_buffer LEFT BY 255 PLACES.
IF lwf_buffer IS INITIAL.
EXIT.
ENDIF.
ENDDO.
ENDIF.
“————————————————————
” To add the recipients email address
“————————————————————
lfs_reclist–receiver = i_email.
lfs_reclist–express = lc_express.
lfs_reclist–rec_type = lc_rec_type.
APPEND lfs_reclist TO lint_reclist.
CLEAR lfs_reclist.
“————————————————————
” Mail Content
“————————————————————
** for Header
lfs_objtxt = ‘Dear User,’.
APPEND lfs_objtxt TO lint_objtxt.
CLEAR lfs_objtxt.
** For space
APPEND lfs_objtxt TO lint_objtxt.
CLEAR lfs_objtxt.
** For content
CONCATENATE ‘Purchase order : ‘ i_ebeln ‘, has been approved.’ INTO lfs_objtxt.
APPEND lfs_objtxt TO lint_objtxt.
CLEAR lfs_objtxt.
** For space
APPEND lfs_objtxt TO lint_objtxt.
CLEAR lfs_objtxt.
** For footer
lfs_objtxt = ‘This is an auto generated email, please do not reply.’.
APPEND lfs_objtxt TO lint_objtxt.
CLEAR lfs_objtxt.
“————————————————————
” Mail Header
“————————————————————
DESCRIBE TABLE lint_objtxt LINES lwf_lines_txt.
lfs_doc_chng–obj_name = lc_obj_name.
lfs_doc_chng–obj_langu = sy–langu.
CONCATENATE ‘Purchase order: ‘ i_ebeln ‘, has been approved’ INTO lfs_doc_chng–obj_descr.
lfs_doc_chng–sensitivty = lc_sensitivty.
lfs_doc_chng–doc_size = 1.
“————————————————————
” Pack to main body as RAW.
“————————————————————
CLEAR lfs_objpack–transf_bin.
lfs_objpack–head_start = 1.
lfs_objpack–head_num = 0.
lfs_objpack–body_start = 1.
lfs_objpack–body_num = lwf_lines_txt.
lfs_objpack–doc_type = lc_doc_type.
APPEND lfs_objpack TO lint_objpack.
“————————————————————
” Packing as PDF.
“————————————————————
CLEAR lwf_lines_bin.
DESCRIBE TABLE lint_mess_att LINES lwf_lines_bin.
lfs_objpack–transf_bin = abap_true.
lfs_objpack–head_start = 1.
lfs_objpack–head_num = 1.
lfs_objpack–body_start = 1.
lfs_objpack–body_num = lwf_lines_bin.
lfs_objpack–doc_type = lc_pdf.
CONCATENATE ‘Purchase order: ‘ i_ebeln ‘, has been approved’ INTO lfs_objpack–obj_name.
CONCATENATE ‘Purchase order’ ‘:’ i_ebeln INTO lfs_objpack–obj_descr.
**
lfs_objpack–doc_size = lwf_lines_bin * 255.
APPEND lfs_objpack TO lint_objpack.
“————————————————————
” To send the smartform as attachment in Mail to the above receipent
“————————————————————
CALL FUNCTION ‘SO_NEW_DOCUMENT_ATT_SEND_API1’
EXPORTING
document_data = lfs_doc_chng
put_in_outbox = lc_in_out
TABLES
packing_list = lint_objpack
contents_bin = lint_mess_att
contents_txt = lint_objtxt
receivers = lint_reclist
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.
IF sy–subrc = 0.
COMMIT WORK.
ENDIF.
ENDIF.
ENDIF.
ENDFUNCTION.
New NetWeaver Information at SAP.com
Very Helpfull