0% found this document useful (0 votes)
13 views

METHOD Envia - Email.

This method sends an email using the BCS (Business Communication Services) API in ABAP. It takes data from a table, generates an Excel attachment, and constructs an email with the attachment. It handles errors that could occur during the email sending process.

Uploaded by

Tiago Henrique
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

METHOD Envia - Email.

This method sends an email using the BCS (Business Communication Services) API in ABAP. It takes data from a table, generates an Excel attachment, and constructs an email with the attachment. It handles errors that could occur during the email sending process.

Uploaded by

Tiago Henrique
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

METHOD envia_email.

DATA: lt_excel TYPE TABLE OF zst_tm_gestao_agenda_excel.


DATA: ls_excel TYPE zst_tm_gestao_agenda_excel.
DATA: lt_text TYPE bcsy_text.
DATA: lr_torstgseq TYPE RANGE OF char3.

IF i_job IS INITIAL.

lr_torstgseq = VALUE #( FOR ls_data IN i_data ( sign = 'I'


option = 'EQ'
low = ls_data-torstgseq ) ).
DELETE lr_torstgseq WHERE low = ''.

SELECT *
FROM zi_tm_ckp_gestao_agend_app
FOR ALL ENTRIES IN @i_data
WHERE vbeln = @i_data-vbeln
AND seq = @i_data-seq
AND ordem = @i_data-ordem
AND torstgseq IN @lr_torstgseq[]
INTO TABLE @DATA(lt_app).
* and tor = @i_data-tor_id.
* and torstgseq = @i_data-torstgseq.
ELSE.
lt_app = CORRESPONDING #( i_job ).
ENDIF.

IF lt_app[] IS NOT INITIAL.

* LOOP AT lt_app INTO DATA(ls_app).


* DATA(lv_tabix) = sy-tabix.
* READ TABLE i_data TRANSPORTING NO FIELDS WITH KEY vbeln = ls_app-vbeln
* seq = ls_app-seq.
** ordem = ls_app-ordem
** tor_id = ls_app-tor.
* IF sy-subrc IS NOT INITIAL.
** DELETE lt_app FROM lv_tabix.
* DELETE lt_app INDEX lv_tabix.
* ELSE.
* ls_excel = CORRESPONDING #( ls_app ).
*
* CALL FUNCTION 'CONVERSION_EXIT_CGCBR_OUTPUT'
* EXPORTING
* input = ls_excel-cgc
* IMPORTING
* output = ls_excel-cgc.
*
* APPEND ls_excel TO lt_excel.
* ENDIF.
* ENDLOOP.

IF i_job IS NOT INITIAL.


LOOP AT lt_app INTO DATA(ls_app).
ls_excel = CORRESPONDING #( ls_app ).

SHIFT ls_excel-vbeln LEFT DELETING LEADING '0'.


SHIFT ls_excel-ordem LEFT DELETING LEADING '0'.
SHIFT ls_excel-nfenum LEFT DELETING LEADING '0'.
SHIFT ls_excel-kunnr LEFT DELETING LEADING '0'.
SHIFT ls_excel-bstkd LEFT DELETING LEADING '0'.
SHIFT ls_excel-fu LEFT DELETING LEADING '0'.
SHIFT ls_excel-tor_id LEFT DELETING LEADING '0'.
SHIFT ls_excel-tspid LEFT DELETING LEADING '0'.
SHIFT ls_excel-btd_id LEFT DELETING LEADING '0'.

APPEND ls_excel TO lt_excel.


ENDLOOP.
ENDIF.

CHECK lt_app[] IS NOT INITIAL.

DATA(lv_recipient) = i_email.

IF i_job IS NOT INITIAL.


GET REFERENCE OF lt_excel INTO DATA(lo_data_ref).
DATA(lv_xstring) = gera_excel( lo_data_ref ).
ENDIF.

*--- Email code starts here


TRY.
"Create send request
DATA(lo_send_request) = cl_bcs=>create_persistent( ).

"Create mail body


IF i_job IS INITIAL.
DATA(lt_body) = get_template_email( EXPORTING i_data = lt_app
i_template =
i_template ).
ENDIF.

"Set up document object


DATA(lo_document) = cl_document_bcs=>create_document(
i_type = 'HTM'
* i_text = lt_body
i_subject = COND #( WHEN i_template =
gc_inseriragenda
THEN TEXT-001
WHEN i_template =
gc_solicitaragenda
THEN TEXT-002
WHEN i_template = gc_reagendar
THEN TEXT-003
WHEN i_template =
gc_excluiragenda
THEN TEXT-004
) ).
IF i_job IS NOT INITIAL.
"Add attachment
lo_document->add_attachment(
i_attachment_type = 'xls'
i_attachment_size = CONV #( xstrlen( lv_xstring ) )
i_attachment_subject = TEXT-005
i_attachment_header = VALUE #( ( line = TEXT-006 ) )
i_att_content_hex = cl_bcs_convert=>xstring_to_solix( lv_xstring
)
).
ENDIF.

"Add document to send request


lo_send_request->set_document( lo_document ).

"Set sender
lo_send_request->set_sender(
cl_cam_address_bcs=>create_internet_address(
i_address_string = CONV #( TEXT-007 )
)
).

SPLIT lv_recipient AT ';' INTO TABLE DATA(lt_recipient).

LOOP AT lt_recipient INTO DATA(ls_recipient).


"Set Recipient | This method has options to set CC/BCC as well
lo_send_request->add_recipient(
i_recipient = cl_cam_address_bcs=>create_internet_address(
i_address_string = CONV #( ls_recipient )
)
i_express = abap_true ).

ENDLOOP.

"Send Email
DATA(lv_sent_to_all) = lo_send_request->send( ).
* COMMIT WORK.

CATCH cx_send_req_bcs INTO DATA(lx_req_bsc).


"Error handling
CATCH cx_document_bcs INTO DATA(lx_doc_bcs).
"Error handling
CATCH cx_address_bcs INTO DATA(lx_add_bcs).
"Error handling
ENDTRY.

ENDIF.

ENDMETHOD.

You might also like