You are on page 1of 6

Enviar link de acesso do SAP por e-mail

Índice
Solução __________________________________ ________________________ 02
Telas ____________________________________ ________________________ 02
Programa _________________________________ ________________________ 03
Solução
 Neste exemplo o link de acesso ao SAP por e-mail é disparado por um programa Z. Este programa recebe
as informações na tela, monta um e-mail com a notificação e anexa um link (atalho) para acesso de um
determinado usuário ao SAP. Caso o usuário já esteja logado na mesma máquina, o link com a transação é
executado e a transação é aberta automaticamente numa janela nova.
 Esse programa pode ser ajustado facilmente para uma função ou para um programa include onde é feita
a chamada do FORM (rotina) que excuta a tarefa de envio de e-mail.

Telas
Programa
*&---------------------------------------------------------------------*
*& Report ZNIXX02
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT znixx04.

* Envia_Link_SAP-Constantes-Início
CONSTANTS: c_charx(1) TYPE C VALUE 'X',
c_charu(1) TYPE C VALUE 'U',
c_yes(3) TYPE C VALUE 'Yes', "#EC NEEDED
c_no(2) TYPE C VALUE 'No'. "#EC NEEDED
* Envia_Link_SAP-Constantes-Fim

* Envia_Link_SAP-Variáveis-Início
DATA: gv_zowner TYPE syuname, "Login user id of SAP Shortcut
* Comment data comes from function module CATSXT_SIMPLE_TEXT_EDITOR
wa_comments TYPE LINE OF catsxt_longtext_itab,
it_comments TYPE catsxt_longtext_itab.
* Envia_Link_SAP-Variáveis-Fim

* Envia_Link_SAP-Workareas-Início
*-- Work area for all the internal tables used for Sending Mail
DATA: wa_packing_list TYPE sopcklsti1,
wa_contents_txt TYPE solisti1,
wa_reclist TYPE somlreci1.
* Envia_Link_SAP-Workareas-Fim

* Envia_Link_SAP-Variáveis do e-mail-Início
*-- Variables used for Sending Mail
DATA: gv_smtp_addr TYPE adr6-smtp_addr,
sender TYPE soextreci1-RECEIVER,
send_adr_type TYPE so_adr_typ.

DATA: gv_count TYPE sy-INDEX,


gv_line_no TYPE sy-INDEX. "#EC NEEDED

*-- MAIL related Internal tables


DATA: wa_doc_chng TYPE sodocchgi1, " document
*attributes
it_packing_list TYPE STANDARD TABLE OF sopcklsti1, " attachment
*table
it_contents_txt TYPE STANDARD TABLE OF solisti1, " object text
it_reclist TYPE STANDARD TABLE OF somlreci1. " mail
*recipients
* Envia_Link_SAP-Variáveis do e-mail-Fim

* Envia_Link_SAP-Dados do Atalho-Início
* For GUI shortcut attachment
DATA: it_shortcut TYPE soli_tab.
* Envia_Link_SAP-Dados do Atalho-Fim

parameters:
usuario type sy-uname memory id zxn1,
e-mail type c length 60 memory id zxn2,
trans type sy-tcode memory id zxn3,
titulo type c length 60 memory id zxn4.

start-of-selection.
perform f_envia_link_sap using usuario
e-mail
trans
titulo.

*&---------------------------------------------------------------------*
*& Form F_ENVIA_LINK_SAP
*&---------------------------------------------------------------------*
* Envia e-mail com link SAP
*----------------------------------------------------------------------*
* --> P_USUARIO
* --> P_E_MAIL
* --> P_TRANS
* --> P_TITULO
*----------------------------------------------------------------------*
form F_ENVIA_LINK_SAP using p_usuario
p_e_mail
p_trans
p_titulo.

* Initialize some data


gv_zowner = p_usuario.
gv_smtp_addr = p_e_mail.

sender = 'jnilson.oliveira@finitycons.com'.
send_adr_type = 'SMTP'.

**Put the Email ID of approver in list of Recepients of Notification


*Email.
* Get the mail Id for Purchase Group Owner
CLEAR: wa_reclist, it_reclist.

*-Populate Mail Id's


wa_reclist-rec_type = c_charu. "U = Internet address
wa_reclist-express = c_charx. "Send express "X = Value for activated
wa_reclist-RECEIVER = gv_smtp_addr.
APPEND wa_reclist TO it_reclist.

CLEAR: gv_smtp_addr, wa_reclist.

*-If receiver table is not initial


CHECK it_reclist[] IS NOT INITIAL.
*--To check Mail ID's internal table should not be Empty.
*--populate document attributes

* Mail subject line


CLEAR: wa_doc_chng.
wa_doc_chng-obj_name = 'HEADING'.
wa_doc_chng-obj_descr = p_titulo.

* Mail body
wa_comments = 'Requisição 80000000001'.
APPEND wa_comments TO it_comments.

wa_comments = 'Requisição 80000000002'.


APPEND wa_comments TO it_comments.

wa_comments = 'Requisição 80000000003'.


APPEND wa_comments TO it_comments.

wa_comments = 'Requisição 80000000004'.


APPEND wa_comments TO it_comments.

IF it_comments IS NOT INITIAL.


CLEAR: wa_contents_txt, it_contents_txt.
* wa_contents_txt = 'Comments from Requestor:'(029).
wa_contents_txt = 'Detalhamento:'(029).
APPEND wa_contents_txt TO it_contents_txt.
* APPEND INITIAL LINE TO it_contents_txt.
wa_contents_txt = '------------------------'(029).
APPEND wa_contents_txt TO it_contents_txt.

LOOP AT it_comments INTO wa_comments.


wa_contents_txt = wa_comments.
APPEND wa_contents_txt TO it_contents_txt.
ENDLOOP.
APPEND INITIAL LINE TO it_contents_txt.
ENDIF.

* Mensagem final (para não mresponder esse e-mail)


wa_contents_txt =
'Mensagem gerada pelo sistema. Não responder.'(030).

APPEND wa_contents_txt TO it_contents_txt.


APPEND INITIAL LINE TO it_contents_txt.

* Document size
CLEAR: gv_line_no, gv_count.

DESCRIBE TABLE it_contents_txt LINES gv_count.


gv_line_no = gv_count.
READ TABLE it_contents_txt INTO wa_contents_txt INDEX gv_count.
wa_doc_chng-doc_size = ( gv_count - 1 ) *
255 +
STRLEN( wa_contents_txt ).

* Populate packing list for body text


wa_packing_list-head_start = 1.
wa_packing_list-head_num = 0.
wa_packing_list-body_start = 1.
wa_packing_list-body_num = gv_count."v_table_lines.
wa_packing_list-doc_type = 'RAW'.
APPEND wa_packing_list TO it_packing_list.
CLEAR wa_packing_list.

*--Create Shortcut to be added to mail,Clicking which user can


*--Login into SAP

CLEAR: it_shortcut.

CALL FUNCTION 'SWN_CREATE_SHORTCUT'


EXPORTING
i_transaction = trans "TO take user to Inbox directly
i_sysid = sy-sysid
i_client = sy-mandt
i_user = gv_zowner
i_language = sy-langu
i_windowsize = 'Normal Window' "#EC NOTEXT
IMPORTING
shortcut_table = it_shortcut "Shortcut(link) created
EXCEPTIONS
inconsistent_parameters = 1
OTHERS = 2.

IF sy-subrc EQ SPACE.
APPEND LINES OF it_shortcut TO it_contents_txt.
ENDIF.

DESCRIBE TABLE it_shortcut LINES gv_count.


CLEAR it_shortcut.

wa_packing_list-head_start = 1.
wa_packing_list-head_num = 1.
wa_packing_list-body_start = gv_line_no + 1 .
wa_packing_list-body_num = gv_count.
wa_packing_list-doc_type = 'SAP'.
wa_packing_list-doc_size = 255 * gv_count.
wa_packing_list-obj_name = 'ATTACHMENT'.
wa_packing_list-obj_descr =
'Link para aprovação'(031). "Name Of the link appearing
wa_packing_list-obj_langu = sy-langu.
APPEND wa_packing_list TO it_packing_list.
CLEAR wa_packing_list.
* Sending the EMail document in given format
CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
EXPORTING
document_data = wa_doc_chng
put_in_outbox = c_charx "'X'
sender_address = sender
sender_address_type = send_adr_type
commit_work = c_charx "'X'
TABLES
packing_list = it_packing_list
contents_txt = it_contents_txt
receivers = it_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.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
message s398(00) with 'Notificação enviada com sucesso'
'' '' ''.
ENDIF.

endform. " F_ENVIA_LINK_SAP