Skip to Content
-1
Jan 09, 2023 at 02:43 PM

I want to send the data of internal table i_final to external email id in attachments

123 Views Last edit Jan 10, 2023 at 10:21 PM 3 rev

Can someone help me with below code.

TABLES : zbw_pc_schedule,zbw_processname,zbw_pc_threshold, rspclogchain, rspcprocesslog. " TABLES USED FOR THIS PROGRAM ARE RSPCLOGCHAIN AND RSPCPROCESSLOG.

TYPES: BEGIN OF ty_rspclogchain,
chain_id TYPE rspclogchain-chain_id,
log_id TYPE rspclogchain-log_id,
draw_variant TYPE rspclogchain-draw_variant,
datum TYPE rspclogchain-datum,
zeit TYPE rspclogchain-zeit,
analyzed_status TYPE rspclogchain-analyzed_status,
END OF ty_rspclogchain,
BEGIN OF ty_rspcprocesslog,
chain_id TYPE rspclogchain-chain_id,
log_id TYPE rspcprocesslog-log_id,
type TYPE rspcprocesslog-type,
event_start TYPE rspcprocesslog-event_start,
batchdate TYPE rspcprocesslog-batchdate,
batchtime TYPE rspcprocesslog-batchtime,
variante TYPE rspcprocesslog-variante,
actual_state TYPE rspcprocesslog-actual_state,
event_end TYPE rspcprocesslog-event_end,
starttimestamp TYPE rspcprocesslog-starttimestamp,
endtimestamp TYPE rspcprocesslog-endtimestamp,
END OF ty_rspcprocesslog,
BEGIN OF ty_zbw_processname,
process_name TYPE zbw_processname-process_name,
END OF ty_zbw_processname,
BEGIN OF ty_zbw_pc_schedule,
process_chain TYPE zbw_pc_schedule-process_chain,
process_name TYPE zbw_pc_schedule-process_name,
schedule_day TYPE zbw_pc_schedule-schedule_day,
schedule_time TYPE zbw_pc_schedule-schedule_time,
END OF ty_zbw_pc_schedule,
BEGIN OF ty_zbw_pc_threshold,
process_name TYPE zbw_pc_threshold-process_name,
threshold TYPE zbw_pc_threshold-threshold,
END OF ty_zbw_pc_threshold,
BEGIN OF ty_final,
chain_id TYPE rspclogchain-chain_id,
start_date TYPE SY-DATUM,
start_time TYPE SY-UZEIT,
end_date TYPE SY-DATUM,
end_time TYPE SY-UZEIT,
duration TYPE SY-TABIX,
actual_state TYPE rspcprocesslog-actual_state,
status TYPE CHAR50,

END OF ty_final,
BEGIN OF ty_charfinal,
chain_id(25) TYPE C,
start_date TYPE SY-DATUM,
start_time TYPE SY-UZEIT,
end_date TYPE SY-DATUM,
end_time TYPE SY-UZEIT,
duration TYPE SY-TABIX,
actual_state(1) TYPE C,
status TYPE CHAR50,
END OF ty_charfinal.

*WORKAREA
DATA : wa_rspclogchain TYPE ty_rspclogchain,
wa_rspcprocesslog TYPE ty_rspcprocesslog,
wa_final TYPE ty_final,
wa_zbw_pc_threshold TYPE ty_zbw_pc_threshold,
r_log_id TYPE RANGE OF rspclogchain-log_id,
w_log_id LIKE LINE OF r_log_id,
wa_charfinal TYPE ty_charfinal,
p_email TYPE somlreci1-receiver,
imessage TYPE standard TABLE OF solisti1 WITH HEADER LINE,
iattach TYPE standard TABLE OF solisti1 WITH HEADER LINE,


*INTERNAL TABLE
i_rspclogchain TYPE STANDARD TABLE OF ty_rspclogchain,
i_rspcprocesslog TYPE STANDARD TABLE OF ty_rspcprocesslog,
i_final TYPE STANDARD TABLE OF ty_final,
i_zbw_processname TYPE STANDARD TABLE OF ty_zbw_processname,
i_zbw_pc_schedule TYPE STANDARD TABLE OF ty_zbw_pc_schedule,
i_zbw_pc_threshold TYPE STANDARD TABLE OF ty_zbw_pc_threshold.

CONSTANTS :c_i TYPE char1 VALUE 'I',
c_eq TYPE char2 VALUE 'EQ'.

SELECT PROCESS_NAME
FROM zbw_processname
INTO TABLE i_zbw_processname.

IF i_zbw_processname IS NOT INITIAL.
SELECT PROCESS_CHAIN
PROCESS_NAME
SCHEDULE_DAY
SCHEDULE_TIME
FROM zbw_pc_schedule
INTO TABLE i_zbw_pc_schedule
FOR ALL ENTRIES IN i_zbw_processname
WHERE PROCESS_NAME EQ i_zbw_processname-PROCESS_NAME.
ENDIF.

IF i_zbw_processname IS NOT INITIAL.
SELECT PROCESS_NAME
THRESHOLD
FROM zbw_pc_threshold
INTO TABLE i_zbw_pc_threshold
FOR ALL ENTRIES IN i_zbw_processname
WHERE PROCESS_NAME EQ i_zbw_processname-PROCESS_NAME.
ENDIF.

IF i_zbw_processname IS NOT INITIAL.
SELECT chain_id
log_id
draw_variant
datum
zeit
analyzed_status
FROM rspclogchain
INTO TABLE i_rspclogchain
FOR ALL ENTRIES IN i_zbw_processname
WHERE chain_id EQ i_zbw_processname-PROCESS_NAME.
ENDIF.

IF i_rspclogchain IS NOT INITIAL.
LOOP AT i_rspclogchain INTO wa_rspclogchain.
w_log_id-sign = c_i.
w_log_id-option = c_eq.
w_log_id-low = wa_rspclogchain-log_id.
APPEND w_log_id TO r_log_id.
clear wa_rspclogchain.
ENDLOOP.


SELECT rc~chain_id
rl~log_id
rl~type
rl~event_start
rl~batchdate
rl~batchtime
rl~variante
rl~actual_state
rl~event_end
rl~starttimestamp
rl~endtimestamp
FROM rspcprocesslog as rl
INNER JOIN rspclogchain AS rc
ON rl~log_id = rc~log_id
INTO TABLE i_rspcprocesslog
WHERE rc~log_id IN r_log_id.
ENDIF.

LOOP AT i_rspcprocesslog INTO wa_rspcprocesslog.
wa_final-chain_id = wa_rspcprocesslog-chain_id.
DATA i_timestamp TYPE timestamp.

*--------------Functional Module used for converting starttimestamp to date and time------------*

i_timestamp = wa_rspcprocesslog-starttimestamp.
CALL FUNCTION 'UPS_STS_TSTAMP_TO_DATE'

EXPORTING
I_UNAME = 'SAPUSER'
I_TSTAMP = i_timestamp
I_TZONE = 'UST'
IMPORTING
E_DATE = wa_final-start_date
E_TIME = wa_final-start_time.

*---------------Functional Module used for converting endtimestamp to date and time------------*

i_timestamp = wa_rspcprocesslog-endtimestamp.
CALL FUNCTION 'UPS_STS_TSTAMP_TO_DATE'

EXPORTING
I_UNAME = 'SAPUSER'
I_TSTAMP = i_timestamp
I_TZONE = 'UST'
IMPORTING
E_DATE = wa_final-end_date
E_TIME = wa_final-end_time.

*---------------Functional Module used for calculating duration------------*

CALL FUNCTION 'SWI_DURATION_DETERMINE'
EXPORTING
START_DATE = wa_final-start_date
END_DATE = wa_final-end_date
START_TIME = wa_final-start_time
END_TIME = wa_final-end_time
IMPORTING
DURATION = wa_final-duration.


*-------------------------Functional module to convert date to day---------------------------------*

* CALL FUNCTION 'DATE_COMPUTE_DAY'
* EXPORTING
* DATE = wa_final-start_date
* IMPORTING
* DAY = wa_final-start_day.
*
APPEND wa_final TO i_final.
ENDLOOP.

*-------------------------Read internal table to match the threshold-----------------------*

LOOP AT i_final INTO wa_final.
READ TABLE i_zbw_pc_threshold INTO wa_zbw_pc_threshold WITH KEY process_name = wa_final-chain_id.

IF wa_final-duration <= wa_zbw_pc_threshold-threshold.
* wa_final-ACTUAL_STATE = 'G'.
wa_final-STATUS = 'Successfully Completed'.

ELSE.
* Append i_output2.
ENDIF.
MODIFY i_final FROM wa_final.
ENDLOOP.
************************************************************************

*FORM BUILD_XLS_DATA

************************************************************************
* Populate internal table with data from I_FINAL
SELECT * FROM i_final INTO TABLE iattach.

* Build the Excel data
PERFORM build_xls_data.

* Form to build the data for the Excel attachment
FORM build_xls_data .
* * Constants for new line and tab characters
constants: con_cret type x value '0D', "OK for non Unicode
con_tab type x value '09'. "OK for non Unicode

* * If you have Unicode check active in program attributes then you will
* * need to declare constants as follows
* * class cl_abap_char_utilities definition load.
constants:
* con_tab type c value cl_abap_char_utilities=>horizontal_tab,
* con_cret type c value cl_abap_char_utilities=>cr_lf.
*
* * Build the header row of the Excel file
concatenate 'CHAIN_ID' 'START_DATE' 'START_TIME' 'END_DATE' 'END_TIME' 'DURATION' 'ACTUAL_STATE' 'STATUS'
into iattach separated by con_tab.
concatenate con_cret iattach into iattach.
append iattach.

* * Loop through the data in the internal table and build the rows for the Excel file
loop at i_final into wa_charfinal.
concatenate wa_charfinal-chain_id wa_charfinal-start_date wa_charfinal-start_time wa_charfinal-end_date wa_charfinal-end_time wa_charfinal-duration
wa_charfinal-actual_state wa_charfinal-status
into iattach separated by con_tab.
concatenate con_cret iattach into iattach.
append iattach.
endloop.
ENDFORM.



* Populate the message body text
CLEAR imessage.
REFRESH imessage.
imessage = 'Please find attached the data from table I_FINAL in an Excel file'.
APPEND imessage.

* Set the email address of the recipient
p_email = 'xyz@abc.com'.

* Call the function module to send the email with the Excel attachment
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = imessage
put_in_outbox = 'X'
TABLES
packing_list = iattach
receivers = p_email
EXCEPTIONS
too_many_attachments = 1
document_not_sent = 2
document_type_not_supported= 3
operation_no_authorization = 4
parameter_error = 5
x_error = 6
OTHERS = 7.

* Check for errors
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.