Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Sending SAP Query output executed in Background as mail attachment

Former Member
0 Kudos

Hi Gurus,

We are using SAP Query (SQ01). We want to download the file to an Excel spreadsheet as a attachment of mail in background job . we has been planned to send by email a spool. However, it always send an empty attachement with extension .ALI. The attachment should not be empty.

Any ideas on how this can be achieved would be appreciated.

Regards,

Ravinder

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

see the sample code..



we can execute the abap query through background in t-code START_REPORT b y giving the details...


and  call the methiod

*FM called that returns the Spool Request Number data into and internal table
  CALL FUNCTION 'RSPO_RETURN_ABAP_SPOOLJOB'
    EXPORTING
      rqident              = p_spool                     "Spool Request Number
      first_line           = 1
    TABLES
      buffer               = it_spool_xls                            "Internal table that will have the Spool Request No data

*To convert the spool data into excel format
  CALL FUNCTION 'SO_RAW_TO_RTF'
    TABLES
      objcont_old = it_spool_xls               "Internal table having spool data
      objcont_new = it_xls_spool.           "Int table having Excel format data converted from Spool data

*To convert the excel format data into string
  LOOP AT it_xls_spool.
    CONCATENATE xls_string it_xls_spool INTO xls_string.           "xls_string will have the data as one string
  ENDLOOP.
*To convert the string into xstring format for mail sending
  CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    EXPORTING
      text           = xls_string                         "String of String data type
   MIMETYPE       = ' '
   ENCODING       = ' '
   IMPORTING
     buffer         = xls_str                             "String of XString data type
   EXCEPTIONS
     failed         = 1
     OTHERS         = 2
            .
  IF sy-subrc <> 0.
  ENDIF.

*To send mail the xstring having excel format data
  TRY.
*     Create persistent send request
      send_request = cl_bcs=>create_persistent( ).

*     Create and set document
      pdf_content = cl_document_bcs=>xstring_to_solix( xls_str ).          "String having Excel format
                                                                                "Data above populated

      document = cl_document_bcs=>create_document(
            i_type    = 'XLS'
            i_hex     = pdf_content
            i_length  = pdf_size
            i_subject = 'Asset Origin Details Report' ).    "#EC NOTEXT

*     Add document object to send request
      send_request->set_document( document ).

*     Add recipient (e-mail address)
*     Create recipient object
      recipient = cl_cam_address_bcs=>create_internet_address( p_email ).

*     Add recipient object to send request
      send_request->add_recipient( recipient ).

*     Send document
      sent_to_all = send_request->send( i_with_error_screen = 'X' ).

      COMMIT WORK.

D@s

5 REPLIES 5

Former Member
0 Kudos

Hi,

see the sample code..



we can execute the abap query through background in t-code START_REPORT b y giving the details...


and  call the methiod

*FM called that returns the Spool Request Number data into and internal table
  CALL FUNCTION 'RSPO_RETURN_ABAP_SPOOLJOB'
    EXPORTING
      rqident              = p_spool                     "Spool Request Number
      first_line           = 1
    TABLES
      buffer               = it_spool_xls                            "Internal table that will have the Spool Request No data

*To convert the spool data into excel format
  CALL FUNCTION 'SO_RAW_TO_RTF'
    TABLES
      objcont_old = it_spool_xls               "Internal table having spool data
      objcont_new = it_xls_spool.           "Int table having Excel format data converted from Spool data

*To convert the excel format data into string
  LOOP AT it_xls_spool.
    CONCATENATE xls_string it_xls_spool INTO xls_string.           "xls_string will have the data as one string
  ENDLOOP.
*To convert the string into xstring format for mail sending
  CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    EXPORTING
      text           = xls_string                         "String of String data type
   MIMETYPE       = ' '
   ENCODING       = ' '
   IMPORTING
     buffer         = xls_str                             "String of XString data type
   EXCEPTIONS
     failed         = 1
     OTHERS         = 2
            .
  IF sy-subrc <> 0.
  ENDIF.

*To send mail the xstring having excel format data
  TRY.
*     Create persistent send request
      send_request = cl_bcs=>create_persistent( ).

*     Create and set document
      pdf_content = cl_document_bcs=>xstring_to_solix( xls_str ).          "String having Excel format
                                                                                "Data above populated

      document = cl_document_bcs=>create_document(
            i_type    = 'XLS'
            i_hex     = pdf_content
            i_length  = pdf_size
            i_subject = 'Asset Origin Details Report' ).    "#EC NOTEXT

*     Add document object to send request
      send_request->set_document( document ).

*     Add recipient (e-mail address)
*     Create recipient object
      recipient = cl_cam_address_bcs=>create_internet_address( p_email ).

*     Add recipient object to send request
      send_request->add_recipient( recipient ).

*     Send document
      sent_to_all = send_request->send( i_with_error_screen = 'X' ).

      COMMIT WORK.

D@s

Former Member
0 Kudos

Hi All/D@s

I am issues is not for logic

I had scheduled background for query and i am getting attachment in SOST with extension .ALI and i can see data in attachment in SOST .But the destination mail id received attachment data is empty.

So please suggest the solution.

Thanks for understanding.

Regards,

Ravinder

0 Kudos

That seems like an odd suggestion/method anyway. Why not use the private file option and the standard query enhancement SQUE0001 for accessing query result data in the background? You can work with the actual data instead of hacking around with spool/ALI output.

Former Member
0 Kudos

Hi Brad Bohn and All

Thanks for your reply

till now i did not worked standard query enhancement SQUE0001, will please tell me how to develop that enhancement for sap queries and please provide example if passible

Thanks for understanding

Regards

Ravinder

0 Kudos

See the documentation in SMOD. I believe it's also documented in OSS if I remember correctly. It's an old enhancement.