02-21-2012 8:20 AM
Hi Folks
I have created an implementation of BAdI HRPAD00INFTY to send an email whenever an infotype is changed from PA40.
I have created a FM to send the email using CL_BCS and it works 100% when run through se37, however when run via the BAdI it doesn't work and the email message doesn't reach tx SOST.
My code is reached through debugging the BAdI and the return code is 0.
Can anyone help explain why the email is generated via se37, but not when called in the BAdI?
THanks
Anton Kruse
02-21-2012 9:20 AM
Hello Anton,
Can you provide the code snippet which you're using to send the email? Is the mail getting generated, can you view it in SOST?
Also, let us know in which method of the BAdI have you put your code?
BR,
Suhas
02-21-2012 9:31 AM
Hi Suhas
The mail only gets generated in SOST when the FM is called directly from se37.
The BAdI code: (method IN_UPDATE)
line_message = 'Hello World'.
APPEND line_message TO body_message.
mail_recipient = '<insert email address>'.
subject = 'Test'.
CALL FUNCTION 'ZSEND_EXT_EMAIL'
EXPORTING
subject = subject
message_body = body_message
sender_mail = mail_recipient
recipient_mail = mail_recipient
IMPORTING
result = send_result.
The FM code:
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(SUBJECT) TYPE SO_OBJ_DES
*" REFERENCE(MESSAGE_BODY) TYPE BCSY_TEXT
*" REFERENCE(SENDER_MAIL) TYPE ADR6-SMTP_ADDR OPTIONAL
*" REFERENCE(RECIPIENT_MAIL) TYPE ADR6-SMTP_ADDR OPTIONAL
*" EXPORTING
*" REFERENCE(RESULT) TYPE BOOLEAN
*"----------------------------------------------------------------------
*Data Declaration
DATA: lo_sender TYPE REF TO if_sender_bcs VALUE IS INITIAL,
l_send TYPE adr6-smtp_addr ,
l_rec TYPE adr6-smtp_addr .
DATA : itab TYPE TABLE OF sval,
ls_itab TYPE sval,
i_return.
DATA:
lo_send_request TYPE REF TO cl_bcs VALUE IS INITIAL.
DATA: lx_document_bcs TYPE REF TO cx_document_bcs VALUE IS INITIAL.
DATA: lo_recipient TYPE REF TO if_recipient_bcs VALUE IS INITIAL.
DATA: lv_recipient_mail TYPE adr6-smtp_addr.
*Prepare Mail Object
CLASS cl_bcs DEFINITION LOAD.
lo_send_request = cl_bcs=>create_persistent( ).
* Message body and subject
DATA: lo_document TYPE REF TO cl_document_bcs VALUE IS INITIAL.
lo_document = cl_document_bcs=>create_document(
i_type = 'RAW'
i_text = message_body
i_subject = subject ).
* Pass the document to send request
lo_send_request->set_document( lo_document ).
TRY.
IF sender_mail IS NOT INITIAL.
lo_sender = cl_cam_address_bcs=>create_internet_address( sender_mail ).
ELSE.
lo_sender = cl_sapuser_bcs=>create( sy-uname ).
ENDIF.
* Set sender
lo_send_request->set_sender(
EXPORTING
i_sender = lo_sender ).
CATCH cx_address_bcs.
RETURN.
ENDTRY.
* Set recipients
IF recipient_mail IS NOT INITIAL.
lo_recipient = cl_cam_address_bcs=>create_internet_address( recipient_mail ).
ELSE.
lo_recipient = cl_sapuser_bcs=>create( sy-uname ).
ENDIF.
lo_send_request->add_recipient(
EXPORTING
i_recipient = lo_recipient
i_express = 'X' ).
TRY.
** Send email
lo_send_request->send(
EXPORTING
i_with_error_screen = 'X'
RECEIVING
result = result ).
COMMIT WORK.
CATCH cx_send_req_bcs.
result = ''.
ENDTRY.
02-21-2012 9:47 AM
Hello Anton,
Couple of points:
TRY.
** Send email
lo_send_request->send(
EXPORTING
i_with_error_screen = 'X'
RECEIVING
result = result ).
COMMIT WORK.
CATCH cx_send_req_bcs.
result = ''.
ENDTRY.
You should never issue a COMMIT WORK within a BAdI as this might interfere with the current LUW.
From the documentation of the method IF_EX_HRPAD00INFTY~IN_UPDATE, i understand it is called in an update task. If this is the case, then any statement which triggers a DB commit is forbidden.
BR,
Suhas
02-21-2012 9:56 AM
02-21-2012 12:08 PM
Hi Anton,
The below example should provide you the solution:
*// Call the function module to send mail notification
CALL FUNCTION 'ZHR_EMAIL_SEND_NOTIFCATION' IN BACKGROUND TASK AS SEPARATE UNIT
EXPORTING
im_pernr = ipspar-pernr
im_massn = ipspar-massn
im_massg = ipspar-massg
im_begda = ipspar-begda
im_endda = ipspar-endda.
The catch is calling the FM which actually build and send the mail as a SEPERATE UNIT which ensures its a different Logical Unit of Work
Regards
Raj
02-21-2012 12:27 PM
Hi Raj
That makes sense.
Please could you send me the code behind your email sending FM.
Thanks
02-21-2012 12:36 PM
Ok so I finally managed to get it to work.
Raj's suggestion help a lot, but then I also needed to set the FM as remote enabled.
01-28-2015 12:03 PM
I got similar kind of issue , and Rajesh's Suggestion of calling the FM in BACKGROUND TASK AS SEPARATE UNIT has resolved my problem.
But still i dont understand why we do so, can you explain the reason behind the same.
Regards,
Khushbu
02-21-2012 12:38 PM
Hi Anton,
Here is the code ssnippet.
Regards
Raj
FUNCTION zhr_email_send_notifcation.
*"----
""Local Interface:
*" IMPORTING
*" VALUE(IM_PERNR) TYPE PERNR_D
*" VALUE(IM_MASSN) TYPE MASSN
*" VALUE(IM_MASSG) TYPE MASSG
*" VALUE(IM_BEGDA) TYPE BEGDA
*" VALUE(IM_ENDDA) TYPE ENDDA
*"----
*======================================================================
Function/Purpose: We need to provide e-mail notification to the
supervisor and HRBP when an employee is placed on
leave (inactive status) and when the employee is
returned to active status.
I/O:
*
*======================================================================
&----
Type *
&----
TYPES: BEGIN OF ty_reason,
massn TYPE massn, "Action
massg TYPE massg, "Reason for Action
mgtxt TYPE mgtxt, "Reason for Action Text
END OF ty_reason.
&----
Global Data Declarations *
&----
DATA: lf_massn TYPE massn, "Action Type
lf_act_txt TYPE mntxt, "Action text
lf_massg TYPE massg, "Reason for Action
lf_str TYPE string, "String variable
lf_str1 TYPE string, "String variable
lf_hrp1001 TYPE hrp1001,
lf_st_dt(10) TYPE c, "Effective Date
lf_position TYPE objid, "Position
lf_smtp_addr TYPE adr6-smtp_addr,
lv_btrtl TYPE btrtl, "Personnel Subarea
lv_dlinam TYPE so_dli_nam, "Distribution List
lv_mlrec TYPE so_obj_nam, "Distribution List
lf_pernr TYPE prelp-pernr, "Employee number
lf_mpernr TYPE prelp-pernr, "Manager Employee number
lf_hrbp_mail TYPE string, "HRBP EmailID
lf_mmail TYPE string, "Manager EmailID
lf_ename TYPE pad_cname, "Employee name
lf_fname TYPE vorna, "First name
lf_lname TYPE nachn. "Last name
&----
Constants *
&----
CONSTANTS:
c_endda TYPE sy-datum VALUE '99991231',
c_0000 TYPE infty VALUE '0000',
c_0001 TYPE infty VALUE '0001',
c_0002 TYPE infty VALUE '0002',
c_0006 TYPE infty VALUE '0006',
c_0105 TYPE infty VALUE '0105',
c_us TYPE land1 VALUE 'US', "USA
c_ca TYPE land1 VALUE 'CA', "Canada
c_usmail TYPE string VALUE '<EMAIL>', "#EC NOTEXT
c_camail TYPE c VALUE '<EMAIL>', "#EC NOTEXT
c_x(1) TYPE c VALUE 'X',
c_coma(1) TYPE c VALUE ',',
c_period(1) TYPE c VALUE '.',
c_req_sts TYPE bcs_rqst VALUE 'E',
c_space TYPE abap_char1 VALUE cl_abap_char_utilities=>horizontal_tab,
c_z2 TYPE massn VALUE 'Z2', "Paid Leave
c_z3 TYPE massn VALUE 'Z3', "Unpaid Leave
c_z4 TYPE massn VALUE 'Z4'. "Return from Leave
&----
Internal Tables *
&----
DATA: t_reason TYPE STANDARD TABLE OF ty_reason WITH HEADER LINE,
lw_reason TYPE ty_reason,
t_p0001 TYPE TABLE OF p0001 WITH HEADER LINE,
lw_p0001 TYPE p0001,
t_p0002 TYPE TABLE OF p0002 WITH HEADER LINE,
lw_p0002 TYPE p0002,
t_p0006 TYPE TABLE OF p0006 WITH HEADER LINE,
lw_p0006 TYPE p0006,
t_p0105 TYPE TABLE OF p0105 WITH HEADER LINE,
lw_p0105 TYPE p0105.
*// Mailing related declarations
DATA: send_request TYPE REF TO cl_bcs,
document TYPE REF TO cl_document_bcs,
sender TYPE REF TO cl_sapuser_bcs,
recipient TYPE REF TO if_recipient_bcs,
exception_info TYPE REF TO if_os_exception_info,
bcs_exception TYPE REF TO cx_bcs.
DATA: status_mail TYPE bcs_stml.
DATA: conlength TYPE i,
conlengths TYPE so_obj_len.
DATA: l_mailtext TYPE bcsy_text, "soli_tab,
subject TYPE so_obj_des.
lf_massn = im_massn. "Action
lf_massg = im_massg. "Action Reason
*// Employee number
lf_pernr = im_pernr.
IF im_endda IS INITIAL.
im_endda = c_endda.
ENDIF.
SET COUNTRY 'US'.
*// Get action text
SELECT SINGLE mntxt
INTO lf_act_txt
FROM t529t
WHERE sprsl = sy-langu AND
massn = lf_massn.
*// Get all reason texts into t_reason
SELECT
r~massn
r~massg
t~mgtxt
INTO TABLE t_reason
FROM t530 AS r INNER JOIN
t530t AS t ON
rmassn = tmassn AND
rmassg = tmassg
WHERE
t~sprsl = sy-langu AND
r~massn IN (c_z2, c_z3, c_z4).
IF sy-subrc = 0.
SORT t_reason BY massn massg. "Sort on Action and Reason for Action
ENDIF.
*// Based on employee country key populate the EmailID of HRBP
CALL FUNCTION 'HR_READ_INFOTYPE'
EXPORTING
tclas = 'A'
pernr = lf_pernr
infty = c_0006
begda = im_begda
endda = im_endda
TABLES
infty_tab = t_p0006
EXCEPTIONS
infty_not_found = 1
OTHERS = 2.
IF sy-subrc = 0.
READ TABLE t_p0006 INTO lw_p0006 INDEX 1.
IF sy-subrc = 0.
IF lw_p0006-land1 = c_us.
lf_hrbp_mail = c_usmail.
ELSEIF lw_p0006-land1 = c_ca.
lf_hrbp_mail = c_camail.
ENDIF.
ENDIF.
ENDIF.
*// Get the employee name
CALL FUNCTION 'HR_READ_INFOTYPE'
EXPORTING
tclas = 'A'
pernr = lf_pernr
infty = c_0002
begda = im_begda
endda = im_endda
TABLES
infty_tab = t_p0002
EXCEPTIONS
infty_not_found = 1
OTHERS = 2.
IF sy-subrc = 0.
*// Employee name
READ TABLE t_p0002 INTO lw_p0002 INDEX 1.
IF sy-subrc = 0.
lf_fname = lw_p0002-vorna.
lf_lname = lw_p0002-nachn.
CONCATENATE lf_fname lf_lname INTO lf_ename SEPARATED BY space.
ENDIF.
ENDIF.
*// Get the supervisor EmailID
CALL FUNCTION 'HR_READ_INFOTYPE'
EXPORTING
tclas = 'A'
pernr = lf_pernr
infty = c_0001
begda = im_begda
endda = im_endda
TABLES
infty_tab = t_p0001
EXCEPTIONS
infty_not_found = 1
OTHERS = 2.
IF sy-subrc = 0.
*// Get position of the employee
READ TABLE t_p0001 INTO lw_p0001 INDEX 1.
IF sy-subrc = 0.
SELECT *
INTO lf_hrp1001
FROM hrp1001 UP TO 1 ROWS
WHERE otype = 'O' AND
objid = lw_p0001-orgeh AND
rsign = 'B' AND
relat = '012'.
ENDSELECT.
IF sy-subrc = 0.
lf_position = lf_hrp1001-sobid.
CLEAR lf_hrp1001.
*// Get Manager employee number
SELECT *
INTO lf_hrp1001
FROM hrp1001 UP TO 1 ROWS
WHERE otype = 'S' AND
objid = lf_position AND
rsign = 'A' AND
relat = '008'.
ENDSELECT.
IF sy-subrc = 0.
lf_mpernr = lf_hrp1001-sobid.
ENDIF.
ENDIF.
ENDIF.
*// Get EmailID from PA0105 (USRTY = 0010)
IF NOT lf_mpernr IS INITIAL.
CALL FUNCTION 'HR_READ_INFOTYPE'
EXPORTING
tclas = 'A'
pernr = lf_mpernr
infty = c_0105
begda = im_begda
endda = im_endda
TABLES
infty_tab = t_p0105
EXCEPTIONS
infty_not_found = 1
OTHERS = 2.
IF sy-subrc = 0.
*// Manager EmailID
READ TABLE t_p0105 INTO lw_p0105 INDEX 1.
IF sy-subrc = 0.
lf_mmail = lw_p0105-usrid_long.
CONDENSE lf_mmail NO-GAPS.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
*// If supervisor mailId is initial, set it to <EMAIL>.
IF lf_mmail IS INITIAL.
lf_mmail = lf_hrbp_mail.
ENDIF.
*// Prepare and send the Email
TRY.
*// Create persistent send request
send_request = cl_bcs=>create_persistent( ).
*// Get sender object
sender = cl_sapuser_bcs=>create( sy-uname ).
*// Add sender
CALL METHOD send_request->set_sender
EXPORTING
i_sender = sender.
*// Add recipients
IF NOT lf_hrbp_mail IS INITIAL.
TRY.
CLEAR lf_smtp_addr.
lf_smtp_addr = lf_hrbp_mail.
recipient = cl_cam_address_bcs=>create_internet_address( lf_smtp_addr ).
CALL METHOD send_request->add_recipient
EXPORTING
i_recipient = recipient
i_express = c_x.
CATCH cx_send_req_bcs .
ENDTRY.
ENDIF.
IF NOT lf_mmail IS INITIAL.
TRY.
CLEAR lf_smtp_addr.
lf_smtp_addr = lf_mmail.
recipient = cl_cam_address_bcs=>create_internet_address( lf_smtp_addr ).
CALL METHOD send_request->add_recipient
EXPORTING
i_recipient = recipient
i_express = c_x.
CATCH cx_send_req_bcs .
ENDTRY.
ENDIF.
*// Get the Personnel Subarea (BTRTL) for the employee
SELECT btrtl
INTO lv_btrtl
FROM pa0001 UP TO 1 ROWS
WHERE pernr = lf_pernr AND
endda = im_endda.
ENDSELECT.
IF sy-subrc = 0.
*// Get the distribution list from table ZHR_PERS_LIST based on BTRTL
SELECT SINGLE dlinam
INTO lv_dlinam
FROM zhr_pers_list
WHERE btrtl = lv_btrtl.
IF sy-subrc = 0.
*// Add the mailIDs from the distribution list.
lv_mlrec = lv_dlinam.
TRY.
recipient = cl_distributionlist_bcs=>getu_persistent(
i_dliname = lv_mlrec
i_private = space ).
CALL METHOD send_request->add_recipient
EXPORTING
i_recipient = recipient
i_express = c_x.
CATCH cx_send_req_bcs .
ENDTRY.
ENDIF.
ENDIF.
*// Build the mail content
CLEAR: lf_str, lw_reason.
IF lf_massn = c_z2 OR lf_massn = c_z3.
READ TABLE t_reason INTO lw_reason WITH KEY massn = lf_massn
massg = lf_massg BINARY SEARCH.
IF sy-subrc = 0.
lf_str = lw_reason-mgtxt.
ENDIF.
ELSEIF lf_massn = c_z4.
lf_str = 'Returned to work'(003).
ENDIF.
CONCATENATE lf_str1 '<table cellspacing="1" cellpadding="1" border="0">'(007)
INTO lf_str1.
CONCATENATE lf_str1 '<tbody>'(008) INTO lf_str1.
CONCATENATE lf_str1 '<tr>'(013) 'Name:'(001) c_space '<strong>'(009) lf_ename '</strong>'(010) '</tr>'(014) INTO lf_str1.
CONCATENATE lf_str1 '<tr>'(013) 'Employee ID#:'(002) c_space '<strong>'(009) lf_pernr '</strong>'(010) '</tr>'(014) INTO lf_str1.
CONCATENATE lf_str1 '<tr>'(013) 'Status:'(004) c_space '<strong>'(009) lf_str '</strong>'(010) '</tr>'(014) INTO lf_str1.
WRITE im_begda TO lf_st_dt.
CONCATENATE lf_str1 '<tr>'(013) 'Effective Date:'(005) c_space '<strong>'(009) lf_st_dt '</strong>'(010) '</tr>'(014) INTO lf_str1.
CLEAR lf_str.
CONCATENATE '<tr>'(013) 'If you have any questions, please contact:'(012) lf_hrbp_mail '</tr>'(014) INTO lf_str.
CONCATENATE lf_str1 lf_str INTO lf_str1.
***End of Table
CONCATENATE lf_str1 '</tbody></table>'(011) INTO lf_str1.
conlength = strlen( lf_str1 ).
conlengths = conlength.
CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
EXPORTING
text = lf_str1
TABLES
ftext_tab = l_mailtext.
l_mailtext = cl_document_bcs=>string_to_soli( lf_str1 ).
*// Subject
CONCATENATE 'Email notification on Employee:'(006) lf_ename INTO subject SEPARATED BY space.
CLEAR document.
document = cl_document_bcs=>create_document(
i_type = 'HTM'
i_text = l_mailtext
i_length = conlengths
i_subject = subject ).
*// Add document to send request
CALL METHOD send_request->set_document( document ).
*// Set that you dont need a return status mail
status_mail = c_req_sts.
CALL METHOD send_request->set_status_attributes
EXPORTING
i_requested_status = c_req_sts
i_status_mail = status_mail.
*// Set send immediately
send_request->set_send_immediately( c_x ).
*// Send the document
CALL METHOD send_request->send( ).
*// Commit work
COMMIT WORK.
*// Catch the exceptions raised
CATCH cx_bcs INTO bcs_exception.
ENDTRY.
ENDFUNCTION.
Edited by: Rajasekhar Dinavahi on Feb 21, 2012 6:13 PM