Thursday, April 7, 2011

Upload Program to Create Notification

This program helps to upload legacy system data to SAP system to create Notification.

F4 help id provided at selection screen to upload data in excel file format.

After program execution Notification will be created. Error log file will be created at presentation server.

Program logic is given as below.

REPORT zpmcima0001
NO STANDARD PAGE HEADING LINE-SIZE 255.
TYPE-POOLS: truxs.
************************************************************************
* Struture Declaration *
************************************************************************
TYPES: BEGIN OF ty_data,
notif_type LIKE bapi2080-notif_type,
short_text LIKE bapi2080_nothdri-short_text, "Short Text for Notification
funct_loc LIKE bapi2080_nothdri-funct_loc, "Functional location,
equipment LIKE bapi2080_nothdri-equipment, "Equipment

* long_text TYPE CHAR2000,
plangroup LIKE bapi2080_nothdri-plangroup, "PlannerGroup
maintplant LIKE bapi2080_nothdri-maintplant, "Plant
pm_wkctr LIKE bapi2080_nothdri-pm_wkctr, "Maint. Work Center
reportedby LIKE bapi2080_nothdri-reportedby, "Reported By
priority LIKE bapi2080_nothdri-priority, "Priority
strmlfndate LIKE bapi2080_nothdri-strmlfndate, "Malfunction Start (DDMMYYYY)
strmlfntime LIKE bapi2080_nothdri-strmlfntime, "Malfunction StartTime (HHMMSS)(24 hour format)
endmlfndate LIKE bapi2080_nothdri-endmlfndate, "Malfunction End (DDMMYYYY)
endmlfntime LIKE bapi2080_nothdri-endmlfntime, "Malfunction EndTime (HHMMSS)(24 hour format)
desstdate LIKE bapi2080_nothdri-desstdate, "Required Start (DDMMYYYY)
dessttime LIKE bapi2080_nothdri-dessttime, "Required Starttime (HHMMSS)(24 hour format)"
desenddate LIKE bapi2080_nothdri-desenddate, "Required End (DDMMYYYY)
desendtm LIKE bapi2080_nothdri-desendtm, "Required End Time (HHMMSS)(24 hour format)"
breakdown LIKE bapi2080_nothdri-breakdown, "Break down Indicator (Y or N)
long_text LIKE bapi2080_notfulltxti-text_line, "Long text
item_sort_no LIKE bapi2080_notitemi-item_sort_no, "Item Sort Number
dl_codegrp LIKE bapi2080_notitemi-dl_codegrp, "Object part code group
dl_code LIKE bapi2080_notitemi-dl_code, "Object part code
d_codegrp LIKE bapi2080_notitemi-d_codegrp, "Damage code group
d_code LIKE bapi2080_notitemi-d_code, "Damage code
descript LIKE bapi2080_notitemi-descript, "Item Text
item_key LIKE bapi2080_notcausi-item_key, "Item Key for Cause "New Added
cause_sort_no LIKE bapi2080_notcausi-cause_sort_no, "Cause Sort Number "New Added
cause_codegrp LIKE bapi2080_notcausi-cause_codegrp, "Cause code group
cause_code LIKE bapi2080_notcausi-cause_code, "Cause code
causetext LIKE bapi2080_notcausi-causetext, "Cause Text

act_key LIKE bapi2080_notactvi-act_key, "Iteam Key for Activity "New Addded
act_sort_no LIKE bapi2080_notactvi-act_sort_no, "Activity Sort Number
act_codegrp LIKE bapi2080_notactvi-act_codegrp, "Activity code group
act_code LIKE bapi2080_notactvi-act_code, "Activity code
acttext LIKE bapi2080_notactvi-acttext, "Activity Text
start_date LIKE bapi2080_notactvi-start_date, "START_DATE Activity Start date
start_time LIKE bapi2080_notactvi-start_time, "START_TIME Activity Start Time
end_date LIKE bapi2080_notactvi-end_date, "END_DATE Activity End date
end_time LIKE bapi2080_notactvi-end_time, "END_TIME Activity End Time
END OF ty_data.

TYPES: BEGIN OF ty_log,
notif_type(2),
short_text(40),
funct_loc(30),
equipment(18),
plangroup(3) ,
maintplant(4) ,
pm_wkctr(8) ,
reportedby(12),
priority(1) ,
strmlfndate(8),
strmlfntime(6),
endmlfndate(8),
endmlfntime(6),
desstdate(8) ,
dessttime(6) ,
desenddate(8),
desendtm(6) ,
breakdown(1) ,
text TYPE char255,
END OF ty_log.

TYPES: BEGIN OF ty_log1,
notification_type(4),
notification_text(40),
functional_location(30),
equipment(18),
planner_group(15),
plant(8),
maint_work_center(16),
reported_by(12),
priority(10),
malfunction_startdate(20),
malfunction_starttime(20),
malfunction_enddate(20),
malfunction_endtime(20),
required_startdate(20),
required_starttime(20),
required_enddate(20),
required_endtime(20),
breakdown_indicator(20),
text TYPE char255,
END OF ty_log1.

TYPES: BEGIN OF ty_equi,
equnr LIKE equi-equnr,
iwerk LIKE equz-iwerk,
tplnr LIKE iloa-tplnr,
END OF ty_equi.
************************************************************************
* Intrnal Table Declaration *
************************************************************************
DATA: it_nothdri TYPE STANDARD TABLE OF bapi2080_nothdri WITH HEADER LINE,
it_bapiret2 TYPE STANDARD TABLE OF bapiret2 WITH HEADER LINE,
it_nothdre TYPE STANDARD TABLE OF bapi2080_nothdre WITH HEADER LINE,
it_notfulltxti TYPE STANDARD TABLE OF bapi2080_notfulltxti WITH HEADER LINE,
it_notitemi TYPE STANDARD TABLE OF bapi2080_notitemi WITH HEADER LINE,
it_notcausi TYPE STANDARD TABLE OF bapi2080_notcausi WITH HEADER LINE,
it_notactvi TYPE STANDARD TABLE OF bapi2080_notactvi WITH HEADER LINE,
it_data TYPE STANDARD TABLE OF ty_data,
it_raw TYPE truxs_t_text_data,
it_equi TYPE STANDARD TABLE OF ty_equi,
it_log TYPE STANDARD TABLE OF ty_log,
wa_equi TYPE ty_equi,
wa_data TYPE ty_data.

************************************************************************
* Data Declaration *
************************************************************************
DATA: wa_log TYPE ty_log,
wa_t100 TYPE t100,
g_flag TYPE c,
g_flag1 TYPE c, "Used for log file
g_type LIKE bapi2080-notif_type,
g_count_item LIKE bapi2080_notitemi-item_sort_no,
g_count_in_error TYPE i,
error_text(200) TYPE c,
g_count_confirmed TYPE i.

************************************************************************
* Selection Screen *
************************************************************************
*Upload File Selection Screen
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS : p_file LIKE rlgrap-filename OBLIGATORY .
SELECTION-SCREEN END OF BLOCK b1.
*Log File selection screen
*SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME.
*PARAMETERS : p_file1 LIKE rlgrap-filename OBLIGATORY .
*SELECTION-SCREEN END OF BLOCK b2.
************************************************************************
* At Selection Screen *
************************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
field_name = ' '
IMPORTING
file_name = p_file.

*AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file1.
*
* CALL FUNCTION 'F4_FILENAME'
* EXPORTING
* program_name = syst-cprog
* dynpro_number = syst-dynnr
* field_name = ' '
* IMPORTING
* file_name = p_file1.
************************************************************************
* Start of Selection *
************************************************************************
START-OF-SELECTION.
*Read the data from the file to internal table.
PERFORM read_data.
*Create Measuring Doc
PERFORM create_notification.
*Download Log File
PERFORM download_file.

*&---------------------------------------------------------------------*
*& Form READ_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM read_data .
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
* I_FIELD_SEPERATOR =
i_line_header = 'X'
i_tab_raw_data = it_raw
i_filename = p_file
TABLES
i_tab_converted_data = it_data[]
* EXCEPTIONS
* CONVERSION_FAILED = 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.
RAISE upload_problem.
ENDIF.
ENDFORM. " READ_DATA


*&---------------------------------------------------------------------*
*& Form CREATE_NOTIFICATION
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM create_notification .
DATA: l_type TYPE qmart,
ls_notifheader TYPE bapi2080_nothdre,
l_equnr TYPE equnr,
l_tplnr TYPE tplnr.
*Select Equipment Number and Plant combination into an internal table
SELECT equnr iwerk tplnr
INTO TABLE it_equi
FROM v_equi
WHERE iwerk = 'I302'.

SORT it_equi BY equnr tplnr.
DELETE ADJACENT DUPLICATES FROM it_equi.
g_count_item = 1.

LOOP AT it_data INTO wa_data.
CLEAR: it_notitemi,
it_notcausi,
it_notactvi.

IF ( wa_data-notif_type IS NOT INITIAL AND g_flag IS INITIAL ).
g_type = wa_data-notif_type.
*Check plant whether it belongs to gpec (i302) or not.
IF wa_data-equipment IS NOT INITIAL .
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = wa_data-equipment
IMPORTING
output = l_equnr.

READ TABLE it_equi INTO wa_equi
WITH KEY equnr = l_equnr.
IF sy-subrc <> 0.
PERFORM build_log_header.
MOVE-CORRESPONDING wa_data TO wa_log.
CONCATENATE 'Equipment' wa_data-equipment 'does not belong to GPEC plant' INTO wa_log-text
SEPARATED BY space.
APPEND wa_log TO it_log.
PERFORM build_error_output.
CLEAR: wa_equi,wa_data.
CONTINUE.
ELSE.
it_nothdri-equipment = wa_data-equipment. "'900000000'."'10000030'.
ENDIF.
ENDIF.
IF wa_data-funct_loc IS NOT INITIAL.
CALL FUNCTION 'CONVERSION_EXIT_TPLNR_INPUT'
EXPORTING
input = wa_data-funct_loc
i_flg_check_internal = 'X'
IMPORTING
output = l_tplnr.
READ TABLE it_equi INTO wa_equi
WITH KEY tplnr = l_tplnr.
IF sy-subrc <> 0.
PERFORM build_log_header.
MOVE-CORRESPONDING wa_data TO wa_log.
CONCATENATE 'Functiona Location' wa_data-funct_loc 'does not belong to GPEC plant' INTO wa_log-text
SEPARATED BY space.
APPEND wa_log TO it_log.
PERFORM build_error_output.
CLEAR: wa_equi,wa_data.
CONTINUE.
ELSEIF sy-subrc EQ 0 .
* it_nothdri-funct_loc = wa_data-funct_loc.
it_nothdri-funct_loc = l_tplnr.
ENDIF.
ENDIF.
* create header data
it_nothdri-refobjectkey = wa_data-notif_type. "'B2'."'PJAREF'.
it_nothdri-reportedby = wa_data-reportedby. "'ASC3832'.
it_nothdri-short_text = wa_data-short_text. "'AKS Short Test from BAPI'.
it_nothdri-priority = wa_data-priority. "'2'."High
it_nothdri-plangroup = wa_data-plangroup.
it_nothdri-maintplant = wa_data-maintplant.
it_nothdri-pm_wkctr = wa_data-pm_wkctr.
it_nothdri-strmlfndate = wa_data-strmlfndate.
it_nothdri-strmlfntime = wa_data-strmlfntime.
it_nothdri-endmlfndate = wa_data-endmlfndate.
it_nothdri-endmlfntime = wa_data-endmlfntime.
it_nothdri-desstdate = wa_data-desstdate.
it_nothdri-dessttime = wa_data-dessttime.
it_nothdri-desenddate = wa_data-desenddate.
it_nothdri-desendtm = wa_data-desendtm.
IF wa_data-breakdown = 'Y'.
it_nothdri-breakdown = 'X'.
ENDIF.

*Create long text
PERFORM create_long_text USING wa_data-long_text.

*Item
IF wa_data-item_sort_no IS NOT INITIAL.
it_notitemi-item_key = wa_data-item_sort_no.
it_notitemi-item_sort_no = wa_data-item_sort_no. "'1'.
it_notitemi-descript = wa_data-descript."'Test Item '.
it_notitemi-d_codegrp = wa_data-d_codegrp." 'FCSCP001'.
it_notitemi-d_code = wa_data-d_code. "'CP01'.
it_notitemi-dl_codegrp = wa_data-dl_codegrp. "'FCP001'.
it_notitemi-dl_code = wa_data-dl_code. "'P001'.
APPEND it_notitemi.
ENDIF.

*Cause
IF wa_data-cause_sort_no IS NOT INITIAL. "'1'..
it_notcausi-cause_sort_no = wa_data-cause_sort_no. "'1'.
it_notcausi-item_sort_no = wa_data-item_sort_no. "'1'.
it_notcausi-item_key = wa_data-item_key.
it_notcausi-causetext = wa_data-causetext."'Test Cause'.
it_notcausi-cause_codegrp = wa_data-cause_codegrp."'FCVNP001'.
it_notcausi-cause_code = wa_data-cause_code. "'CC01'.
APPEND it_notcausi.
ENDIF.

*Activity
IF wa_data-act_key IS NOT INITIAL.
it_notactvi-act_key = wa_data-act_key.
it_notactvi-act_sort_no = wa_data-act_sort_no. "'1'.
it_notactvi-item_sort_no = wa_data-act_key. " '1'.
it_notactvi-acttext = wa_data-acttext."'Test Activity'.
it_notactvi-act_codegrp = wa_data-act_codegrp."'FCVNP001'.
it_notactvi-act_code = wa_data-act_code. "'RM06'.
it_notactvi-start_date = wa_data-start_date."'06.10.2010'.
it_notactvi-start_time = wa_data-start_time. "'182118'.
it_notactvi-end_date = wa_data-end_date."'06.10.2010'.
it_notactvi-end_time = wa_data-end_time. "'182139'.
APPEND it_notactvi.
ENDIF.
g_flag = 'X'.
ELSEIF ( wa_data-notif_type IS INITIAL AND g_flag IS NOT INITIAL ).
g_count_item = g_count_item + 1.
*Create long text
PERFORM create_long_text USING wa_data-long_text.
*Insert Item level data
IF wa_data-item_sort_no IS NOT INITIAL.
it_notitemi-item_key = wa_data-item_sort_no.
it_notitemi-item_sort_no = wa_data-item_sort_no. "'1'.
it_notitemi-descript = wa_data-descript."'Test Item '.
it_notitemi-d_codegrp = wa_data-d_codegrp." 'FCSCP001'.
it_notitemi-d_code = wa_data-d_code. "'CP01'.
it_notitemi-dl_codegrp = wa_data-dl_codegrp. "'FCP001'.
it_notitemi-dl_code = wa_data-dl_code. "'P001'.
APPEND it_notitemi.
ELSE.
it_notitemi-item_key = g_count_item.
it_notitemi-item_sort_no = g_count_item.
APPEND it_notitemi.
ENDIF.
*Cause
IF wa_data-cause_sort_no IS NOT INITIAL. "'1'..
it_notcausi-cause_sort_no = wa_data-cause_sort_no. "'1'.
IF wa_data-item_sort_no IS INITIAL.
it_notcausi-item_sort_no = g_count_item.
ELSE.
it_notcausi-item_sort_no = wa_data-item_sort_no. "'1'.
ENDIF.
*i_BAPI2080_NOTCAUSI-ITEM_KEY =.
it_notcausi-item_key = wa_data-item_key.
it_notcausi-causetext = wa_data-causetext."'Test Cause'.
it_notcausi-cause_codegrp = wa_data-cause_codegrp."'FCVNP001'.
it_notcausi-cause_code = wa_data-cause_code. "'CC01'.
APPEND it_notcausi.
ENDIF.

*Activity
IF wa_data-act_key IS NOT INITIAL.
it_notactvi-act_key = wa_data-act_key.
it_notactvi-act_sort_no = wa_data-act_sort_no. "'1'.
it_notactvi-item_sort_no = wa_data-act_key. " '1'.
it_notactvi-acttext = wa_data-acttext."'Test Activity'.
it_notactvi-act_codegrp = wa_data-act_codegrp."'FCVNP001'.
it_notactvi-act_code = wa_data-act_code. "'RM06'.
it_notactvi-start_date = wa_data-start_date."'06.10.2010'.
it_notactvi-start_time = wa_data-start_time. "'182118'.
it_notactvi-end_date = wa_data-end_date."'06.10.2010'.
it_notactvi-end_time = wa_data-end_time. "'182139'.
APPEND it_notactvi.
ENDIF.
ELSEIF ( wa_data-notif_type IS NOT INITIAL AND g_flag IS NOT INITIAL ).
*Call BAPI to Create Notification
PERFORM call_bapi.
CLEAR: g_flag,g_count_item.
g_count_item = 1.
g_type = wa_data-notif_type.
*Check plant whether it belongs to gpec (i302) or not.
IF wa_data-equipment IS NOT INITIAL .
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = wa_data-equipment
IMPORTING
output = l_equnr.
READ TABLE it_equi INTO wa_equi
WITH KEY equnr = l_equnr.
IF sy-subrc <> 0.
MOVE-CORRESPONDING wa_data TO wa_log.
CONCATENATE 'Equipment' wa_data-equipment 'does not belong to GPEC plant'
INTO wa_log-text SEPARATED BY space.
APPEND wa_log TO it_log.
CLEAR: wa_equi,wa_data.
CONTINUE.
ELSE.
it_nothdri-equipment = wa_data-equipment. "'900000000'."'10000030'.
ENDIF.
ENDIF.
IF wa_data-funct_loc IS NOT INITIAL.
CALL FUNCTION 'CONVERSION_EXIT_TPLNR_INPUT'
EXPORTING
input = wa_data-funct_loc
i_flg_check_internal = 'X'
IMPORTING
output = l_tplnr.
READ TABLE it_equi INTO wa_equi
WITH KEY tplnr = l_tplnr.
IF sy-subrc <> 0.
MOVE-CORRESPONDING wa_data TO wa_log.
CONCATENATE 'Functiona Location' wa_data-funct_loc 'does not belong to GPEC plant'
INTO wa_log-text SEPARATED BY space.
APPEND wa_log TO it_log.
CLEAR: wa_equi,wa_data.
CONTINUE.
ELSEIF sy-subrc EQ 0.
* it_nothdri-funct_loc = wa_data-funct_loc.
it_nothdri-funct_loc = l_tplnr.
ENDIF.
ENDIF.
* create header data
it_nothdri-refobjectkey = wa_data-notif_type. "'B2'."'PJAREF'.
it_nothdri-reportedby = wa_data-reportedby. "'ASC3832'.
it_nothdri-short_text = wa_data-short_text. "'AKS Short Test from BAPI'.
it_nothdri-priority = wa_data-priority. "'2'."High
it_nothdri-plangroup = wa_data-plangroup.
it_nothdri-maintplant = wa_data-maintplant.
it_nothdri-pm_wkctr = wa_data-pm_wkctr.
it_nothdri-strmlfndate = wa_data-strmlfndate.
it_nothdri-strmlfntime = wa_data-strmlfntime.
it_nothdri-endmlfndate = wa_data-endmlfndate.
it_nothdri-endmlfntime = wa_data-endmlfntime.
it_nothdri-desstdate = wa_data-desstdate.
it_nothdri-dessttime = wa_data-dessttime.
it_nothdri-desenddate = wa_data-desenddate.
it_nothdri-desendtm = wa_data-desendtm.
IF wa_data-breakdown = 'Y'.
it_nothdri-breakdown = 'X'.
ENDIF.
*Create long text
PERFORM create_long_text USING wa_data-long_text.
*Item
IF wa_data-item_sort_no IS NOT INITIAL.
it_notitemi-item_key = wa_data-item_sort_no.
it_notitemi-item_sort_no = wa_data-item_sort_no. "'1'.
it_notitemi-descript = wa_data-descript."'Test Item '.
it_notitemi-d_codegrp = wa_data-d_codegrp." 'FCSCP001'.
it_notitemi-d_code = wa_data-d_code. "'CP01'.
it_notitemi-dl_codegrp = wa_data-dl_codegrp. "'FCP001'.
it_notitemi-dl_code = wa_data-dl_code. "'P001'.
APPEND it_notitemi.
ELSE.
it_notitemi-item_key = g_count_item.
it_notitemi-item_sort_no = g_count_item.
ENDIF.
*Cause
IF wa_data-cause_sort_no IS NOT INITIAL. "'1'..
it_notcausi-cause_sort_no = wa_data-cause_sort_no. "'1'.
IF wa_data-item_sort_no IS INITIAL.
it_notcausi-item_sort_no = g_count_item.
ELSE.
it_notcausi-item_sort_no = wa_data-item_sort_no. "'1'.
ENDIF.
*i_BAPI2080_NOTCAUSI-ITEM_KEY =.
it_notcausi-item_key = wa_data-item_key.
it_notcausi-causetext = wa_data-causetext."'Test Cause'.
it_notcausi-cause_codegrp = wa_data-cause_codegrp."'FCVNP001'.
it_notcausi-cause_code = wa_data-cause_code. "'CC01'.
APPEND it_notcausi.
ENDIF.
*Activity
IF wa_data-act_key IS NOT INITIAL.
it_notactvi-act_key = wa_data-act_key.
it_notactvi-act_sort_no = wa_data-act_sort_no. "'1'.
it_notactvi-item_sort_no = wa_data-act_key. " '1'.
it_notactvi-acttext = wa_data-acttext."'Test Activity'.
it_notactvi-act_codegrp = wa_data-act_codegrp."'FCVNP001'.
it_notactvi-act_code = wa_data-act_code. "'RM06'.
it_notactvi-start_date = wa_data-start_date."'06.10.2010'.
it_notactvi-start_time = wa_data-start_time. "'182118'.
it_notactvi-end_date = wa_data-end_date."'06.10.2010'.
it_notactvi-end_time = wa_data-end_time. "'182139'.
APPEND it_notactvi.
ENDIF.
g_flag = 'X'.
ENDIF.
ENDLOOP.
*Create Notification for the last Notification item
IF it_nothdri IS NOT INITIAL.
PERFORM call_bapi.
CLEAR g_flag.
ENDIF.

ENDFORM. " CREATE_NOTIFICATION
*&---------------------------------------------------------------------*
*& Form DOWNLOAD_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM download_file .

DATA: l_dsn LIKE rlgrap-filename,
l_filelen TYPE i,
gui_filename TYPE string,
l_path TYPE string,
l_line TYPE i.
* Prepare the error log file location and file name
l_filelen = STRLEN( p_file ).
l_filelen = l_filelen - 3.
CONCATENATE p_file(l_filelen) 'ERR.txt' INTO l_dsn.

gui_filename = l_dsn.

DESCRIBE TABLE it_log LINES l_line.
IF l_line = 0.
wa_log-text = 'Error not found. Upload process completed!'.
APPEND wa_log TO it_log.
CLEAR wa_log.
ENDIF.


CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
* BIN_FILESIZE =
filename = gui_filename
filetype = 'ASC'
* APPEND = ' '
write_field_separator = 'X'
TABLES
data_tab = it_log
* FIELDNAMES =
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
OTHERS = 22
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ELSE.
SKIP.
CONCATENATE 'Error file downloaded at' l_dsn INTO l_path
SEPARATED BY space.
WRITE:/ l_path.
ENDIF.
ENDFORM. " DOWNLOAD_FILE
*&---------------------------------------------------------------------*
*& Form CALL_BAPI
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM call_bapi .
DATA ls_notifheader TYPE bapi2080_nothdre.
*Call BAPI to create Notification
CALL FUNCTION 'BAPI_ALM_NOTIF_CREATE'
EXPORTING
* EXTERNAL_NUMBER =
notif_type = g_type
notifheader = it_nothdri
* TASK_DETERMINATION = ' '
* SENDER =
* ORDERID =
IMPORTING
notifheader_export = it_nothdre
TABLES
notitem = it_notitemi
notifcaus = it_notcausi
notifactv = it_notactvi
* NOTIFTASK =
* NOTIFPARTNR =
longtexts = it_notfulltxti
* KEY_RELATIONSHIPS =
return = it_bapiret2.

*Save Notification
CALL FUNCTION 'BAPI_ALM_NOTIF_SAVE'
EXPORTING
number = it_nothdre-notif_no
IMPORTING
notifheader = ls_notifheader
* TABLES
* RETURN =
.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
* EXPORTING
* WAIT =
* IMPORTING
* RETURN =
.
*Update Error messages into log file.
LOOP AT it_bapiret2 WHERE type = 'E'.
PERFORM log_error.
ENDLOOP.
*Append blank lines in error log
IF it_bapiret2 IS NOT INITIAL.
CLEAR wa_log.
APPEND wa_log TO it_log.
ENDIF.
*Delete Items with Blank fields.
IF ls_notifheader-notif_no IS NOT INITIAL.
* CLEAR wa_log.
* CONCATENATE 'Notification' ls_notifheader-notif_no 'created for' g_type
* 'Type' INTO wa_log-text SEPARATED BY space.
** wa_log-text = ls_notifheader-notif_no.
* APPEND wa_log TO it_log.
* CLEAR wa_log.
* APPEND wa_log TO it_log.

WAIT UP TO 2 SECONDS.
PERFORM delete_notif_items USING ls_notifheader-notif_no .
ENDIF.
CLEAR: g_type,ls_notifheader.
REFRESH: it_nothdri,
it_bapiret2,
it_nothdre,
it_notfulltxti,
it_notitemi,
it_notcausi ,
it_notactvi.
ENDFORM. " CALL_BAPI
*&---------------------------------------------------------------------*
*& Form DELETE_NOTIF_ITEMS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_LS_NOTIFHEADER_NOTIF_NO text
*----------------------------------------------------------------------*
FORM delete_notif_items USING p_notif_no.
DATA: lt_notitem TYPE STANDARD TABLE OF bapi2080_notiteme WITH HEADER LINE,
lt_notitem1 TYPE STANDARD TABLE OF bapi2080_notitemi WITH HEADER LINE,
lt_bapiret2 TYPE STANDARD TABLE OF bapiret2 WITH HEADER LINE,
ls_notifheader1 TYPE bapi2080_nothdre.

CALL FUNCTION 'BAPI_ALM_NOTIF_GET_DETAIL'
EXPORTING
number = p_notif_no
TABLES
notitem = lt_notitem.

LOOP AT lt_notitem
WHERE d_codegrp EQ space
AND d_code EQ space
AND dl_codegrp EQ space
AND dl_code EQ space
AND delete_flag EQ space.
lt_notitem1-item_key = lt_notitem-item_key. " = '0005'.
lt_notitem1-item_sort_no = lt_notitem-item_sort_no. " = '0005'.
APPEND lt_notitem1.
ENDLOOP.

CALL FUNCTION 'BAPI_ALM_NOTIF_DATA_DELETE'
EXPORTING
number = p_notif_no
TABLES
notitem = lt_notitem1[]
return = lt_bapiret2.

* Save Notification
CALL FUNCTION 'BAPI_ALM_NOTIF_SAVE'
EXPORTING
number = p_notif_no
IMPORTING
notifheader = ls_notifheader1
* TABLES
* RETURN =
.
*WRITE:/ ls_notifheader1-notif_no.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
* EXPORTING
* WAIT =
* IMPORTING
* RETURN =
.
CLEAR : lt_notitem,lt_notitem1,lt_bapiret2,p_notif_no.
REFRESH: lt_notitem,lt_notitem1,lt_bapiret2.
ENDFORM. " DELETE_NOTIF_ITEMS
*&---------------------------------------------------------------------*
*& Form CREATE_LONG_TEXT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_WA_DATA_LONG_TEXT text
*----------------------------------------------------------------------*
FORM create_long_text USING p_long_text.

it_notfulltxti-format_col = 'U1'.
it_notfulltxti-objtype = 'QMEL'.
it_notfulltxti-text_line = p_long_text.
APPEND it_notfulltxti.
ENDFORM. " CREATE_LONG_TEXT
*&---------------------------------------------------------------------*
*& Form LOG_ERROR
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM log_error .
* IF g_flag1 IS INITIAL.
PERFORM build_log_header.
* APPEND wa_log TO it_log.
* g_flag1 = 'X'.
* ENDIF.

ADD 1 TO g_count_in_error.
SELECT SINGLE * FROM t100 INTO wa_t100
WHERE sprsl = sy-langu
AND arbgb = sy-msgid
AND msgnr = sy-msgno.
MOVE wa_t100-text TO error_text.
IF error_text CS '&1'.
REPLACE '&1' WITH sy-msgv1 INTO error_text.
ELSE.
REPLACE '&' WITH sy-msgv1 INTO error_text.
ENDIF.
IF error_text CS '&2'.
REPLACE '&2' WITH sy-msgv2 INTO error_text.
ELSE.
REPLACE '&' WITH sy-msgv2 INTO error_text.
ENDIF.
IF error_text CS '&3'.
REPLACE '&3' WITH sy-msgv3 INTO error_text.
ELSE.
REPLACE '&' WITH sy-msgv3 INTO error_text.
ENDIF.
IF error_text CS '&4'.
REPLACE '&4' WITH sy-msgv4 INTO error_text.
ELSE.
REPLACE '&' WITH sy-msgv4 INTO error_text.
ENDIF.
CONDENSE error_text.

*Write Error log on the output screen
WRITE:
/(4) g_type,
(40) it_nothdri-short_text,
(30) it_nothdri-funct_loc,
(18) it_nothdri-equipment,
(15) it_nothdri-plangroup,
(8) it_nothdri-maintplant,
(16) it_nothdri-pm_wkctr,
(12) it_nothdri-reportedby,
(10) it_nothdri-priority,
(20) it_nothdri-strmlfndate,
(20) it_nothdri-strmlfntime,
(20) it_nothdri-endmlfndate,
(20) it_nothdri-endmlfntime,
(20) it_nothdri-desstdate,
(20) it_nothdri-dessttime,
(20) it_nothdri-desenddate,
(20) it_nothdri-desendtm,
(20) it_nothdri-breakdown,
(80) error_text.

wa_log-notif_type = g_type.
wa_log-short_text = it_nothdri-short_text.
wa_log-funct_loc = it_nothdri-funct_loc.
wa_log-equipment = it_nothdri-equipment.
wa_log-plangroup = it_nothdri-plangroup.
wa_log-maintplant = it_nothdri-maintplant.
wa_log-pm_wkctr = it_nothdri-maintplant.
wa_log-reportedby = it_nothdri-reportedby.
wa_log-priority = it_nothdri-priority.
wa_log-strmlfndate = it_nothdri-strmlfndate.
wa_log-strmlfntime = it_nothdri-strmlfntime.
wa_log-endmlfndate = it_nothdri-endmlfndate.
wa_log-endmlfntime = it_nothdri-strmlfntime.
wa_log-desstdate = it_nothdri-desstdate.
wa_log-dessttime = it_nothdri-dessttime.
wa_log-desenddate = it_nothdri-desenddate.
wa_log-desendtm = it_nothdri-desendtm.
wa_log-breakdown = it_nothdri-breakdown.
wa_log-text = error_text.
APPEND wa_log TO it_log.
ENDFORM. " LOG_ERROR
*&---------------------------------------------------------------------*
*& Form BUILD_LOG_HEADER
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM build_log_header .
IF g_flag1 IS INITIAL.
wa_log-notif_type = 'Notif_Type'.
wa_log-short_text = 'Notification Text'.
wa_log-funct_loc = 'Function Location'.
wa_log-equipment = 'Notification'.
wa_log-plangroup = 'Planner Grp'.
wa_log-maintplant = 'MPlnt'.
wa_log-pm_wkctr = 'Work Centre'.
wa_log-reportedby = 'Reported by'.
wa_log-priority = 'Priority'.
wa_log-strmlfndate = 'Mf_st_dt'.
wa_log-strmlfntime = 'Mf_st_t'.
wa_log-endmlfndate = 'Mf_end_date'.
wa_log-endmlfntime = 'Mf_end_time'.
wa_log-desstdate = 'Req_st_date'.
wa_log-dessttime = 'Req_st_time'.
wa_log-desenddate = 'Req_end_date'.
wa_log-desendtm = 'Req_end_time'.
wa_log-breakdown = 'Breakdown'.
wa_log-text ='Error text'.
APPEND wa_log TO it_log.

*Write header on report output.
WRITE:/ wa_log-notif_type,
wa_log-short_text,
wa_log-funct_loc,
wa_log-equipment,
wa_log-plangroup,
wa_log-maintplant,
wa_log-pm_wkctr,
wa_log-reportedby,
wa_log-priority,
wa_log-strmlfndate,
wa_log-strmlfntime,
wa_log-endmlfndate,
wa_log-endmlfntime,
wa_log-desstdate,
wa_log-dessttime,
wa_log-desenddate,
wa_log-desendtm,
wa_log-breakdown,
wa_log-text.
SKIP.

CLEAR wa_log.
APPEND wa_log TO it_log.
g_flag1 = 'X'.
ENDIF.

ENDFORM. " BUILD_LOG_HEADER
*&---------------------------------------------------------------------*
*& Form BUILD_ERROR_OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM build_error_output .
*Write Error on the output screen
WRITE:/ wa_log-notif_type,
wa_log-short_text,
wa_log-funct_loc,
wa_log-equipment,
wa_log-plangroup,
wa_log-maintplant,
wa_log-pm_wkctr,
wa_log-reportedby,
wa_log-priority,
wa_log-strmlfndate,
wa_log-strmlfntime,
wa_log-endmlfndate,
wa_log-endmlfntime,
wa_log-desstdate,
wa_log-dessttime,
wa_log-desenddate,
wa_log-desendtm,
wa_log-breakdown,
wa_log-text.
CLEAR wa_log.
ENDFORM. " BUILD_ERROR_OUTPUT

1 comment:

  1. every thing works fine but ls_nothdre and lt_nothdre are empty after declaring the bapi function module.

    ReplyDelete