Sending hyperlink in email with ABAP code

This blog will explain the ABAP code you can use to send an email from SAP system which is in HTML format including hyperlink.

Questions that will be answered in this blog are:

  • Which basis settings do I need to make for HTML mail format sending?
  • What code snippets can I re-use to send a hyperlink in an email from my custom ABAP program?

Basis settings for HTML mail

In order to be able to send an mail with a hyperlink the mail must have HTML format.

First check this table entry exists in table SXCONVERT2:

If not create it.

Now go to transaction SCOT and set the output format of RAW to HTM:

Save the settings.

ABAP code to mail hyperlink

The ABAP code to mail is as follows:

*&---------------------------------------------------------------------*
*& Report zemail_cl_bcs
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
  REPORT  zemail_cl_bcs.

  CONSTANTS:
    gc_subject TYPE so_obj_des VALUE 'ABAP Email with CL_BCS',
    gc_raw     TYPE char03 VALUE 'HTM'.

  DATA:
    gv_mlrec         TYPE so_obj_nam,
    gv_sent_to_all   TYPE os_boolean,
    gv_email         TYPE adr6-smtp_addr,
    gv_subject       TYPE so_obj_des,
    gv_text          TYPE bcsy_text,
    zls_text         TYPE soli,
    xhtml_string     TYPE xstring,
    gr_send_request  TYPE REF TO cl_bcs,
    gr_bcs_exception TYPE REF TO cx_bcs,
    gr_recipient     TYPE REF TO if_recipient_bcs,
    gr_sender        TYPE REF TO cl_sapuser_bcs,
    t_hex            TYPE solix_tab,
    gr_document      TYPE REF TO cl_document_bcs.

  DATA: zlv_longstring_message TYPE string.
  DATA: zlt_et_soli TYPE soli_tab.
  DATA: zls_et_soli TYPE soli.

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

      "Email FROM...
      gr_sender = cl_sapuser_bcs=>create( sy-uname ).
      "Add sender to send request
      CALL METHOD gr_send_request->set_sender
        EXPORTING
          i_sender = gr_sender.

      "Email TO...
      gv_email = 'guru@saptechnicalguru.com'.
      gr_recipient = cl_cam_address_bcs=>create_internet_address( gv_email ).
      "Add recipient to send request
      CALL METHOD gr_send_request->add_recipient
        EXPORTING
          i_recipient = gr_recipient
          i_express   = 'X'.

      CONCATENATE '<html><strong>Decission needed</strong><br/><br/>'
      '<tr><th style="color:blue;">Approval item</th>'
      '<a href=https://server:port/sap/bc/ui2/flp#WorkflowTask-displayInbox?allItems'
      '=true&/detail/XXX999_PGW/000000226597/TaskCollection(SAP__Origin=&#39;XXX999_PGW&#39;,InstanceID=&#39;000000226597&#39;)> click here to decide 000000226597</a>'

                   INTO zlv_longstring_message.

      CONCATENATE zlv_longstring_message '</html>' INTO zlv_longstring_message.

      CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
        EXPORTING
          text   = zlv_longstring_message
        IMPORTING
          buffer = xhtml_string
        EXCEPTIONS
          failed = 1
          OTHERS = 2.

      CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
        EXPORTING
          buffer     = xhtml_string
        TABLES
          binary_tab = t_hex.

      gr_document = cl_document_bcs=>create_document(
                      i_type    = gc_raw
                      i_hex    = t_hex
                      i_length  = '1200'
                      i_subject = gc_subject ).
      "Add document to send request
      CALL METHOD gr_send_request->set_document( gr_document ).

* set send immediately flag
      gr_send_request->set_send_immediately( 'X' ).
      "Send email
      CALL METHOD gr_send_request->send(
        EXPORTING
          i_with_error_screen = 'X'
        RECEIVING
          result              = gv_sent_to_all ).
      IF gv_sent_to_all = 'X'.
        WRITE 'Email sent!'.
      ENDIF.

      "Commit to send email
      COMMIT WORK.

      "Exception handling
    CATCH cx_bcs INTO gr_bcs_exception.
      WRITE:
        'Error!',
        'Error type:',
        gr_bcs_exception->error_type.
  ENDTRY.

The end result is as follows in the mail:

The hyperlink in the mail jumps to the URL, which in this case is the URL link to this specific workflow item in the FIORI inbox.

The coding explained

We use the CL_BCS class from SAP. BCS stands for Business Communication Service. This class provides all modern options to send mail. We set the sender and receiver.

We now build the mail in HTML. All is stored in zlv_longstring_message. We start with the <html> tag, and a header text in bold (strong). Then we add the text with the hyperlink (a href) in blue color.

The hyperlink towards the FIORi inbox contains ‘ characters. This does not convert well for all further on steps. So we replace ‘ instead the &#39 text. This &#39 text is the HTML character coding for an apostrophe (‘). In this way there is no misinterpretation at any browser.

At the end, we add the closing tag </html>. Now the HTML build up is ready and can be used to send.

The HTLM is converted via function modules SCMS_STRING_TO_XSTRING and SCMS_XSTRING_TO_BINARY to a binary. This binary is set as document with type HTM to the mail. The mail is then sent with immediate flag.

FIORI app for display mail transmissions

SAP has delivered diverse apps for basis administrators.

This blog will explain about the display email transmissions FIORI app.

Activating the app display email transmissions

The full activation manual is published on the FIORI reference library.

Short manual:

  • Activate SICF service nw_aps_om_eq
  • Activate ODATA service APS_OM_EMAIL_QUEUE_SRV
  • Manually add the tile in your catalog (use edit home page and than add the app)

Using the app display email transmissions

The main FIORI app tile will already show the amount of email transmission errors today:

By clicking the tile you reach the overview screen

By clicking the line you get details plus hints on the possible issue:

Workflow tips & tricks

SAP workflow is used for many different business scenarios. This blog will give tips and tricks for the basis part of SAP workflow.

Questions that will be answered in this blog are:

  • How can I send a reminder email to the workflow owners?
  • How to check how many items in the inbox a user has?
  • How to delete the items in the inbox of a user?
  • How to delete the items in the outbox of a user?
  • How can I terminate workflow(s) as administrator?
  • How do I execute general workflow activation?
  • Where can I find more FAQ and manuals on workflow?
  • How to solve workflow transport issues?
  • How can I add a general task to a transport?
  • How can I setup forwarding of workflow as admin?
  • How can I check if a user has setup a substitution?
  • How can I restart workflows?
  • What to check in case of delays in workflow?
  • How can I trigger workflow processing again after a system crash?
  • How to solve the Workflow error “maximum number of 10,000 nodes reached” error?

Workflow FAQ note

SAP has created an excellent FAQ note: 2214571 – Collection Note: Workflow troubleshooting guides, FAQs and important notes. This is a good starting point for find solutions to workflow issues.

Sending reminder email to workflow owners

Program RSWUWFML2 can be used to send reminders to workflow owners that they still have open workflow items:

It is important to know that only reminders will be send in mail if the workflow owner user ID’s mail address in maintained in SU01.

Important explanation OSS note:

Important bug fix OSS notes:

Changing the message subject of the reminder mail

To change the message subject of a mail, create a new message in SE91. Example is class Z_CUSTOM message 010 with text: “Gentle reminder of workflow”. Now put in field message class for subject field the name of your class (Z_CUSTOM) and the message number in message number for Subject:

Changing the body of the message of the reminder mail

Goto transaction SE61 and select text type Text in Dialog. First the default text SWU_NOTIF_INBOX:

Now use the copy button to copy the text to a Z text. For example Z_SWU_NOTIF_INBOX. Change the text as per you requirement and activate the text.

Now you can use this new text in the workflow reminder mail program:

SAP workflow inbox

Using transaction SOY5 or via program RSSOINBO you can get an overview of the amount of workflow items per user:

Via program RSSOINBD you can delete the inbox of a user:

Removing work items from user inbox

There are many ways to remove work items from and end users inbox: 2382266 – How to remove work items from user’s Inbox.

Removing work items from user outbox

To delete expired items from the users outbox, run program RSSOEXDA: 2774728 – Remove entries from user Outbox.

Changing priority of a work item

To change priority of a work item in SBWP follow the instructions in OSS note 2863528 – How to change the priority of a work item from SBWP.

Terminating workflows as administrator

Start transaction SWIA:

In the second screen select all the items you want to terminate and use menu option Edit / Work Item / Logically Delete. The workitem will now to status CANCELLED. Then they can be archived (see blog).

See also OSS note 2422812 – How to delete workflow item from inbox and OSS note 1705866 – “Logically Delete” for a large number of work items.

For execution of mass logical deletion in SWIA, you must select all items and in the command area enter ADMC as command and press enter:

Or select all and choose menu option Edit/Work Item/Logically Delete.

Please note: mass cancellation is possible. Mass completion not. Only via ABAP code (read this blog for the custom code which performs this). See OSS note 2650820 – Mass complete work items manually.

Restarting workflows

Transaction SWPR can be used to restart a workflow:

When there was a system crash transaction SWPC can be used to continue workflows:

Bug fix note: 3270257 – Multiple selection value help does not work for input field “Multistep task” in transaction SWPC.

How to find top level ID for a workflow item?

To find the top level for a workflow, follow the instructions in OSS note 3277360 – How to find the Top-Level work item ID of a workflow.

Reducing size of workflow tables

Workflow tables start with SWW. They can grow very large in a productive environment. For analysis see blog. For deletion and archiving see blog.

For workflow from idocs specifically, read OSS note 1813141 – How to delete unnecessary workitems of IDoc processing.

Specific note: 2847116 – SWF_TRC_CONT table huge growth.

Deleting workflow trace can be done via program RSWEQDELETE (see note 2162503 – Deleting trace records from Workflow tables).

Basic workflow activation in a new system

For activating workflow in a new system or after an S4HANA upgrade, please read this dedicated blog.

Delays in workflow

When you are experiencing delays in workflow, read OSS note 2146408 – Delay in executing a Workflow. Also check OSS note 888279 – Regulating/distributing the workflow load for load balancing on workflows.

Transport issues with workflow

Workflow development objects can give some issues in transports, since not all objects are immediately put into a transport upon development.

If you have set a workflow task to general and want to transport it, use program RHMOVE30 to put it into a transport. For more background read this SAP blog.

Substitution and forwarding

Using transaction SBCS_EXTCOM you can setup forwarding for a user ID towards different user ID as admin. This might be needed in case of illness of a user:

In the third tab of this transaction you can see if a user has setup a substitution himself.

Workflow error “maximum number of 10,000 nodes reached”

If you get this error, increase the value Maximum node number in transaction SWPA:

Do not just increase, but read OSS note 2397114 – Workflow error “maximum number of 10,000 nodes reached”, which advices to check the workflow definition, before increasing this value.

Use report RSWP_CHANGE_MAX_NODES to solve your immediate issue.

Workflow delegation and substitution

SAP workflow has options for delegation and substitution (for example when person is on holiday). Read more on this in the SAP wiki for substitution.

Workflow and system copy

After a system copy, check the workflow configuration again. See OSS note 3227538 – Workflow background steps are not getting completed after system copy.

SAP mail sending tips & tricks

This blog focuses on SAP mail sending tips and tricks.

Questions that will be answered are:

  • How can I add a disclosure to the mails I send form non-productive systems?
  • How do I restrict access to transaction SOST?
  • Which batch job to plan for sending mails?
  • How can I send encrypted or signed mails?
  • Is there a display only version of SCOT available?
  • How to send hyperlink in mail using ABAP?

Adding disclosure from to mails from development and test systems

If you want to send mails from development and test systems, but don’t want any risk that it looks like a productive mail, you can add a disclosure to the mail.

In SOST mail settings go to the disclosure function:

SOST to SODIS link

Or you can go directly there using the SODIS transaction.

In SODIS you key in the disclosure text:

SODIS disclosure

If you want you can test for any mail address if the disclosure will be shown or not by using the Routing Test function:

SODIS routing test

When sending mails from the SAP system the receiver now gets the disclosure. The real mail is pushed as text in the attachment of the mail (see OSS note 2842085 – Email body becoming to attachment in receiver side). You need to open the attachment to see the body of the text. Hyperlinks in the body will still work.

Restricting access to SOST transaction: give SOSG access

As admin you might want to restrict access to SOST transaction. This transaction is also often used by functional consultants to see if their mail is sent or not. When having access to SOST all functions like deletion and stopping of mails is also granted. What you can do is fully restrict access to SOST and grant the functional consultants access to transaction SOSG to display the mail status. It looks same as SOST, but has additional authorization checks. See also OSS note 2351372 – User access to transactions SOST, SOSV, SOSG and SOSB.

To allow sending of mail in SOSG, follow instructions from OSS note 2573586 – “Send” function greyed out in transaction SOSG.

Batch job for mail sending

For sending mail, you need to schedule batch job RSCONN01  with variant SAP&CONNECTINT. See OSS note 1912890 – Stuck email messages on SOST – RSCONN01 client dependent.

Mail read receipts

SAP mail sending can also use mail receipts. This might be wanted, but most of the times it is not wanted. More about read receipts is explained in OSS note 2161462 – How does Read Receipt work in SAPConnect?

To suppress it follow the instructions in OSS note 1607686 – Suppressing read notification requests.

Mail send restrictions based on user group

If you want to restrict mail sending based on user group, follow the instructions from OSS note 2623113 – How to limit e-mails by Sender Group in SCOT.

Mail encryption and signature

Start program RSCONN05 to set the mail signature and encryption settings. More background in OSS note 149926 – Secure e-mail: Encryption, digital signature.

Guided answers

See OSS note 3225275 – BC-SRV-COM Guided Answer for guided answers on mail setup.

Sending hyperlink in mail

If you have a requirement to send working hyperlinks in mails coming from SAP, read this blog on how to do this using custom ABAP code.

Increasing value of SMTP password length

See note 2363295 – Password for SMTP authentication is too short for increasing SMTP password length.