*----------------------------------------------------------------------*
* Report BCS_EXAMPLE_6
*----------------------------------------------------------------------*
* Email documents using PDF based forms
*----------------------------------------------------------------------*
REPORT bcs_example_6.
DATA: carr_id TYPE sbook-carrid.
PARAMETER: p_custid TYPE scustom-id DEFAULT 12.
SELECT-OPTIONS: s_carrid FOR carr_id DEFAULT 'AA' TO 'ZZ'.
PARAMETER: p_email TYPE adr6-smtp_addr OBLIGATORY.
DATA: customer TYPE scustom,
bookings TYPE ty_bookings,
connections TYPE ty_connections,
fm_name TYPE rs38l_fnam,
fp_docparams TYPE sfpdocparams,
fp_outputparams TYPE sfpoutputparams,
error_string TYPE string.
DATA ls_formoutput TYPE fpformoutput.
DATA lx_fp_api TYPE REF TO cx_fp_api.
data lp_form TYPE tdsfname value 'FP_TEST_03'.
data lp_langu TYPE langu value 'D'.
data lp_countr TYPE land1 value 'DE'.
* BCS data
DATA send_request TYPE REF TO cl_bcs.
DATA text TYPE bcsy_text.
DATA document TYPE REF TO cl_document_bcs.
DATA recipient TYPE REF TO if_recipient_bcs.
DATA: bcs_exception TYPE REF TO cx_bcs.
DATA sent_to_all TYPE os_boolean.
DATA pdf_content TYPE solix_tab.
DATA lp_pdf_size TYPE so_obj_len.
* get data
SELECT SINGLE * FROM scustom INTO customer WHERE id = p_custid.
CHECK sy-subrc = 0.
SELECT * FROM sbook INTO TABLE bookings
WHERE customid = p_custid
AND carrid IN s_carrid
ORDER BY PRIMARY KEY.
if sy-subrc = 0.
SELECT * FROM spfli INTO TABLE connections
FOR ALL ENTRIES IN bookings
WHERE carrid = bookings-carrid
AND connid = bookings-connid
ORDER BY PRIMARY KEY.
endif.
* Print data:
* First get name of the generated function module
TRY.
CALL FUNCTION 'FP_FUNCTION_MODULE_NAME'
EXPORTING
i_name = lp_form
IMPORTING
e_funcname = fm_name.
CATCH cx_fp_api INTO lx_fp_api.
* exception handling
MESSAGE ID lx_fp_api->msgid TYPE lx_fp_api->msgty
NUMBER lx_fp_api->msgno
WITH lx_fp_api->msgv1 lx_fp_api->msgv2
lx_fp_api->msgv3 lx_fp_api->msgv4.
EXIT.
ENDTRY.
* Set output parameters and open spool job
fp_outputparams-nodialog = 'X'. " no print preview
fp_outputparams-getpdf = 'X'. " request PDF
CALL FUNCTION 'FP_JOB_OPEN'
CHANGING
ie_outputparams = fp_outputparams
EXCEPTIONS
cancel = 1
usage_error = 2
system_error = 3
internal_error = 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.
* Set form language and country (->form locale)
fp_docparams-langu = lp_langu.
fp_docparams-country = lp_countr.
* Now call the generated function module
CALL FUNCTION fm_name
EXPORTING
/1bcdwb/docparams = fp_docparams
customer = customer
bookings = bookings
connections = connections
IMPORTING
/1bcdwb/formoutput = ls_formoutput
EXCEPTIONS
usage_error = 1
system_error = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
CALL FUNCTION 'FP_GET_LAST_ADS_ERRSTR'
IMPORTING
e_adserrstr = error_string.
IF NOT error_string IS INITIAL.
* we received a detailed error description
WRITE:/ error_string.
EXIT.
ELSE.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF.
* Close spool job
CALL FUNCTION 'FP_JOB_CLOSE'
* IMPORTING
* E_RESULT =
EXCEPTIONS
usage_error = 1
system_error = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
* ------------ Call BCS interface ----------------------------------
TRY.
* ---------- create persistent send request ----------------------
send_request = cl_bcs=>create_persistent( ).
* ---------- add document ----------------------------------------
* get PDF xstring and convert it to BCS format
lp_pdf_size = XSTRLEN( ls_formoutput-pdf ).
pdf_content = cl_document_bcs=>xstring_to_solix(
ip_xstring = ls_formoutput-pdf ).
document = cl_document_bcs=>create_document(
i_type = 'PDF'
i_hex = pdf_content
i_length = lp_pdf_size
i_subject = 'test created by BCS_EXAMPLE_6' ). "#EC NOTEXT
* add document to send request
send_request->set_document( document ).
* ---------- add recipient (e-mail address) ----------------------
recipient = cl_cam_address_bcs=>create_internet_address(
i_address_string = p_email ).
* add recipient to send request
send_request->add_recipient( i_recipient = recipient ).
* ---------- send document ---------------------------------------
sent_to_all = send_request->send(
i_with_error_screen = 'X' ).
IF sent_to_all = 'X'.
message i022(so).
ENDIF.
* ---------- explicit 'commit work' is mandatory! ----------------
COMMIT WORK.
* ------------------------------------------------------------------
* * exception handling
* ------------------------------------------------------------------
* * replace this very rudimentary exception handling
* * with your own one !!!
* ------------------------------------------------------------------
CATCH cx_bcs INTO bcs_exception.
WRITE: text-001.
WRITE: text-002, bcs_exception->error_type.
EXIT.
ENDTRY.
如何将打印报表作为PDF附件发送的代码样例
最新推荐文章于 2024-10-23 15:05:53 发布