Creating SAP shortcut at run time The following function module can be used to create a shortcut for any SAP transaction. Further, certain values available in the transaction can be defaulted by passing the values as parameters to this FM. This shortcut created can then be attached in a mail and sent to the appropriate recipients. *************************************************************** * Author: Prasath Natesan * * Source: SDN * *************************************************************** FUNCTION zfm_create_shortcut. *"--------------------------------------------------------------------- *"*"Local Interface: *" IMPORTING *" REFERENCE(RECIPIENT_USER_ID) TYPE SYUNAME *" REFERENCE(TRANSACTION) TYPE TCODE *" EXPORTING *" REFERENCE(CONTENT) TYPE STRING *" TABLES *" SHORTCUT_PARAM STRUCTURE ZST_SHORTCUT_PAR OPTIONAL *"--------------------------------------------------------------------- *** Declaration for shortcut content DATA : parameter TYPE text255, v_pernr(12) TYPE c. DATA : v_tcode TYPE tcode. * Check if transaction code is available CLEAR v_tcode. SELECT SINGLE tcode FROM tstc INTO v_tcode WHERE tcode EQ transaction. IF v_tcode IS INITIAL. MESSAGE 'Enter a valid transaction' TYPE 'E' DISPLAY LIKE 'A'. EXIT. ENDIF. * Populate the parameters to be passed to the shortcut IF NOT shortcut_param[] IS INITIAL. CLEAR parameter. LOOP AT shortcut_param. CONCATENATE parameter shortcut_param-fieldname '=' shortcut_param-fieldvalue ';' INTO parameter. ENDLOOP. ENDIF. *** create the shortcut content for the required transaction CALL FUNCTION 'SWN_CREATE_SHORTCUT' EXPORTING i_transaction = transaction i_parameter = parameter i_sysid = sy-sysid i_client = sy-mandt i_user = recipient_user_id i_language = sy-langu i_windowsize = 'Normal window' IMPORTING shortcut_string = content EXCEPTIONS inconsistent_parameters = 1 OTHERS = 2. IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ENDFUNCTION. | This FM receives RECIPIENT_USER_ID and TRANSACTION as import parameters. The value of parameters if any can be passed through table parameter SHORCTCUT_PARAM. On execution the shortcut content is created and returned through the export parameter "CONTENT". The table SHORTCUT_PARAM refers to a custom structure described in Appendix 1. Example scenario Consider a scenario where the employee has requested for a change in address. Now once the change in address is completed a notification email is sent to the employee indicating the successful change in address. In a normal scenario the employee needs to log into the system manually and enter the required transaction. Then the required details (employee number, infotype and subtype) need to be entered before displaying the updated information. This process can be simplified by sending a shortcut which will navigate the user to the required transaction with all required data pre-loaded. The sample code for this process is given below, *&---------------------------------------------------------------------* *& Report ZRP_MAIL_SHORTCUT *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT zrp_mail_shortcut. ************************************************************************ *** Report to send mail to employee to display temp address *** ************************************************************************ *** Declarations for attachment creation DATA: doc_chng LIKE sodocchgi1. DATA: tab_lines LIKE sy-tabix, body_start LIKE sy-tabix. DATA: it_objtxt LIKE solisti1 OCCURS 0 WITH HEADER LINE. DATA: it_objpack LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE. DATA: it_objbin LIKE solisti1 OCCURS 10 WITH HEADER LINE. DATA: it_reclist LIKE somlreci1 OCCURS 5 WITH HEADER LINE. DATA: it_shortcut_param LIKE zst_shortcut_par OCCURS 0 WITH HEADER LINE. DATA: content TYPE string. *** Pass the required parameters and create the shortcut CLEAR it_shortcut_param. REFRESH it_shortcut_param. it_shortcut_param-fieldname = 'RP50G-PERNR'. it_shortcut_param-fieldvalue = '1001'. "Employee number APPEND it_shortcut_param. it_shortcut_param-fieldname = 'RP50G-CHOIC'. it_shortcut_param-fieldvalue = '0006'. " Address Infotype APPEND it_shortcut_param. it_shortcut_param-fieldname = 'RP50G-TIMR1'. it_shortcut_param-fieldvalue = 'X'. "Period selected as "Today" APPEND it_shortcut_param. it_shortcut_param-fieldname = 'RP50G-SUBTY'. it_shortcut_param-fieldvalue = '2'. "Temporary address subtype APPEND it_shortcut_param. CALL FUNCTION 'ZFM_CREATE_SHORTCUT' EXPORTING recipient_user_id = 'DEVHYD' transaction = 'PA20' IMPORTING content = content TABLES shortcut_param = it_shortcut_param. *** Mail Subject doc_chng-obj_descr = 'Employee address changed'. *** Mail Contents CONCATENATE ' The requested change has been made to your temporary address.' ' Please double click on the attachment and choose display to view the updated address' INTO it_objtxt. APPEND it_objtxt. *** Creation of the entry for the document DESCRIBE TABLE it_objtxt LINES tab_lines. CLEAR it_objpack-transf_bin. it_objpack-head_start = 1. it_objpack-head_num = 0. it_objpack-body_start = 1. it_objpack-body_num = tab_lines. it_objpack-doc_type = 'RAW'. APPEND it_objpack. *** Populate attachment content CLEAR : tab_lines, it_objbin. CONCATENATE content it_objbin-line INTO it_objbin-line. APPEND it_objbin. DESCRIBE TABLE it_objbin LINES tab_lines. *** Creation of the entry for the compressed attachment it_objpack-transf_bin = 'X'. "Will get content from content_bin it_objpack-head_start = 1. it_objpack-head_num = 1. it_objpack-body_start = 1. it_objpack-body_num = tab_lines. it_objpack-doc_type = 'EXT'. it_objpack-obj_name = 'SAPSHORTCUTMAIL'. it_objpack-obj_descr = 'DisplayAddress.SAP'. it_objpack-doc_size = tab_lines * 255. APPEND it_objpack. *** target recipent(s) CLEAR it_reclist. it_reclist-receiver = 'employeemailid@employeecompany.com'. it_reclist-rec_type = 'U'. APPEND it_reclist. *** Sending the document to recipients with the shortcut attachment CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1' EXPORTING document_data = doc_chng put_in_outbox = 'X' commit_work = 'X' TABLES packing_list = it_objpack contents_bin = it_objbin contents_txt = it_objtxt receivers = it_reclist EXCEPTIONS too_many_receivers = 1 document_not_sent = 2 operation_no_authorization = 4 OTHERS = 99.