Skip to Content

Report in Background

I developing a report which has to run in background. The results have to be extracted to a excel file and mailed to the concerned user entered on the selection screen. How can i do that...? thank you.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Nov 30, 2007 at 03:31 PM

    Hi,

    I know little bit abt background job

    To run the prog in bakbround..the following is the procedure..

    Go to se38, open the program

    Execute it (Press F8)

    Enter values on selection screen and press F9

    Enter output device

    In spool option uncheck print immediately

    Press enter, another screen appears with heading start time, u can press start immediately

    Then save. Background job is scheduled.

    You can check the status in sm37. Check the job log for the status.

    Reward if helpful..

    Regards,

    Karthick.

    Message was edited by:

    Karthick

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Nov 30, 2007 at 03:36 PM

    Hi There,

    When you run the report in Background, don't try to download the file to PC, so use open dataset concept and download it to app. server.

    Regarding sending mail you can search in SDN, you will get good examples simply you can copy and paste with small changes.

    Pavan

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 30, 2007 at 04:10 PM

    Hi

    I guess you will put all your results in the internal table.Then you can use following code for refernce.It sends a pdf attachment to the mail user id mentioned in the select-options.you can convert spool to pdf and then send it as an attachment

    &----


    *& Report ZBP_BDCP_CLFO_UPLOAD *

    *& *

    &----


    *& Created By : Raghuram Devagiri. *

    REPORT zbp_bdcp_clfo_upload LINE-SIZE 255 LINE-COUNT 65

    NO STANDARD PAGE HEADING MESSAGE-ID zcrm_upload.

    TABLES:bbp_iu01.

    ----


    • Structure declarations *

    ----


    *Main Upload structure

    TYPES:BEGIN OF w_data,

    cust_id TYPE bu_id_number, "Customer ID

    idnumber TYPE bu_id_number, "Investor ID

    tag_code(2) TYPE c, "Tag code

    lob(3) TYPE c, "Line of business

    matching_rule(255) TYPE c, "Matching rule

    create_date TYPE dats, "Creation date

    mod_date TYPE dats, "Modified date

    crt_user_id(20) TYPE c, "Creator User ID

    mod_user_id(20) TYPE c, "Modifier USer ID

    END OF w_data.

    *Business partner

    DATA:w_bp TYPE bu_partner. "Business partner number

    *Structure of the Successful record

    TYPES:BEGIN OF w_success ,

    custid TYPE bu_bpext, "Customer ID

    rec TYPE i, "Record Number

    bp TYPE bu_partner, "Business Partner Number

    END OF w_success.

    *Structure of the Unsuccessful record

    TYPES:BEGIN OF w_errors,

    rec(5) TYPE c, "Record Number

    cust_id TYPE bu_id_number, "Customer ID

    idnumber TYPE bu_id_number, "Investor ID

    tag_code(2) TYPE c, "Tag code

    lob(3) TYPE c, "Line of business

    matching_rule(255) TYPE c, "Matching rule

    create_date TYPE dats, "Creation date

    mod_date TYPE dats, "Modified date

    crt_user_id(20) TYPE c, "Creator User ID

    mod_user_id(20) TYPE c, "Modifier USer ID

    msg TYPE bapi_msg,

    END OF w_errors.

    *Spool IDs

    TYPES: BEGIN OF w_tbtcp.

    INCLUDE STRUCTURE tbtcp.

    TYPES: END OF w_tbtcp.

    • Class for String processing

    CLASS cl_abap_char_utilities DEFINITION LOAD.

    ----


    • Constant declarations *

    ----


    *Identification types

    CONSTANTS:c_cust_id TYPE bu_id_type VALUE 'ZID009', "Customer id type

    c_folio_id_type TYPE bu_id_type VALUE 'ZID001', "Folio id type

    c_client_id_type TYPE bu_id_type VALUE 'ZID002', "Client id type

    c_lob_mf(2) TYPE c VALUE 'MF', "Line of business(MF)

    c_lob_pms(3) TYPE c VALUE 'PMS'. "Line of business(PMS)

    CONSTANTS: con_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.

    *Binary store for PDF

    CONSTANTS:c_device(4) TYPE c VALUE 'LOCL',

    c_format(3) TYPE c VALUE 'PDF',

    c_no(1) TYPE c VALUE ' '.

    ----


    • Internal Table declarations *

    ----


    DATA: it_data TYPE STANDARD TABLE OF w_data WITH HEADER LINE,

    it_success_records TYPE STANDARD TABLE OF w_success WITH HEADER LINE,

    it_error_records TYPE STANDARD TABLE OF w_errors WITH HEADER LINE,

    it_bapiret2 LIKE bapiret2 OCCURS 0 WITH HEADER LINE.

    • Binary store for PDF

    DATA: BEGIN OF i_pdf_output OCCURS 0.

    INCLUDE STRUCTURE tline.

    DATA: END OF i_pdf_output.

    DATA: it_tbtcp TYPE STANDARD TABLE OF w_tbtcp INITIAL SIZE 0.

    DATA: it_mess_bod LIKE solisti1 OCCURS 0 WITH HEADER LINE,

    it_mess_att LIKE solisti1 OCCURS 0 WITH HEADER LINE.

    ----


    • Data declarations *

    ----


    DATA:g_tabix LIKE sy-tabix,

    g_identification TYPE bapibus1006_identification,

    w1_tbtcp TYPE w_tbtcp.

    • Job Runtime Parameters

    DATA: g_eventid LIKE tbtcm-eventid,

    g_eventparm LIKE tbtcm-eventparm,

    g_external_program_active LIKE tbtcm-xpgactive,

    g_jobcount LIKE tbtcm-jobcount,

    g_jobname LIKE tbtcm-jobname,

    g_stepcount LIKE tbtcm-stepcount,

    g_error LIKE sy-subrc,

    g_reciever LIKE sy-subrc.

    • Spool to PDF conversions

    DATA: g_spool_nr LIKE tsp01-rqident,

    g_bytecount LIKE tst01-dsize,

    g_buffer TYPE string.

    *converting spool request to PDF and to trigger mail

    DATA: g_recsize TYPE i,

    g_subject LIKE sodocchgi1-obj_descr,

    g_sender_type LIKE soextreci1-adr_typ,

    g_attachment_desc TYPE so_obj_nam,

    g_attachment_name TYPE so_obj_des.

    *Mail IDs

    DATA: g_sender TYPE somlreci1-receiver.

    ***************************************************************************

    • Selection Screen Declaration *

    ***************************************************************************

    SELECTION-SCREEN: BEGIN OF BLOCK blk1 WITH FRAME TITLE text-001.

    PARAMETERS: p_file(1024) TYPE c OBLIGATORY LOWER CASE.

    SELECTION-SCREEN: END OF BLOCK blk1.

    SELECTION-SCREEN: BEGIN OF BLOCK blk2 WITH FRAME TITLE text-002.

    SELECT-OPTIONS:s_email FOR bbp_iu01-email NO INTERVALS.

    SELECTION-SCREEN: END OF BLOCK blk2.

    SELECTION-SCREEN: BEGIN OF BLOCK blk3 WITH FRAME TITLE text-026.

    SELECTION-SCREEN COMMENT /1(72) text-027.

    SELECTION-SCREEN COMMENT /1(72) text-028.

    SELECTION-SCREEN COMMENT /1(79) text-029.

    SELECTION-SCREEN COMMENT /1(72) text-030.

    SELECTION-SCREEN: END OF BLOCK blk3.

    ----


    • START-OF-SELECTION

    ----


    START-OF-SELECTION.

    *To upload folio file data from application server

    PERFORM file_upload USING p_file.

    LOOP AT it_data.

    g_tabix = sy-tabix.

    IF it_data-cust_id IS NOT INITIAL.

    *To get Business partner based on customer id.

    PERFORM get_bp.

    *To add identification to the selected business partner

    IF it_data-lob EQ c_lob_mf.

    PERFORM add_identification TABLES it_data

    USING c_folio_id_type

    w_bp.

    ELSEIF it_data-lob EQ c_lob_pms.

    PERFORM add_identification TABLES it_data

    USING c_client_id_type

    w_bp.

    ELSE.

    MOVE-CORRESPONDING it_data TO it_error_records.

    it_error_records-rec = g_tabix.

    it_error_records-msg = text-005.

    APPEND it_error_records.

    ENDIF.

    ENDIF.

    ENDLOOP.

    *Constructing the Return Messages for the Job

    PERFORM job_status TABLES it_success_records it_error_records.

    • Converting the return messages into PDF file and trigerring a mail notification

    IF sy-batch EQ 'X'.

    PERFORM get_job_details. "To get job details

    PERFORM obtain_spool_id. "To obtain spool id

    PERFORM convert_spool_to_pdf. "To convert spool to pdf

    PERFORM process_email. "To process email

    WAIT UP TO 5 SECONDS.

    SUBMIT rsconn01 WITH mode = 'INT'

    WITH output = 'X'

    AND RETURN.

    ENDIF.

    &----


    *& Form file_upload

    &----


    • To upload the file from Applcation server to internal table

    ----


    FORM file_upload USING uv_filename TYPE any .

    DATA:l_filestr TYPE string.

    REFRESH it_data.

    CLEAR it_data.

    • Uploading the file from application server.

    OPEN DATASET uv_filename FOR INPUT IN TEXT MODE ENCODING DEFAULT.

    IF sy-subrc = 0.

    DO.

    READ DATASET uv_filename INTO l_filestr.

    IF sy-subrc <> 0.

    EXIT.

    ELSE.

    SPLIT l_filestr AT con_tab INTO:

    it_data-cust_id it_data-idnumber

    it_data-tag_code it_data-lob

    it_data-matching_rule it_data-create_date

    it_data-mod_date it_data-crt_user_id

    it_data-mod_user_id.

    APPEND it_data.

    CLEAR it_data.

    ENDIF.

    ENDDO.

    ENDIF.

    CLOSE DATASET uv_filename.

    CLEAR :it_data.

    ENDFORM. " file_upload

    &----


    *& Form GET_BP

    &----


    • To obtain business partner based on customer id

    ----


    FORM get_bp.

    CLEAR w_bp.

    SELECT SINGLE partner FROM but0id

    INTO w_bp

    WHERE

    type = c_cust_id

    AND

    idnumber = it_data-cust_id.

    • If there is no BP number corresponding to the customer ID

    IF sy-subrc <> 0.

    it_error_records-cust_id = it_data-cust_id.

    it_error_records-rec = g_tabix.

    it_error_records-msg = text-007.

    it_error_records-idnumber = it_data-idnumber.

    it_error_records-lob = it_data-lob.

    it_error_records-tag_code = it_data-tag_code.

    it_error_records-matching_rule = it_data-matching_rule.

    it_error_records-create_date = it_data-create_date.

    it_error_records-mod_date = it_data-mod_date.

    it_error_records-crt_user_id = it_data-crt_user_id.

    it_error_records-mod_user_id = it_data-mod_user_id.

    APPEND it_error_records.

    ENDIF.

    ENDFORM. " GET_BP

    &----


    *& Form ADD_IDENTIFICATION

    &----


    • * Adding up Identification numbers to the BP

    ----


    FORM add_identification TABLES ut_data STRUCTURE it_data

    USING uv_id_type TYPE bu_id_type

    uv_bp TYPE bu_partner.

    REFRESH it_bapiret2.

    CLEAR it_bapiret2.

    CALL FUNCTION 'BUPA_IDENTIFICATION_ADD'

    EXPORTING

    iv_partner = uv_bp

    iv_identificationnumber = ut_data-idnumber

    is_identification = g_identification

    iv_identificationtype = uv_id_type

    iv_x_save = 'X'

    TABLES

    et_return = it_bapiret2.

    *If there are no error messages Success internal table is updated

    IF it_bapiret2[] IS INITIAL.

    *Commit bapi transaction

    PERFORM bapi_commit.

    *Filling up success records table

    it_success_records-custid = ut_data-cust_id.

    it_success_records-rec = g_tabix.

    it_success_records-bp = uv_bp.

    APPEND it_success_records.

    ELSE.

    *Filling up error records table

    READ TABLE it_bapiret2 INDEX 1.

    it_error_records-cust_id = ut_data-cust_id.

    it_error_records-rec = g_tabix.

    it_error_records-msg = it_bapiret2-message.

    it_error_records-idnumber = ut_data-idnumber.

    it_error_records-lob = ut_data-lob.

    it_error_records-tag_code = ut_data-tag_code.

    it_error_records-matching_rule = ut_data-matching_rule.

    it_error_records-create_date = ut_data-create_date.

    it_error_records-mod_date = ut_data-mod_date.

    it_error_records-crt_user_id = ut_data-crt_user_id.

    it_error_records-mod_user_id = ut_data-mod_user_id.

    APPEND it_error_records.

    ENDIF.

    ENDFORM. "add_identification

    &----


    *& Form show_status

    &----


    • Show the statuses of all the records of the input file after

    • the processing of each of the records

    ----


    FORM job_status TABLES ut_success_records STRUCTURE it_success_records

    ut_error_records STRUCTURE it_error_records.

    DATA: l_success_lines TYPE i,

    l_error_lines TYPE i,

    l_filename TYPE string VALUE 'error_file.txt',

    l_filestring TYPE string,

    l_lines TYPE i,

    l_msg TYPE string.

    WRITE: text-006.

    IF it_data[] IS NOT INITIAL. "To check whether data is transferred from file

    DESCRIBE TABLE it_data LINES l_lines.

    DESCRIBE TABLE ut_error_records LINES l_error_lines.

    l_success_lines = l_lines - l_error_lines.

    WRITE:/ text-008, p_file, 40 text-009,l_lines.

    SKIP 2.

    WRITE: / text-010,l_success_lines.

    WRITE: / text-011,l_error_lines.

    SKIP 2.

    IF ut_error_records[] IS NOT INITIAL.

    REPLACE '.TXT' WITH '_ERROR.TXT' INTO p_file .

    REPLACE '.txt' WITH '_error.txt' INTO p_file.

    CONCATENATE text-012 ' : ' p_file INTO l_msg.

    SKIP 2.

    WRITE:/ l_msg.

    • To create the error records file in application server

    OPEN DATASET p_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

    IF sy-subrc EQ 0.

    LOOP AT ut_error_records.

    CONCATENATE ut_error_records-rec ut_error_records-cust_id

    ut_error_records-idnumber ut_error_records-tag_code

    ut_error_records-lob ut_error_records-matching_rule

    ut_error_records-create_date ut_error_records-mod_date

    ut_error_records-crt_user_id ut_error_records-mod_user_id

    ut_error_records-msg

    INTO l_filestring SEPARATED BY con_tab.

    TRANSFER l_filestring TO p_file.

    CLEAR l_filestring.

    ENDLOOP.

    CLOSE DATASET l_filename.

    ENDIF.

    ENDIF.

    IF ut_success_records[] IS NOT INITIAL.

    WRITE: / text-015.

    WRITE: / '----


    '.

    WRITE: /1 text-003,15 text-025, 35 text-004.

    • To display successful records

    LOOP AT ut_success_records.

    WRITE: /1 ut_success_records-rec CENTERED,

    15 ut_success_records-custid LEFT-JUSTIFIED,

    35 ut_success_records-bp CENTERED.

    ENDLOOP.

    ENDIF.

    ELSE.

    SKIP 2.

    WRITE: p_file,

    / text-013. "If no file is found in application server

    ENDIF.

    • For Spool request to be converted to PDF

    NEW-PAGE.

    COMMIT WORK.

    NEW-PAGE PRINT OFF.

    ENDFORM. "show_status

    &----


    *& Form get_job_details

    &----


    • Get the Job details

    ----


    FORM get_job_details .

    • Get current job details

    CALL FUNCTION 'GET_JOB_RUNTIME_INFO'

    IMPORTING

    eventid = g_eventid

    eventparm = g_eventparm

    external_program_active = g_external_program_active

    jobcount = g_jobcount

    jobname = g_jobname

    stepcount = g_stepcount

    EXCEPTIONS

    no_runtime_info = 1

    OTHERS = 2.

    ENDFORM. " get_job_details

    &----


    *& Form obtain_spool_id *

    &----


    • Get the Spool ID *

    ----


    FORM obtain_spool_id .

    CHECK NOT ( g_jobname IS INITIAL ).

    CHECK NOT ( g_jobcount IS INITIAL ).

    SELECT * FROM tbtcp

    INTO TABLE it_tbtcp

    WHERE jobname = g_jobname

    AND jobcount = g_jobcount

    AND stepcount = g_stepcount

    AND listident <> '0000000000'

    ORDER BY jobname

    jobcount

    stepcount.

    READ TABLE it_tbtcp INTO w1_tbtcp INDEX 1.

    IF sy-subrc = 0.

    g_spool_nr = w1_tbtcp-listident.

    MESSAGE s004 WITH g_spool_nr.

    ELSE.

    MESSAGE s005 .

    ENDIF.

    ENDFORM. " obtain_spool_id

    &----


    *& Form convert_spool_to_pdf *

    &----


    • Convert the Spool to PDF *

    ----


    FORM convert_spool_to_pdf .

    CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'

    EXPORTING

    src_spoolid = g_spool_nr

    no_dialog = c_no

    dst_device = c_device

    IMPORTING

    pdf_bytecount = g_bytecount

    TABLES

    pdf = i_pdf_output

    EXCEPTIONS

    err_no_abap_spooljob = 1

    err_no_spooljob = 2

    err_no_permission = 3

    err_conv_not_possible = 4

    err_bad_destdevice = 5

    user_cancelled = 6

    err_spoolerror = 7

    err_temseerror = 8

    err_btcjob_open_failed = 9

    err_btcjob_submit_failed = 10

    err_btcjob_close_failed = 11

    OTHERS = 12.

    CHECK sy-subrc = 0.

    • Transfer the 132-long strings to 255-long strings

    LOOP AT i_pdf_output.

    TRANSLATE i_pdf_output USING ' ~'.

    CONCATENATE g_buffer i_pdf_output INTO g_buffer.

    ENDLOOP.

    TRANSLATE g_buffer USING '~ '.

    DO.

    it_mess_att = g_buffer.

    APPEND it_mess_att.

    SHIFT g_buffer LEFT BY 255 PLACES.

    IF g_buffer IS INITIAL.

    EXIT.

    ENDIF.

    ENDDO.

    ENDFORM. " convert_spool_to_pdf

    &----


    *& Form process_email *

    &----


    • Process the email *

    ----


    FORM process_email .

    DESCRIBE TABLE it_mess_att LINES g_recsize.

    CHECK g_recsize > 0.

    PERFORM send_email TABLES s_email.

    ENDFORM. " process_email

    &----


    *& Form send_email *

    &----


    • Send email to concerned recievers *

    ----


    FORM send_email TABLES ut_email STRUCTURE s_email.

    CHECK NOT ( ut_email IS INITIAL ).

    REFRESH it_mess_bod.

    • Default subject matter

    g_subject = text-016.

    g_attachment_desc = text-017.

    it_mess_bod = text-018.

    APPEND it_mess_bod.

    it_mess_bod = text-019.

    APPEND it_mess_bod.

    CLEAR it_mess_bod.

    it_mess_bod = text-020.

    APPEND it_mess_bod.

    CLEAR it_mess_bod.

    it_mess_bod = text-021.

    APPEND it_mess_bod.

    CLEAR it_mess_bod.

    APPEND it_mess_bod.

    APPEND it_mess_bod.

    it_mess_bod = text-022.

    APPEND it_mess_bod.

    • If no sender specified - default blank

    IF g_sender EQ space.

    g_sender_type = space.

    ELSE.

    g_sender_type = 'INT'.

    ENDIF.

    • Send file by email as PDF document

    PERFORM send_file_as_email_attachment

    TABLES it_mess_bod

    it_mess_att

    ut_email

    USING text-023

    c_format

    g_attachment_name

    g_attachment_desc

    g_sender

    g_sender_type

    CHANGING g_error

    g_reciever.

    ENDFORM. " send_email

    &----


    *& Form send_file_as_email_attachment

    &----


    • This Subroutine is used to send the PDF file as attachment *

    ----


    FORM send_file_as_email_attachment TABLES ut_message STRUCTURE it_mess_bod

    ut_attach STRUCTURE it_mess_att

    ut_mail STRUCTURE s_email

    USING uv_mtitle TYPE any

    uv_format TYPE any

    uv_filename TYPE so_obj_des

    uv_attdescription TYPE so_obj_nam

    uv_sender_address TYPE so_recname

    uv_sender_addres_type TYPE so_adr_typ

    CHANGING uv_error TYPE sysubrc

    uv_reciever TYPE sysubrc.

    DATA: l_error TYPE sy-subrc,

    l_reciever TYPE sy-subrc,

    l_mtitle LIKE sodocchgi1-obj_descr,

    l_format TYPE so_obj_tp ,

    l_attdescription TYPE so_obj_nam ,

    l_attfilename TYPE so_obj_des ,

    l_sender_address LIKE soextreci1-receiver,

    l_sender_address_type LIKE soextreci1-adr_typ,

    l_cnt TYPE i,

    l_sent_all(1) TYPE c,

    l_receiver LIKE sy-subrc.

    DATA: lt_packing_list LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,

    lt_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE,

    lt_attachment LIKE solisti1 OCCURS 0 WITH HEADER LINE,

    wa_doc_data LIKE sodocchgi1.

    l_mtitle = uv_mtitle.

    l_format = uv_format.

    l_attdescription = uv_attdescription.

    l_attfilename = uv_filename.

    l_sender_address = uv_sender_address.

    l_sender_address_type = uv_sender_addres_type.

    • Fill the document data.

    wa_doc_data-doc_size = 1.

    • Populate the subject/generic message attributes

    wa_doc_data-obj_langu = sy-langu.

    wa_doc_data-obj_name = 'SAPRPT'.

    wa_doc_data-obj_descr = l_mtitle .

    wa_doc_data-sensitivty = 'F'.

    • Fill the document data and get size of attachment

    CLEAR wa_doc_data.

    READ TABLE ut_attach INDEX l_cnt.

    wa_doc_data-doc_size =

    ( l_cnt - 1 ) * 255 + STRLEN( ut_attach ).

    wa_doc_data-obj_langu = sy-langu.

    wa_doc_data-obj_name = 'SAPRPT'.

    wa_doc_data-obj_descr = l_mtitle.

    wa_doc_data-sensitivty = 'F'.

    CLEAR lt_attachment.

    REFRESH lt_attachment.

    lt_attachment[] = ut_attach[].

    • Describe the body of the message

    CLEAR lt_packing_list.

    REFRESH lt_packing_list.

    lt_packing_list-transf_bin = space.

    lt_packing_list-head_start = 1.

    lt_packing_list-head_num = 0.

    lt_packing_list-body_start = 1.

    DESCRIBE TABLE ut_message LINES lt_packing_list-body_num.

    lt_packing_list-doc_type = 'RAW'.

    APPEND lt_packing_list.

    • Create attachment notification

    lt_packing_list-transf_bin = 'X'.

    lt_packing_list-head_start = 1.

    lt_packing_list-head_num = 1.

    lt_packing_list-body_start = 1.

    DESCRIBE TABLE lt_attachment LINES lt_packing_list-body_num.

    lt_packing_list-doc_type = l_format.

    lt_packing_list-obj_descr = l_attdescription.

    lt_packing_list-obj_name = l_attfilename.

    lt_packing_list-doc_size = lt_packing_list-body_num * 255.

    APPEND lt_packing_list.

    • Add the recipients email address

    LOOP AT ut_mail.

    CLEAR lt_receivers.

    REFRESH lt_receivers.

    lt_receivers-receiver = ut_mail-low.

    lt_receivers-rec_type = 'U'.

    lt_receivers-com_type = 'INT'.

    lt_receivers-notif_del = 'X'.

    lt_receivers-notif_ndel = 'X'.

    APPEND lt_receivers.

    ENDLOOP.

    CALL FUNCTION 'SO_DOCUMENT_SEND_API1'

    EXPORTING

    document_data = wa_doc_data

    put_in_outbox = 'X'

    commit_work = 'X'

    IMPORTING

    sent_to_all = l_sent_all

    TABLES

    packing_list = lt_packing_list

    contents_bin = lt_attachment

    contents_txt = ut_message

    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.

    • Populate zerror return code

    l_error = sy-subrc.

    • Populate zreceiver return code

    LOOP AT lt_receivers.

    l_receiver = lt_receivers-retrn_code.

    ENDLOOP.

    ENDFORM. " send_file_as_email_attachment

    &----


    *& Form bapi_commit

    &----


    • To commit Bapi transaction

    ----


    FORM bapi_commit .

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

    EXPORTING

    wait = 'X'.

    ENDFORM. " bapi_commit

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 02, 2007 at 07:06 AM

    Hi

    Sending mail with attachment report in Background

    Content Author: Fernando Faian

    I have read the hint about "Sending mail with attachment report".

    It's great, but how can I make this function work in background??

    I had that needed last year too. See attachment a function group with two functions. The second one has that functionality to send email or fax (SAP office) with attachment objects in background job using SO_ATTACHMENT_INSERT function.

    Pay attention because itÂ’s working with output list from spool converted to pdf.

    =================================================================================

    z_send_email_fax_global

    FUNCTION-POOL z_gfaian_mail_fax. "MESSAGE-ID ..

    *----


    • WORK TABLE AREAS

    *----


    TABLES: tsp01.

    *----


    • INTERNAL TABLES

    *----


    DATA: lt_rec_tab LIKE STANDARD TABLE OF soos1 WITH HEADER LINE,

    lt_note_text LIKE STANDARD TABLE OF soli WITH HEADER LINE,

    lt_attachments LIKE STANDARD TABLE OF sood5 WITH HEADER LINE.

    DATA: lt_objcont LIKE STANDARD TABLE OF soli WITH HEADER LINE,

    lt_objhead LIKE STANDARD TABLE OF soli WITH HEADER LINE.

    DATA: pdf_format LIKE STANDARD TABLE OF tline WITH HEADER LINE.

    TYPES: BEGIN OF y_files,

    file(60) TYPE c,

    END OF y_files.

    DATA: lt_files TYPE STANDARD TABLE OF y_files WITH HEADER LINE.

    DATA: l_objcont LIKE soli OCCURS 0 WITH HEADER LINE.

    DATA: l_objhead LIKE soli OCCURS 0 WITH HEADER LINE.

    *----


    • STRUCTURES

    *----


    DATA: folder_id LIKE soodk,

    object_id LIKE soodk,

    link_folder_id LIKE soodk,

    g_document LIKE sood4,

    • g_header_data LIKE sood2,

    g_folmem_data LIKE sofm2,

    g_header_data LIKE sood2,

    g_receive_data LIKE soos6,

    g_ref_document LIKE sood4,

    g_new_parent LIKE soodk,

    l_folder_id LIKE sofdk,

    v_email(50).

    DATA: hd_dat like sood1.

    *----


    • VARIABLES

    *----


    DATA: client LIKE tst01-dclient,

    name LIKE tst01-dname,

    objtype LIKE rststype-type,

    type LIKE rststype-type.

    DATA: numbytes TYPE i,

    arc_idx LIKE toa_dara,

    pdfspoolid LIKE tsp01-rqident,

    jobname LIKE tbtcjob-jobname,

    jobcount LIKE tbtcjob-jobcount,

    is_otf.

    DATA: outbox_flag LIKE sonv-flag VALUE 'X',

    store_flag LIKE sonv-flag,

    delete_flag LIKE sonv-flag,

    owner LIKE soud-usrnam,

    on LIKE sonv-flag VALUE 'X',

    sent_to_all LIKE sonv-flag,

    g_authority LIKE sofa-usracc,

    w_objdes LIKE sood4-objdes.

    DATA: c_file LIKE rlgrap-filename,

    n_spool(6) TYPE n.

    DATA: cancel.

    DATA: desired_type LIKE sood-objtp,

    real_type LIKE sood-objtp,

    attach_type LIKE sood-objtp,

    otf LIKE sood-objtp VALUE 'OTF', " SAPscript Ausgabeformat

    ali LIKE sood-objtp VALUE 'ALI'. " ABAP lists

    *----


    • CONSTANTS

    *----


    CONSTANTS: ou_fol LIKE sofh-folrg VALUE 'O',

    c_objtp LIKE g_document-objtp VALUE 'RAW',

    c_file_ext LIKE g_document-file_ext VALUE 'TXT'.

    =================================================================================

    z_send_email_fax2

    FUNCTION z_faian_mail_fax2.

    *"----


    ""Interface local:

    *" IMPORTING

    *" REFERENCE(SRC_SPOOLID) LIKE TSP01-RQIDENT

    *" REFERENCE(FAX_MAIL_NUMBER) TYPE SO_NAME

    *" REFERENCE(HEADER_MAIL) TYPE SO_OBJ_DES

    *" REFERENCE(OBJECT_TYPE) TYPE SO_ESCAPE

    *" TABLES

    *" LT_BODY_EMAIL STRUCTURE SOLI

    *" EXCEPTIONS

    *" ERR_NO_ABAP_SPOOLJOB

    *"----


    • Fist part: Verify if the spool really exists

    SELECT SINGLE * FROM tsp01 WHERE rqident = src_spoolid.

    IF sy-subrc NE 0.

    RAISE err_no_abap_spooljob. "doesn't exist

    ELSE.

    client = tsp01-rqclient.

    name = tsp01-rqo1name.

    CALL FUNCTION 'RSTS_GET_ATTRIBUTES'

    EXPORTING

    authority = 'SP01'

    client = client

    name = name

    part = 1

    IMPORTING

    type = type

    objtype = objtype

    EXCEPTIONS

    fb_error = 1

    fb_rsts_other = 2

    no_object = 3

    no_permission = 4

    OTHERS = 5.

    IF objtype(3) = 'OTF'.

    desired_type = otf.

    ELSE.

    desired_type = ali.

    ENDIF.

    CALL FUNCTION 'RSPO_RETURN_SPOOLJOB'

    EXPORTING

    rqident = src_spoolid

    desired_type = desired_type

    IMPORTING

    real_type = real_type

    TABLES

    buffer = l_objcont

    EXCEPTIONS

    no_such_job = 14

    type_no_match = 94

    job_contains_no_data = 54

    no_permission = 21

    can_not_access = 21

    read_error = 54.

    IF sy-subrc EQ 0.

    attach_type = real_type.

    ENDIF.

    CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET'

    EXPORTING

    owner = sy-uname

    region = ou_fol

    IMPORTING

    folder_id = l_folder_id

    EXCEPTIONS

    OTHERS = 5.

    • fill out informations about the header of the email

    CLEAR: g_document.

    g_document-foltp = l_folder_id-foltp.

    g_document-folyr = l_folder_id-folyr.

    g_document-folno = l_folder_id-folno.

    g_document-objtp = c_objtp.

    g_document-objdes = header_mail.

    g_document-file_ext = c_file_ext.

    g_header_data-objdes = header_mail.

    CALL FUNCTION 'SO_DOCUMENT_REPOSITORY_MANAGER'

    EXPORTING

    method = 'SAVE'

    office_user = sy-uname

    IMPORTING

    authority = g_authority

    TABLES

    objcont = lt_body_email

    attachments = lt_attachments

    CHANGING

    document = g_document

    header_data = g_header_data

    EXCEPTIONS

    OTHERS = 1.

    folder_id-objtp = l_folder_id-foltp.

    folder_id-objyr = l_folder_id-folyr.

    folder_id-objno = l_folder_id-folno.

    object_id-objtp = c_objtp.

    object_id-objyr = g_document-objyr.

    object_id-objno = g_document-objno.

    link_folder_id-objtp = l_folder_id-foltp.

    link_folder_id-objyr = l_folder_id-folyr.

    link_folder_id-objno = l_folder_id-folno.

    REFRESH lt_rec_tab.

    • CLEAR lt_rec_tab.

    • lt_rec_tab-sel = 'X'.

    • lt_rec_tab-recesc = object_type. "This field for FAX/MAIL

    • lt_rec_tab-recnam = 'U-'.

    • lt_rec_tab-deliver = 'X'.

    • lt_rec_tab-not_deli = 'X'.

    • lt_rec_tab-read = 'X'.

    • lt_rec_tab-mailstatus = 'E'.

    • lt_rec_tab-adr_name = fax_mail_number.

    • lt_rec_tab-sortfield = fax_mail_number.

    • lt_rec_tab-recextnam = fax_mail_number.

    • lt_rec_tab-sortclass = '5'.

    • APPEND lt_rec_tab.

    lt_rec_tab-recextnam = fax_mail_number.

    lt_rec_tab-recesc = object_type.

    lt_rec_tab-sndart = 'INT'.

    lt_rec_tab-sndpri = 1.

    APPEND lt_rec_tab.

    lt_files-file = c_file.

    APPEND lt_files.

    • begin of insertion by faianf01

    hd_dat-objdes = header_mail.

    CALL FUNCTION 'SO_ATTACHMENT_INSERT'

    EXPORTING

    object_id = object_id

    attach_type = attach_type

    object_hd_change = hd_dat

    owner = sy-uname

    TABLES

    objcont = l_objcont

    objhead = l_objhead

    EXCEPTIONS

    active_user_not_exist = 35

    communication_failure = 71

    object_type_not_exist = 17

    operation_no_authorization = 21

    owner_not_exist = 22

    parameter_error = 23

    substitute_not_active = 31

    substitute_not_defined = 32

    system_failure = 72

    x_error = 1000.

    IF sy-subrc > 0.

    ENDIF.

    • end of insertion by faianf01

    • send email from SAPOFFICE

    CALL FUNCTION 'SO_OBJECT_SEND'

    EXPORTING

    folder_id = folder_id

    object_id = object_id

    outbox_flag = outbox_flag

    link_folder_id = link_folder_id

    owner = sy-uname

    • check_send_authority = 'X'

    TABLES

    receivers = lt_rec_tab

    • note_text = lt_note_text

    EXCEPTIONS

    active_user_not_exist = 35

    communication_failure = 71

    component_not_available = 1

    folder_no_authorization = 5

    folder_not_exist = 6

    forwarder_not_exist = 8

    object_no_authorization = 13

    object_not_exist = 14

    object_not_sent = 15

    operation_no_authorization = 21

    owner_not_exist = 22

    parameter_error = 23

    substitute_not_active = 31

    substitute_not_defined = 32

    system_failure = 72

    too_much_receivers = 73

    user_not_exist = 35.

    ENDIF.

    ENDFUNCTION.

    =================================================================================

    z_send_email_fax

    FUNCTION ZCBFS_SEND_MAIL.

    *"----


    ""Interface local:

    *" IMPORTING

    *" REFERENCE(SRC_SPOOLID) LIKE TSP01-RQIDENT

    *" REFERENCE(HEADER_MAIL) TYPE SO_OBJ_DES

    *" TABLES

    *" LIST_FAX_MAIL_NUMBER STRUCTURE SOLI

    *" EXCEPTIONS

    *" ERR_NO_ABAP_SPOOLJOB

    *"----


    DATA: vg_achou(1) TYPE n.

    • Fist part: Verify if the spool really exists

    vg_achou = 1.

    DO 60 TIMES.

    SELECT SINGLE * FROM tsp01 WHERE rqident = src_spoolid.

    IF sy-subrc IS INITIAL.

    CLEAR vg_achou.

    EXIT.

    ELSE.

    WAIT UP TO 1 SECONDS.

    ENDIF.

    ENDDO.

    IF vg_achou = 1.

    RAISE err_no_abap_spooljob. "doesn't exist

    ENDIF.

    client = tsp01-rqclient.

    name = tsp01-rqo1name.

    CALL FUNCTION 'RSTS_GET_ATTRIBUTES'

    EXPORTING

    authority = 'SP01'

    client = client

    name = name

    part = 1

    IMPORTING

    type = type

    objtype = objtype

    EXCEPTIONS

    fb_error = 1

    fb_rsts_other = 2

    no_object = 3

    no_permission = 4

    OTHERS = 5.

    IF objtype(3) = 'OTF'.

    desired_type = otf.

    ELSE.

    desired_type = ali.

    ENDIF.

    CALL FUNCTION 'RSPO_RETURN_SPOOLJOB'

    EXPORTING

    rqident = src_spoolid

    desired_type = desired_type

    IMPORTING

    real_type = real_type

    TABLES

    buffer = l_objcont

    EXCEPTIONS

    no_such_job = 14

    type_no_match = 94

    job_contains_no_data = 54

    no_permission = 21

    can_not_access = 21

    read_error = 54.

    IF sy-subrc EQ 0.

    attach_type = real_type.

    ENDIF.

    CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET'

    EXPORTING

    owner = sy-uname

    region = ou_fol

    IMPORTING

    folder_id = l_folder_id

    EXCEPTIONS

    OTHERS = 5.

    • fill out informations about the header of the email

    CLEAR: g_document.

    g_document-foltp = l_folder_id-foltp.

    g_document-folyr = l_folder_id-folyr.

    g_document-folno = l_folder_id-folno.

    g_document-objtp = c_objtp.

    g_document-objdes = header_mail.

    g_document-file_ext = c_file_ext.

    g_header_data-objdes = header_mail.

    CALL FUNCTION 'SO_DOCUMENT_REPOSITORY_MANAGER'

    EXPORTING

    method = 'SAVE'

    office_user = sy-uname

    IMPORTING

    authority = g_authority

    TABLES

    attachments = lt_attachments

    CHANGING

    document = g_document

    header_data = g_header_data

    EXCEPTIONS

    OTHERS = 1.

    folder_id-objtp = l_folder_id-foltp.

    folder_id-objyr = l_folder_id-folyr.

    folder_id-objno = l_folder_id-folno.

    object_id-objtp = c_objtp.

    object_id-objyr = g_document-objyr.

    object_id-objno = g_document-objno.

    link_folder_id-objtp = l_folder_id-foltp.

    link_folder_id-objyr = l_folder_id-folyr.

    link_folder_id-objno = l_folder_id-folno.

    REFRESH lt_rec_tab.

    LOOP AT LIST_FAX_MAIL_NUMBER.

    lt_rec_tab-recextnam = LIST_FAX_MAIL_NUMBER-LINE.

    lt_rec_tab-recesc = 'U'.

    lt_rec_tab-sndart = 'INT'.

    lt_rec_tab-sndpri = 1.

    APPEND lt_rec_tab.

    ENDLOOP.

    lt_files-file = c_file.

    APPEND lt_files.

    hd_dat-objdes = header_mail.

    CALL FUNCTION 'SO_ATTACHMENT_INSERT'

    EXPORTING

    object_id = object_id

    attach_type = attach_type

    object_hd_change = hd_dat

    owner = sy-uname

    TABLES

    objcont = l_objcont

    objhead = l_objhead

    EXCEPTIONS

    active_user_not_exist = 35

    communication_failure = 71

    object_type_not_exist = 17

    operation_no_authorization = 21

    owner_not_exist = 22

    parameter_error = 23

    substitute_not_active = 31

    substitute_not_defined = 32

    system_failure = 72

    x_error = 1000.

    IF sy-subrc > 0.

    ENDIF.

    • send email from SAPOFFICE

    CALL FUNCTION 'SO_OBJECT_SEND'

    EXPORTING

    folder_id = folder_id

    object_id = object_id

    outbox_flag = outbox_flag

    link_folder_id = link_folder_id

    owner = sy-uname

    TABLES

    receivers = lt_rec_tab

    note_text = lt_note_text

    EXCEPTIONS

    active_user_not_exist = 35

    communication_failure = 71

    component_not_available = 1

    folder_no_authorization = 5

    folder_not_exist = 6

    forwarder_not_exist = 8

    object_no_authorization = 13

    object_not_exist = 14

    object_not_sent = 15

    operation_no_authorization = 21

    owner_not_exist = 22

    parameter_error = 23

    substitute_not_active = 31

    substitute_not_defined = 32

    system_failure = 72

    too_much_receivers = 73

    user_not_exist = 35.

    ENDFUNCTION.

    =================================================================================

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.