Menu

How to Attach Executable SAP Transaction Links to E-Mails

2014-08-12       

Today, I’ll be looking at e-mail sending features in SAP ERP for the last time and demonstrate how you can attach executable transaction links to e-mails sent from SAP. This is a very useful feature that is often used in a business workflow environment.

Let’s take a look at the possibilities we have. There are two ways to send executable links to receivers in SAP. One is easy and only works if you’re using SAPOffice (transaction SBWP), the other is a bit harder to implement, but more flexible; it will work with both SAPOffice and external e-mail, which is more common.

Sending executable SAPOffice messages

Let’s look at the simple way first. We can mark a SAPOffice document as executable by simply setting a few parameters in the document properties. Consider the e-mail program you already know from the last articles about e-mails. When working with the receiver type B, we can do the following:

* Attach a transaction
ls_document_data-proc_type = 'T'.
* Name the transaction
ls_document_data-proc_name = 'MM03'.
* Skip first screen
ls_document_data-skip_scren = 'X'.

If the recipient of this message now right-clicks it in his inbox, he can choose Execute to call the transaction given here. It is also possible to execute reports and other callable objects by setting the proc_type parameter to other values (see the documentation of SO_NEW_DOCUMENT_SEND_API1 for a list).

Of course, we want to pass the transaction a parameter since we already instructed it to skip the first screen. This is done by using the table parameter object_para.

* Set the parameters for the TA
ls_object_para-name = 'MAT'.
ls_object_para-low = 'MAT001'.
APPEND ls_object_para TO lt_object_para.

As you can see, I provided the name of a SPA/GPA parameter and a value for it. When the user now chooses Document > Execute in his SBWP inbox, the transaction MM03 will be opened with the material MAT001 already pre-selected. To send the document now, call the function module you already know.

CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
  EXPORTING
    document_data              = ls_document_data
    document_type              = 'RAW'
    put_in_outbox              = 'X'
    commit_work                = 'X'
  TABLES
    object_content             = lt_object_content
    object_para                = lt_object_para
    receivers                  = lt_receivers.

That’s it! Easy, but it has one massive caveat: it does not work for e-mail. The execute functionality is simply not available and unfortunately, the SAP system doesn’t convert it automatically to a SAP shortcut. So let’s look at how we make that work on the next page.

Sending executable transaction links in e-mails

So how do we make that work for e-mails? The only possibility we have is to attach an SAP shortcut to the e-mail. I already discussed how to send e-mails with attachments from SAP ERP, so I’m building on that knowledge here.

Assume we’ve already filled the body content of our e-mail table. What we need to do now is create the SAP shortcut (never heard of these? Check out this SCN wiki entry). Please don’t try to do that manually! There’s a much more convenient way, namely the function module  SWN_CREATE_SHORTCUT. It is used like this:

CALL FUNCTION 'SWN_CREATE_SHORTCUT'
  EXPORTING
    i_transaction           = 'IE03'
  IMPORTING
    shortcut_table          = lt_shortcut
  EXCEPTIONS
    inconsistent_parameters = 1
    OTHERS                  = 2.

You simply provide the name of the transaction you want to execute and the function module will output the constructed SAP shortcut in an internal table. You can provide many more parameters to specify user name, system to be used, et cetera.

After we’ve constructed the shortcut, all we need to do is move it to the e-mail content table. That’s easily done since both tables have the same structure:

 APPEND LINES OF lt_shortcut TO lt_objcont.

All that remains to do is to fill the packing list (check out the sample program on page 4 for the full code) and send off the e-mail by using  SO_NEW_DOCUMENT_ATT_SEND_API1. The document type that you have to provide in the packing list is “SAP”.

Adding parameters to SAP Shortcuts

So far, we’ve only created a shortcut that opens a transaction. That’s nice, but not very useful if you want to point your users towards a specific set of data. Adding parameters to an SAP shortcut is not that easy, but can be done with a little “hacking”.

Adding the parameter is relatively easy. Find out the batch input name of the screen fields you want to fill. In my example, I’ve used IE03. Press F1 to get the field help, then click “Technical Information” in the F1 help popup to get this screen.

Press F1, then choose “Technical Information” to get this popup.

You can find the needed field name at the bottom under “Field Description for Batch Input”. You can pass the parameter name and value as a text string in the i_parameter parameter of the function module  SWN_CREATE_SHORTCUT. There’s one additional trick: If you put an asterisk before the transaction name, the first screen will be skipped – by the way, this also works when you call a transaction directly in the SAPGui (you need to put an additional slash before the asterisk then). The final call looks like this:

CALL FUNCTION 'SWN_CREATE_SHORTCUT'
  EXPORTING
    i_transaction           = '*IE03'
    i_parameter             = 'RM63E-EQUNR=ABCDE123'
  IMPORTING
    shortcut_table          = lt_shortcut
  EXCEPTIONS
    inconsistent_parameters = 1
    OTHERS                  = 2.

This will open the transaction IE03 with the “Equipment” field filled with the value “ABCDE123” and skip the first screen. Proceed as discussed earlier to attach this shortcut to an e-mail and enjoy! The sample programs can be found on the next two pages.

Example program to send executable SAPOffice documents

REPORT ztestemail2.
 
* These are needed for the e-mail itself
DATA ls_object_content TYPE solisti1.
DATA lt_object_content TYPE STANDARD TABLE OF solisti1.
DATA ls_receivers TYPE somlreci1.
DATA lt_receivers TYPE STANDARD TABLE OF somlreci1.
DATA ls_document_data LIKE sodocchgi1.
 
* These are needed for the transaction attachment
DATA ls_object_para TYPE soparai1.
DATA lt_object_para TYPE STANDARD TABLE OF soparai1.
 
* Fill the document data
ls_document_data-obj_name = 'Test'.
ls_document_data-obj_descr = 'SAPOffice with executable TA'.
 
* Fill the mail content
ls_object_content-line = 'Test SAPOffice with transaction'.
APPEND ls_object_content TO lt_object_content.
 
* Fill the receiver list
ls_receivers-receiver = sy-uname.
ls_receivers-rec_type = 'B'.
APPEND ls_receivers TO lt_receivers.
 
* Attach a transaction
ls_document_data-proc_type = 'T'.
* Name the transaction
ls_document_data-proc_name = 'MM03'.
* Skip first screen
ls_document_data-skip_scren = 'X'.
 
* Set the parameter for the TA
ls_object_para-name = 'MAT'.
ls_object_para-low = 'MAT001'.
APPEND ls_object_para TO lt_object_para.
 
CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
  EXPORTING
    document_data              = ls_document_data
    document_type              = 'RAW'
    put_in_outbox              = 'X'
    commit_work                = 'X'
  TABLES
    object_content             = lt_object_content
    object_para                = lt_object_para
    receivers                  = lt_receivers
  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.
  WRITE 'Email could not be sent.'.
  WRITE sy-subrc.
ELSE.
  WRITE 'Email was sent!'.
ENDIF.

 

Example program to send executable e-mail attachments

REPORT ztestemailexec.
 
DATA lt_objcont TYPE STANDARD TABLE OF solisti1.
DATA lt_objcont_bin TYPE STANDARD TABLE OF solisti1.
DATA ls_objcont TYPE solisti1.
DATA lt_receivers TYPE STANDARD TABLE OF somlreci1.
DATA ls_receivers TYPE somlreci1.
DATA ls_document_data LIKE sodocchgi1.
 
DATA lv_body_length TYPE i.
DATA lv_attc_length TYPE i.
 
DATA lt_packing_list TYPE STANDARD TABLE OF sopcklsti1.
DATA ls_packing_list TYPE sopcklsti1.
 
* These are required to build the attachment
DATA lt_shortcut TYPE soli_tab.
DATA lv_param TYPE text255.
 
* Fill the document header
ls_document_data-obj_name = 'Test'.
ls_document_data-obj_descr = 'E-mail with executable Link'.
 
* Fill the mail content
ls_objcont-line = 'E-mail with executable attachment'.
APPEND ls_objcont TO lt_objcont.
 
* Email body is done - create the packing list entry
* E-mail body starts at first line
ls_packing_list-body_start = 1.
DESCRIBE TABLE lt_objcont LINES lv_body_length.
ls_packing_list-body_num = lv_body_length. * The doc type for the e-mail body now is defined here
ls_packing_list-doc_type = 'RAW'.
* The remaining fields stay empty for the first line
APPEND ls_packing_list TO lt_packing_list.
 
* Now we build the executable SAP Shortcut
CALL FUNCTION 'SWN_CREATE_SHORTCUT'
  EXPORTING
    i_transaction           = '*IE03'
    i_parameter             = 'RM63E-EQUNR=ABCDE123'
  IMPORTING
    shortcut_table          = lt_shortcut
  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.
 
APPEND LINES OF lt_shortcut TO lt_objcont.
 
* Create the packing list entry for the attachment
CLEAR ls_packing_list.
ls_packing_list-head_start = 1.
ls_packing_list-head_num = 1.
* The attachment starts where the e-mail body stopped
ls_packing_list-body_start = 1 + lv_body_length.
DESCRIBE TABLE lt_objcont LINES lv_attc_length.
* Substract the length of the email body from total
lv_attc_length = lv_attc_length - lv_body_length.
ls_packing_list-body_num = lv_attc_length.
* Fill the rest of the fields for the attachment
ls_packing_list-doc_type = 'SAP'.
ls_packing_list-obj_name = 'Transaction.SAP'.
ls_packing_list-obj_descr = 'Transaction.SAP'.
ls_packing_list-doc_size = 255 * lv_attc_length.
APPEND ls_packing_list TO lt_packing_list.
 
* Fill the receiver list
ls_receivers-receiver = 'someguy@example.xyz'.
ls_receivers-rec_type = 'U'.
APPEND ls_receivers TO lt_receivers.
 
* Create the e-mail with the attachment and send it
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
  EXPORTING
    document_data              = ls_document_data
    commit_work                = 'X'
  TABLES
    packing_list               = lt_packing_list
    contents_txt               = lt_objcont
    receivers                  = lt_receivers
  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.
  WRITE 'Email could not be sent.'.
  WRITE sy-subrc.
ELSE.
  WRITE 'Email was sent!'.
ENDIF.