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: 

How to set Automated emails on ABAP job failure (sm37)?

Former Member
0 Kudos

Hello,

Can u please tell me how to How to set Automated emails on ABAP job failure (sm37)?

Scenario:

We have scheduled jobs in SM37.

And at the end of job completion or failure we need to generate automated e mails

Please provide me with the steps.

Thank you,

Rezwan

10 REPLIES 10

Former Member
0 Kudos

Hi,

You can try this solution.

Check for a BADI or User exit for the transaction SM37.

At the end of Job completion, write the coding logic to send the details of the job (Which you can find from the signatureof the BADI or User exit like properties, attributes, etc.).

The job details will be obtained in an internal table. Just use that internal table and try to check for a field which contains the status of the job.

Then write some IF logic.. If jobstatus = completed or jobstatus = cancelled, then intimate the person responsible with a mail.

Write this in the coding logic.

You can use the function module : SO_NEW_DOCUMENT_SEND_API1.

But the problem here is that, COMMIT_WORK in the function module should be set to X. But inside BADI or User exit, you should not do the commit work.

Search for the forum for this alone. I think some person have already requested how to do commit_work in BADI.

Hope this might help you.

Best Regards,

Suresh

Former Member
0 Kudos

Hi asraf

U can write a report which will check for the failed jobs for the specified time back from the current time (status available in the table TBTCO) and prepare a mail from that details, and send it to the required users.

This report has to be scheduled in SM37 as a periodic job so that it will continuosly check and send mails.

The specified time in the report and the periodicity of the backgrouund job should be in par so that u neither miss any detail nor get repated one.

if this condition is ok for u , u can use the following code.

This code will check for the previouus 24 hrs and send mail to the respective users who scheduled the job.

*********

TABLES : TBTCO.

DATA: USER TYPE SO_RECNAME .

DATA : CURR_DT TYPE SY-DATUM,

PERIOD TYPE SY-DATUM.

DATA : BEGIN OF IT_BKST OCCURS 0.

INCLUDE TYPE TBTCO .

DATA : S_DATE(10),

S_TIME(15),

END OF IT_BKST.

DATA J_NAME(50).

CONSTANTS: IN_SPACE(20) VALUE '                '.

CONSTANTS: IN_SPACE2(5) VALUE '    '.

DATA: OBJPACK LIKE SOPCKLSTI1 OCCURS 2 WITH HEADER LINE.

DATA: OBJHEAD LIKE SOLISTI1 OCCURS 1 WITH HEADER LINE.

DATA: OBJBIN LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE.

DATA: OBJTXT LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE.

DATA: RECLIST LIKE SOMLRECI1 OCCURS 5 WITH HEADER LINE.

DATA: DOC_CHNG LIKE SODOCCHGI1.

DATA: TAB_LINES LIKE SY-TABIX.

DATA: PERIOD2(10).

SELECT-OPTIONS: REQ_DT FOR SY-DATUM NO-DISPLAY,

REQ_TIME FOR SY-UZEIT NO-DISPLAY.

INITIALIZATION.

CURR_DT = SY-DATUM.

PERIOD = SY-DATUM - 1.

START-OF-SELECTION.

PERFORM GET_DATA.

IF NOT IT_BKST[] IS INITIAL.

PERFORM SEND_MAIL.

ENDIF.

*&----


*

*& Form get_data

*&----


*

  • text

*----


*

  • --> p1 text

  • <-- p2 text

*----


*

FORM GET_DATA .

SELECT * FROM TBTCO INTO CORRESPONDING FIELDS OF TABLE IT_BKST WHERE STATUS EQ 'A' AND STRTDATE EQ PERIOD.

IF NOT IT_BKST[] IS INITIAL.

SORT IT_BKST[] BY SDLUNAME ASCENDING STRTDATE DESCENDING STRTTIME ASCENDING.

LOOP AT IT_BKST.

CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'

EXPORTING

DATE_INTERNAL = IT_BKST-STRTDATE

IMPORTING

DATE_EXTERNAL = IT_BKST-S_DATE

EXCEPTIONS

DATE_INTERNAL_IS_INVALID = 1

OTHERS = 2.

IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

CONCATENATE IT_BKST-STRTTIME0(2) ':' IT_BKST-STRTTIME2(2) ':' IT_BKST-STRTTIME+4(2) INTO IT_BKST-S_TIME SEPARATED BY SPACE.

MODIFY IT_BKST TRANSPORTING S_DATE S_TIME.

ENDLOOP.

CONCATENATE PERIOD6(2) PERIOD4(2) PERIOD+0(4) INTO PERIOD2 SEPARATED BY '.'.

ENDIF.

ENDFORM. " get_data

*&----


*

*& Form send_mail

*&----


*

  • text

*----


*

  • --> p1 text

  • <-- p2 text

*----


*

FORM SEND_MAIL .

LOOP AT IT_BKST .

CLEAR: OBJTXT[],DOC_CHNG,RECLIST[],OBJPACK[],OBJTXT,DOC_CHNG,RECLIST,OBJPACK.

IF IT_BKST-SDLUNAME EQ USER.

CONTINUE.

ENDIF.

USER = IT_BKST-SDLUNAME.

DOC_CHNG-OBJ_NAME = 'STATUS'.

DOC_CHNG-OBJ_DESCR = 'Canceled Background job(s)' .

OBJTXT = 'Dear Sir/Madam'.

APPEND OBJTXT.

OBJTXT = ' '.

APPEND OBJTXT.

CONCATENATE 'This is to notify that the following background job(s) you''ve scheduled on  ' '  ' PERIOD2 ' has been canceled, please check SM37 for details' INTO OBJTXT.

APPEND OBJTXT.

OBJTXT = ' '.

APPEND OBJTXT.

LOOP AT IT_BKST WHERE SDLUNAME EQ USER.

WRITE IT_BKST-JOBNAME TO J_NAME.

CONCATENATE J_NAME IT_BKST-S_DATE IN_SPACE2 IT_BKST-S_TIME INTO OBJTXT RESPECTING BLANKS.

APPEND OBJTXT.

ENDLOOP.

OBJTXT = ' '.

APPEND OBJTXT.

OBJTXT = 'Thankyou'.

APPEND OBJTXT.

DESCRIBE TABLE OBJTXT LINES TAB_LINES.

READ TABLE OBJTXT INDEX TAB_LINES.

DOC_CHNG-DOC_SIZE = ( TAB_LINES - 1 ) * 255 + STRLEN( OBJTXT ).

CLEAR OBJPACK-TRANSF_BIN.

OBJPACK-HEAD_START = 1.

OBJPACK-HEAD_NUM = 0.

OBJPACK-BODY_START = 1.

OBJPACK-BODY_NUM = TAB_LINES.

OBJPACK-DOC_TYPE = 'RAW'.

  • OBJPACK-TRANSF_BIN = 'X'.

APPEND OBJPACK.

RECLIST-RECEIVER = USER.

RECLIST-REC_TYPE = 'B'.

RECLIST-EXPRESS = 'X'.

APPEND RECLIST.

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

DOCUMENT_DATA = DOC_CHNG

PUT_IN_OUTBOX = 'X'

COMMIT_WORK = 'X'

TABLES

PACKING_LIST = OBJPACK

OBJECT_HEADER = OBJHEAD

  • CONTENTS_BIN = OBJBIN

CONTENTS_TXT = OBJTXT

RECEIVERS = RECLIST

EXCEPTIONS

TOO_MANY_RECEIVERS = 1

DOCUMENT_NOT_SENT = 2

OPERATION_NO_AUTHORIZATION = 4

OTHERS = 99.

WAIT UP TO 2 SECONDS.

IF SY-SUBRC EQ 0.

SUBMIT RSCONN01 WITH MODE = '*'

WITH OUTPUT = ' '

AND RETURN.

ENDIF.

ENDLOOP.

ENDFORM. " send_mail

All the best

Regards

Prabumanoharan

0 Kudos

Hi Prabhu,

Thank you very much .

u can write a report which will check for the failed jobs for the specified time back from the current time (status available in the table TBTCO) and prepare a mail from that details, and send it to the required users. This report has to be scheduled in SM37 as a periodic job so that it will continuosly check and send mails. The specified time in the report and the periodicity of the backgrouund job should be in par so that u neither miss any detail nor get repated one. if this condition is ok for u , u can use the following code.

Can u please provide step by step procudure to acomplish the above

Please help

Regards

Rezwan

0 Kudos

Hi Rezwan

There are only two steps in this

1. Create a report with the coding i've given.

2. Schedule that report periodically(say for every 5 mins.) in SM36.

in this case the report should fetch data for the previous 6 mins from the current time.

Regards

Prabumanoharan

0 Kudos

Hi Prabhu,

I am very new to ABAP

Do i need to login to sm38 and create the job with the script you had provided?

Can u tell the the steps? as i will be doing it on the production directly

Thanks a lot

Rezwan

0 Kudos

Hi Rezwan

Follow this

Creation of report

(a) Create a new report (Executable type) in SE38.

(b) Put the above code in that report, save and activate

Scheduling Background job

(a) Goto SM36

(b) Give in a Job name (any name)

(c) Click on "Start condition" in application tool bar

(d) Select 'Immediate' ( or u can use Date/time to specify a date and time to start)

(e) Check "Periodic Job" and click on Period values and select " Other Period'

(f) Give the required time (say 5 mins) and save

After saving , u'll come to the first screen (if not press BACK button)

(g) there , select "Step" from application tool bar

(h) Give the ABAP report name there and save

press BACK button to come to initial screen

the STATUS will be shown as 'SCHEDULED'

(j) Press 'SAVE' and now the job will turn to RELEASED status

( The job will be executed in background , only if if the status is 'Released' )

thats it your job will be executed ever 5 mins.

Regards

Prabumanoharan

0 Kudos

Hi Asrafali,

First Try with creating program in SE38 as suggested by our friend Prabhu...

then in SM36 scheduled your report...

try doing these steps in development system first..do not do it directly in production system....

With Regards

0 Kudos

Hi ,

Can u please tell me where to include the email address in the code?

Thanks

Rezwan

0 Kudos

Hi

like this.

RECLIST-RECEIVER = <email id>.

RECLIST-REC_TYPE = 'U'.

APPEND RECLIST.

Regards

Prabumanoharan

0 Kudos

Hi, i'm learning workflows and i think what this is the typical case for apply workflows by event when one job is canceled, the bussines object is BPJOB and the event is the ABORTED, controlled by one workflow and to send email to agent recipient, but i don't know as to do this workflow.