Send background job failure alerts to users by e-mail
The following code will scan for any failure in background jobs and send a email to the respective users with the job logs.
The following code will scan for any failure in background jobs and send a email to the respective users with the...
Continue Reading This Article
Enjoy this article as well as all of our content, including E-Guides, news, tips and more.
job logs. Based on entered time with time unit, program searches for any failed job.
Input: Frequency: (hours/minutes) unit : 'H' for hours 'MIN' for minutes For standard error message, create a standard text with name 'ZERROR_MAIL'
The code was written in version 4.5b, but should work in 4.6x as well.
REPORT YBATCH_JOB_CHECK . *&--------------------------------------------------------------------&* *& Program : ybatch_job_check &* *& Description : To alert users of any aborted background jobs &* *& Developed by : K.T.Dhorai &* *& on : 17.07.2002 &* *& Approved by : &* *& on : &* *& Version : 1.0 &* *&--------------------------------------------------------------------&* SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-S01. PARAMETERS: FREQ_TM(4) TYPE N OBLIGATORY DEFAULT '30', FREQ_UNT LIKE T006-MSEHI OBLIGATORY DEFAULT 'MIN'. SELECTION-SCREEN END OF BLOCK B1. DATA: JOB_SEL LIKE BTCSELECT, J_LIST LIKE TBTCJOB OCCURS 10 WITH HEADER LINE. INITIALIZATION. AUTHORITY-CHECK OBJECT 'S_BTCH_ADM' ID 'BTCADMIN' FIELD 'Y'. IF NOT SY-SUBRC IS INITIAL. MESSAGE E107(Z1) WITH TEXT-E03. ENDIF. START-OF-SELECTION. PERFORM READ_DATA. PERFORM SEND_MAIL_WITH_JOBLOG. *&---------------------------------------------------------------------* *& Form READ_DATA *&---------------------------------------------------------------------* FORM READ_DATA. DATA: WILDCARD(1) VALUE '*', TIM LIKE JOB_SEL-FROM_TIME, TIM_ELAPSED LIKE SY-UZEIT, DURATION TYPE F. * tim = sy-uzeit - s-tm. CLEAR: JOB_SEL. JOB_SEL-JOBNAME = WILDCARD. *---Job search end date and time (Set as run date and time) JOB_SEL-TO_DATE = SY-DATUM. JOB_SEL-TO_TIME = SY-UZEIT. *---From the input set the start date and time DURATION = FREQ_TM. CALL FUNCTION 'COPF_DETERMINE_START_TIME' EXPORTING I_END_DATE = JOB_SEL-TO_DATE I_END_TIME = JOB_SEL-TO_TIME I_DURATION = DURATION I_UNIT_OF_DURATION = FREQ_UNT IMPORTING E_START_DATE = JOB_SEL-FROM_DATE E_START_TIME = JOB_SEL-FROM_TIME EXCEPTIONS EXCEPTION_RAISED = 1 OTHERS = 2. IF SY-SUBRC <> 0. MESSAGE E107(Z1) WITH TEXT-E05. ENDIF. JOB_SEL-USERNAME = WILDCARD. JOB_SEL-ABORTED = 'X'. CALL FUNCTION 'BP_JOB_SELECT' EXPORTING JOBSELECT_DIALOG = 'N' JOBSEL_PARAM_IN = JOB_SEL TABLES JOBSELECT_JOBLIST = J_LIST EXCEPTIONS INVALID_DIALOG_TYPE = 1 JOBNAME_MISSING = 2 NO_JOBS_FOUND = 3 SELECTION_CANCELED = 4 USERNAME_MISSING = 5 OTHERS = 6. IF SY-SUBRC <> 0. CASE SY-SUBRC. WHEN '3'. MESSAGE I107(Z1) WITH TEXT-I01. EXIT. WHEN OTHERS. MESSAGE E107(Z1) WITH TEXT-E01. ENDCASE. ENDIF. ENDFORM. " READ_DATA *&---------------------------------------------------------------------* *& Form SEND_MAIL_WITH_JOBLOG *&---------------------------------------------------------------------* FORM SEND_MAIL_WITH_JOBLOG. DATA: J_LOG LIKE TBTC5 OCCURS 0 WITH HEADER LINE, MAIL_SUBJECT LIKE SODOCCHGI1-OBJ_DESCR, MSG_LIST LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE, WA LIKE SOLISTI1, USER_LIST LIKE ZUSRNAM OCCURS 0 WITH HEADER LINE, ST_TXT LIKE TLINE OCCURS 0 WITH HEADER LINE. CHECK NOT J_LIST[] IS INITIAL. LOOP AT J_LIST. REFRESH: J_LOG. CLEAR J_LOG. CALL FUNCTION 'BP_JOBLOG_READ' EXPORTING CLIENT = SY-MANDT JOBCOUNT = J_LIST-JOBCOUNT JOBLOG = J_LIST-JOBLOG JOBNAME = J_LIST-JOBNAME TABLES JOBLOGTBL = J_LOG EXCEPTIONS CANT_READ_JOBLOG = 1 JOBCOUNT_MISSING = 2 JOBLOG_DOES_NOT_EXIST = 3 JOBLOG_IS_EMPTY = 4 JOBLOG_NAME_MISSING = 5 JOBNAME_MISSING = 6 JOB_DOES_NOT_EXIST = 7 OTHERS = 8. CHECK SY-SUBRC IS INITIAL. REFRESH: MSG_LIST. CLEAR MSG_LIST. LOOP AT J_LOG. *------Read the Standard Mail contents from Standard Text "ZERROR_MAIL" AT FIRST. CALL FUNCTION 'READ_TEXT' EXPORTING CLIENT = SY-MANDT ID = 'ST' LANGUAGE = SY-LANGU NAME = 'ZERROR_MAIL' OBJECT = 'TEXT' TABLES LINES = ST_TXT EXCEPTIONS ID = 1 LANGUAGE = 2 NAME = 3 NOT_FOUND = 4 OBJECT = 5 REFERENCE_CHECK = 6 WRONG_ACCESS_TO_ARCHIVE = 7 OTHERS = 8. IF SY-SUBRC <> 0. MESSAGE E107(Z1) WITH TEXT-E04. ELSE. LOOP AT ST_TXT. CLEAR WA. MOVE: ST_TXT-TDLINE TO WA. APPEND WA TO MSG_LIST. ENDLOOP. DO 2 TIMES. MOVE SPACE TO WA. APPEND WA TO MSG_LIST. CLEAR WA. ENDDO. CONCATENATE: 'JOBNAME:' SPACE J_LIST-JOBNAME INTO WA. APPEND WA TO MSG_LIST. CLEAR WA. ENDIF. ENDAT. *-----Write the job log into the mail CLEAR WA. WRITE: J_LOG-ENTERDATE TO WA+0(10) DD/MM/YYYY, J_LOG-ENTERTIME TO WA+12(8). MOVE: J_LOG-TEXT TO WA+22(200). APPEND WA TO MSG_LIST. *-----Mail Footer AT LAST. CLEAR WA. MOVE SPACE TO WA. APPEND WA TO MSG_LIST. CLEAR WA. MOVE TEXT-004 TO WA. APPEND WA TO MSG_LIST. CLEAR WA. ENDAT. ENDLOOP. *----User list REFRESH: USER_LIST. CLEAR USER_LIST. MOVE:J_LIST-SDLUNAME TO USER_LIST. APPEND USER_LIST. CLEAR USER_LIST. *---Preparing mail subject. CONCATENATE: TEXT-001 ': ' J_LIST-JOBNAME INTO MAIL_SUBJECT. CALL FUNCTION 'Z_SAP_2_INTMAIL_WITH_ATTACHMNT' EXPORTING MAIL_SUBJECT = MAIL_SUBJECT FILE_NAME = 'ALERT' TABLES SAPIDLIST = USER_LIST MSG_CONTENTS = MSG_LIST EXCEPTIONS SAP_ID_LIST_IS_EMPTY = 1 INTERNET_ADRESS_LIST_EMPTY = 2 ATTACHMENT_FILETYPE_REQUIRED = 3 FILE_TYPE_NOT_SUPPORTED = 4 UNKNOWN_ERROR_SENDING_MAIL = 5 AUTHORIZATION_FAILURE = 6 NOT_AUTH_SENDING_TO_MAILDOMAIN = 7 OTHERS = 8. IF SY-SUBRC <> 0. * message e107(z1) with text-e02. ENDIF. ENDLOOP. ENDFORM. " SEND_MAIL_WITH_JOBLOG *--------------------------------------------------------------* */Following are the text elements used in this program */Cut and paste in your program 001 SAP Background Job Failure Alert 002 From Date 003 Time Period (mins) 004 (This message is automatically generated. Need no reply) E01 Error in reading Job Status E02 Error in sending mail alert. E03 You are not authorized to use this program. E04 Maintain St.Text "ERROR_MAIL" for mail contents E05 Error calculating start time of search run.. I01 No Failed jobs found.. Exiting alert process S01 Job Failure Alert *---------------------------------------------------------------* *For internet mailing part, use standard mailing routines.... *---------------------------------------------------------------*