Skip to Content
Technical Articles
Author's profile photo Yasho Ratna Gupta

More than 50 Character lengths in Email Subject with class CL_BCS

Introduction: In SAP, the email subject line is limited to 50 characters only, but there may be circumstances where we need to have the subject line more than that.

In this article, I’d cover how we can get rid of this restriction.

If we use the CL_BCS class to build email functionality, we have the importing parameter (I_SUBJECT) with the data element SO_OBJ_DES (which is 50 characters long and can be used up to that limit) when creating a document (Method CREATE_DOCUMENT of class CL_DOCUMENT_BCS).

DATA(lo_document_ref) = cl_document_bcs=>create_document(
i_type    = lc_doc_type
i_text    = lt_content_txt
 i_subject = lv_subject ).

When we trigger an e-mail, the subject will get truncated in SAP.

And same issue will be outside SAP too.

To extend the e-mail subject by more than 50 characters, we can leverage the method SET_MESSAGE_SUBJECT of class CL_BCS.

And need to pass a blank( space ) in the mandatory importing parameter (I_SUBJECT) when creating the document reference.

DATA(lo_document_refcl_document_bcs=>create_document(
i_type    lc_doc_type
i_text    lt_content_txt
 i_subject space ).

DATA(lo_send_request_ref) = cl_bcs=>create_persistent( ).

    lo_send_request_ref->set_message_subject(
      EXPORTING
        ip_subject = CONV string( lv_subject ) ).

Though there is no Doc. Title in the SAP

But outside of SAP, we can see a subject line in its entirety and uncut.

Please refer to the detailed code logic below.

PARAMETERS : p_email TYPE ad_smtpadr.    "abc@gmail.com

DATA: lv_subject TYPE char100 VALUE 
      'Action Required: Check if you can put more than 50 characters'.

DATA(lo_send_request_ref) = cl_bcs=>create_persistent( ).

*Populate sender name
DATA(lo_sender_ref) = cl_sapuser_bcs=>create( sy-uname ).
IF lo_send_request_ref IS BOUND.

  IF lo_sender_ref IS BOUND.
*Add sender to send request
   lo_send_request_ref->set_sender(
      EXPORTING
        i_sender = lo_sender_ref ).

* If need to send more than 50 characters in length as a subject line
* Then set message in cl_bcs->set_message_subject 
* else populate it in method cl_document_bcs=>create_document
    lo_send_request_ref->set_message_subject(
      EXPORTING
        ip_subject = CONV string( lv_subject ) ).
  ENDIF.
ENDIF.

DATA(lo_recipient_ref) = cl_cam_address_bcs=>create_internet_address( p_email ).
IF lo_recipient_ref IS BOUND.
*Add recipient to send request
  lo_send_request_ref->add_recipient(
    EXPORTING
      i_recipient = lo_recipient_ref
      i_express   = abap_true ).
ENDIF.

DATA(lt_content_txt) = VALUE soli_tab( ( line = 'Hello Team,' )
      ( line = 'This e-mail has been automatically generated. Please do not reply to this e-mail.' )
      ( line = 'Thank You!' ) ).

* Create document for e-mail content
DATA(lo_document_ref) = cl_document_bcs=>create_document(
                                        i_type    = 'TXT'
                                        i_text    = lt_content_txt
*                                       i_subject = CONV so_obj_des( lv_subject ) ).
                                        i_subject = space ).

IF lo_document_ref IS BOUND.
* Add document to send request
  lo_send_request_ref->set_document( lo_document_ref ).
ENDIF.

* Set parameter to send e-mail immediately
lo_send_request_ref->set_send_immediately(
  EXPORTING
    i_send_immediately = abap_true ).

* Send email
DATA(lv_sent_to_all) = lo_send_request_ref->send( ).

* If e-mail is successful then do commit work
IF lv_sent_to_all = abap_true.
  COMMIT WORK AND WAIT.
ENDIF.

This concludes the functionality.

Thank you for reading the article. I hope you found it interesting. Please feel free to share your insightful observations and suggestions.

Assigned Tags

      6 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Jens Michaelsen
      Jens Michaelsen

      Thank you for the blog. I tried it out and it works fine.

      Author's profile photo Yasho Ratna Gupta
      Yasho Ratna Gupta
      Blog Post Author

      Thank you Jens! I am glad that it worked for you.

      Author's profile photo Jens Michaelsen
      Jens Michaelsen

      In mail-templates in SAP there is the problem, that the input field for the subject of the template allows 255 characters, but if you render the template, the subject of the mail document has only 50 characters
      So we can render a second time and get the full subject (255 characters maximum). And now with your coding we can replace the short subject by the full subject.

       

       

      Author's profile photo Yasho Ratna Gupta
      Yasho Ratna Gupta
      Blog Post Author

      You are absolutely right!

      Yes it does allow 255 characters in template but when create email document with CL_DOCUMENT_BCS=>CREATE_FROM_MULTIRELATED( ), it does take only 50 characters in length.

      Author's profile photo Jens Michaelsen
      Jens Michaelsen

      I thought of repository-object called 'EMail-Template', you create it with right click in SE80 by the menu 'more'.There is a good description by Prabhjot Bhatia in  https://blogs.sap.com/2019/10/12/e-mail-templates-in-s4-hana/

      So I used the following statements

      DATA(lo_email_api) = cl_smtg_email_api=>get_instance( iv_template_id = 'ZZ_MY_TEMPLATE' ).
      lo_email_api->render_bcs( io_bcs = lo_send_request iv_language = sy-langu it_data_key = lt_cds_key ).

      And I get a subject with only 50 characters, although the email-template allows 255. But with your coding I can replace the short subject by a full subject.

       

      Author's profile photo René PHILIPPE
      René PHILIPPE

      Your blog post was really helpful for me, thank you !