Immediately send the spool to SAP user after a background run

* After running the background jobs, send the spool to the SAP users Office Mail Box.

REPORT ZMAILSPOOL.

INCLUDE LBTCHDEF.
*
* Include the Business Object Repository object
INCLUDE <CNTN01>.

DATA ABAPNM LIKE SY-REPID.

DATA BEGIN OF LOCAL_JOB.
INCLUDE STRUCTURE TBTCJOB.
DATA END OF LOCAL_JOB.

DATA BEGIN OF LOCAL_STEP_TBL OCCURS 10.
INCLUDE STRUCTURE TBTCSTEP.
DATA END OF LOCAL_STEP_TBL.

*
* Data declarations for the mail recipient
DATA RECIPIENT TYPE SWC_OBJECT.
DATA RECIPIENT_OBJ LIKE SWOTOBJID.
SWC_CONTAINER CONTAINER.

* Data declarations for the background job
PARAMETERS ABAPNAME(20) DEFAULT 'ZTEST1'.
PARAMETERS EMPFNAME LIKE SY-UNAME DEFAULT SY-UNAME.

ABAPNM = ABAPNAME.
LOCAL_JOB-JOBNAME = ABAPNAME.
* Generate recipient object (see report RSSOKIF1 for an example)
*** 1. Create the recipient:

IF EMPFNAME <> SPACE.
"** 1.1 Generate an object reference to a recipient object
SWC_CREATE_OBJECT RECIPIENT 'RECIPIENT' SPACE.

"** 1.2 Write the import parameters for method
"** recipient.createaddress into the container
"** and empty the container
SWC_CLEAR_CONTAINER CONTAINER.
"** Set address element (internal user 'JSMITH')
SWC_SET_ELEMENT CONTAINER 'AddressString' EMPFNAME.
"** Set address type (internal user)
SWC_SET_ELEMENT CONTAINER 'TypeId' 'B'.

"** 1.3 Call the method recipient.createaddress
SWC_CALL_METHOD RECIPIENT 'CreateAddress' CONTAINER.
"** Issue any error message generated by a method exception
IF SY-SUBRC NE 0.
MESSAGE ID SY-MSGID TYPE 'E' NUMBER SY-MSGNO.
ENDIF.
SWC_CALL_METHOD RECIPIENT 'Save' CONTAINER.
SWC_OBJECT_TO_PERSISTENT RECIPIENT RECIPIENT_OBJ.
ENDIF.
*** Recipient has been generated and is ready for use in a
*** background job

CALL FUNCTION 'JOB_OPEN'
     exporting
*      DELANFREP              = ' '
*      JOBGROUP               = ' '
       jobname                = LOCAL_JOB-JOBNAME
*      SDLSTRTDT              = NO_DATE
*      SDLSTRTTM              = NO_TIME
     IMPORTING
       JOBCOUNT               = LOCAL_JOB-JOBCOUNT
     EXCEPTIONS
       CANT_CREATE_JOB        = 1
       INVALID_JOB_DATA       = 2
       JOBNAME_MISSING        = 3
       OTHERS                 = 4
             .
   if sy-subrc <> 0.
      WRITE: / 'JOB_OPEN PROBLEM ', SY-SUBRC.
   ELSE.
      WRITE: / 'JOB OPEN SUCCESS',
                                  LOCAL_JOB-JOBNAME, LOCAL_JOB-JOBCOUNT.
   endif.

CALL FUNCTION 'JOB_SUBMIT'
     exporting
*      ARCPARAMS                         =
       authcknam                         = SY-UNAME
*      COMMANDNAME                       = ' '
*      OPERATINGSYSTEM                   = ' '
*      EXTPGM_NAME                       = ' '
*      EXTPGM_PARAM                      = ' '
*      EXTPGM_SET_TRACE_ON               = ' '
*      EXTPGM_STDERR_IN_JOBLOG           = 'X'
*      EXTPGM_STDOUT_IN_JOBLOG           = 'X'
*      EXTPGM_SYSTEM                     = ' '
*      EXTPGM_RFCDEST                    = ' '
*      EXTPGM_WAIT_FOR_TERMINATION       = 'X'
       jobcount                          = LOCAL_JOB-JOBCOUNT
       jobname                           = LOCAL_JOB-JOBNAME
*      LANGUAGE                          = SY-LANGU
*      PRIPARAMS                         = ' '
       REPORT                            = ABAPNM
*      REPORT                            = 'ZTEST1'
*      VARIANT                           = ' '
*    IMPORTING
*      STEP_NUMBER                       = LOCAL_JOB-STEPCOUNT
     EXCEPTIONS
       BAD_PRIPARAMS                     = 1
       BAD_XPGFLAGS                      = 2
       INVALID_JOBDATA                   = 3
       JOBNAME_MISSING                   = 4
       JOB_NOTEX                         = 5
       JOB_SUBMIT_FAILED                 = 6
       LOCK_FAILED                       = 7
       PROGRAM_MISSING                   = 8
       PROG_ABAP_AND_EXTPG_SET           = 9
       OTHERS                            = 10
             .
   if sy-subrc <> 0.
      WRITE: / 'JOB_SUBMIT PROBLEM ', LOCAL_JOB-JOBCOUNT,
               LOCAL_JOB-JOBNAME, ABAPNAME, SY-SUBRC.
   endif.

IF EMPFNAME <> SPACE.
CALL FUNCTION 'JOB_CLOSE'
  exporting
*   AT_OPMODE                         = ' '
*   AT_OPMODE_PERIODIC                = ' '
*   CALENDAR_ID                       = ' '
*   EVENT_ID                          = ' '
*   EVENT_PARAM                       = ' '
*   EVENT_PERIODIC                    = ' '
    jobcount                          = LOCAL_JOB-JOBCOUNT
    jobname                           = LOCAL_JOB-JOBNAME
*   LASTSTRTDT                        = NO_DATE
*   LASTSTRTTM                        = NO_TIME
*   PRDDAYS                           = 0
*   PRDHOURS                          = 0
*   PRDMINS                           = 0
*   PRDMONTHS                         = 0
*   PRDWEEKS                          = 0
*   PREDJOB_CHECKSTAT                 = ' '
*   PRED_JOBCOUNT                     = ' '
*   PRED_JOBNAME                      = ' '
*   SDLSTRTDT                         = NO_DATE
*   SDLSTRTTM                         = NO_TIME
*   STARTDATE_RESTRICTION             = BTC_PROCESS_ALWAYS
    STRTIMMED                         = 'X'
*   TARGETSYSTEM                      = ' '
*   START_ON_WORKDAY_NOT_BEFORE       = SY-DATUM
*   START_ON_WORKDAY_NR               = 0
*   WORKDAY_COUNT_DIRECTION           = 0
    RECIPIENT_OBJ                     = RECIPIENT_OBJ
*   TARGETSERVER                      = ' '
*   DONT_RELEASE                      = ' '
* IMPORTING
*   JOB_WAS_RELEASED                  = 'X'
  EXCEPTIONS
    CANT_START_IMMEDIATE              = 1
    INVALID_STARTDATE                 = 2
    JOBNAME_MISSING                   = 3
    JOB_CLOSE_FAILED                  = 4
    JOB_NOSTEPS                       = 5
    JOB_NOTEX                         = 6
    LOCK_FAILED                       = 7
    OTHERS                            = 8
          .

IF ( SY-SUBRC <> 0 ).
  WRITE: / 'Job_Close Problem:', ABAPNAME, SY-SUBRC.
ELSE.
  WRITE: / 'Job_Close done'.
ENDIF.

ELSE. "no empfname
...
ENDIF.

More Function Module
Functions / SAP Script / ALV

Tables
Database Table

ABAP Books List
ABAP/4 Certification, Programming, Smartforms, Sapscripts and Object Oriented Programming Books

Smart Forms
SAP Smartforms

ABAP Menu:
ABAP Example Hints and Tips

Return to Index:-
SAP ABAP/4 Programming, Basis Administration, Configuration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.  The site www.gotothings.com is in no way affiliated with SAP AG.
Any unauthorised copying or mirroring is prohibited.