forked from Keller-Michael/autostart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzautostart.abap
289 lines (238 loc) · 7.26 KB
/
zautostart.abap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
*&---------------------------------------------------------------------*
*& Report ZAUTOSTART
*&---------------------------------------------------------------------*
*&
*& start transactions from a folder of your favorites
*&
*& written by Michael Keller
*&
*&---------------------------------------------------------------------*
REPORT zautostart.
CLASS lcl_autostart DEFINITION.
PUBLIC SECTION.
TYPES favorites TYPE TABLE OF smen_buffc.
CLASS-METHODS run.
PRIVATE SECTION.
CLASS-METHODS do_prechecks
IMPORTING
iv_tcode TYPE syst_tcode
EXPORTING
ev_report TYPE raldb_repo
ev_var_exists TYPE abap_bool
EXCEPTIONS
error.
CLASS-METHODS get_favorites_from_folder
IMPORTING
iv_folder TYPE char100sm
EXPORTING
et_favorites TYPE favorites.
ENDCLASS.
CLASS lcl_autostart IMPLEMENTATION.
METHOD do_prechecks.
DATA: ls_tstc TYPE tstc,
lv_message TYPE text100,
lv_report TYPE raldb_repo,
lv_variant TYPE raldb_vari,
lv_subrc TYPE sysubrc.
" check if transaction code exists
SELECT SINGLE *
FROM tstc
INTO ls_tstc
WHERE tcode = iv_tcode.
IF sy-subrc <> 0.
lv_message = 'Transaction code &1 is invalid.'.
REPLACE FIRST OCCURRENCE OF '&1' IN lv_message WITH iv_tcode.
MESSAGE lv_message TYPE 'I'.
RAISE error.
ENDIF.
CALL FUNCTION 'AUTHORITY_CHECK_TCODE'
EXPORTING
tcode = iv_tcode
EXCEPTIONS
ok = 1
not_ok = 2
OTHERS = 3.
IF sy-subrc > 1. " a little bit special
lv_message = 'No authorization for transaction code &1.'.
REPLACE FIRST OCCURRENCE OF '&1' IN lv_message WITH iv_tcode.
MESSAGE lv_message TYPE 'I'.
RAISE error.
ENDIF.
lv_report = ls_tstc-pgmna.
lv_variant = sy-uname.
CALL FUNCTION 'RS_VARIANT_EXISTS'
EXPORTING
report = lv_report
variant = lv_variant
IMPORTING
r_c = lv_subrc
EXCEPTIONS
not_authorized = 1
no_report = 2
report_not_existent = 3
report_not_supplied = 4
OTHERS = 5.
IF lv_subrc = 0.
ev_report = lv_report.
ev_var_exists = abap_true.
ENDIF.
ENDMETHOD.
METHOD get_favorites_from_folder.
DATA: lt_favorites TYPE favorites,
ls_favorites LIKE LINE OF lt_favorites,
lv_object_id TYPE menu_num_5.
CALL FUNCTION 'NAVIGATION_LOAD_FAVORITES'
EXPORTING
user_name = sy-uname
TABLES
favorites_tab = lt_favorites.
* FAVORITES_FOR_GUI =
* LINKS_LIST =
" look for the folder
LOOP AT lt_favorites INTO ls_favorites
WHERE reporttype = space
AND report = space
AND text IS NOT INITIAL.
TRANSLATE ls_favorites-text TO UPPER CASE.
IF ls_favorites-text = iv_folder.
lv_object_id = ls_favorites-object_id.
EXIT.
ENDIF.
ENDLOOP.
IF sy-subrc <> 0 OR lv_object_id IS INITIAL.
RETURN.
ENDIF.
" reduce to relevant transactions
DELETE lt_favorites WHERE parent_id <> lv_object_id
OR reporttype <> 'TR'
OR report = sy-tcode.
IF lt_favorites IS INITIAL.
RETURN.
ENDIF.
" sort favorites, important for starting sequence
SORT lt_favorites BY sort_order ASCENDING.
READ TABLE lt_favorites INTO ls_favorites INDEX 1.
IF sy-subrc <> 0.
RETURN.
ENDIF.
APPEND ls_favorites TO lt_favorites.
DELETE lt_favorites INDEX 1.
IF sy-subrc <> 0.
RETURN.
ENDIF.
et_favorites = lt_favorites.
ENDMETHOD.
METHOD run.
DATA: lt_parts TYPE TABLE OF syst_tcode,
lv_lines TYPE i,
lv_folder TYPE char100sm,
lt_favorites TYPE favorites,
ls_favorites TYPE smen_buffc,
lv_tcode TYPE syst_tcode,
lv_task TYPE text100,
lv_report TYPE raldb_repo,
lv_use_var TYPE abap_bool,
lv_answer TYPE char1,
lv_question TYPE itex132,
lv_tabix TYPE sytabix.
FIELD-SYMBOLS <favorites> TYPE smen_buffc.
" get name of favorites folder from transaction code
SPLIT sy-tcode AT '_' INTO TABLE lt_parts.
IF sy-subrc <> 0.
RETURN.
ENDIF.
lv_lines = lines( lt_parts ).
READ TABLE lt_parts INTO lv_folder INDEX lv_lines.
IF sy-subrc <> 0 OR lv_folder IS INITIAL.
RETURN.
ENDIF.
get_favorites_from_folder(
EXPORTING
iv_folder = lv_folder
IMPORTING
et_favorites = lt_favorites ).
IF lt_favorites IS INITIAL.
RETURN.
ENDIF.
lv_lines = lines( lt_favorites ).
CONCATENATE 'Start transactions from folder "' lv_folder '"?' INTO lv_question.
CALL FUNCTION 'POPUP_TO_CONFIRM'
EXPORTING
titlebar = 'Autostart'
* DIAGNOSE_OBJECT = ' '
text_question = lv_question
text_button_1 = 'Yes'
icon_button_1 = 'ICON_EXECUTE_OBJECT'
text_button_2 = 'Yes, delayed'
icon_button_2 = 'ICON_TIME'
default_button = '2'
display_cancel_button = 'X'
* USERDEFINED_F1_HELP = ' '
* START_COLUMN = 25
* START_ROW = 6
* POPUP_TYPE =
* IV_QUICKINFO_BUTTON_1 = ' '
* IV_QUICKINFO_BUTTON_2 = ' '
IMPORTING
answer = lv_answer
* TABLES
* PARAMETER =
EXCEPTIONS
text_not_found = 1
OTHERS = 2.
IF sy-subrc <> 0 OR lv_answer = 'A'.
RETURN.
ENDIF.
" find all transaction codes in folder and start them
LOOP AT lt_favorites ASSIGNING <favorites>.
CLEAR: lv_report,
lv_use_var.
lv_tabix = sy-tabix.
lv_tcode = <favorites>-report.
lv_task = lv_tcode.
do_prechecks(
EXPORTING
iv_tcode = lv_tcode
IMPORTING
ev_report = lv_report
ev_var_exists = lv_use_var
EXCEPTIONS
error = 1
OTHERS = 2 ).
IF sy-subrc <> 0.
CONTINUE.
ENDIF.
IF lv_tabix <> lv_lines.
CALL FUNCTION 'Z_AUTOSTART'
STARTING NEW TASK lv_task
DESTINATION IN GROUP DEFAULT
EXPORTING
iv_tcode = lv_tcode
iv_report = lv_report
iv_use_var = lv_use_var
EXCEPTIONS
system_failure = 1
communication_failure = 2
resource_failure = 3
OTHERS = 4.
IF sy-subrc <> 0.
CONTINUE.
ENDIF.
IF lv_lines > 2 AND lv_answer = '2'.
WAIT UP TO 3 SECONDS.
ELSE.
" do not start too fast
WAIT UP TO 1 SECONDS.
ENDIF.
ELSE.
CALL FUNCTION 'Z_AUTOSTART'
EXPORTING
iv_tcode = lv_tcode
iv_report = lv_report
iv_use_var = lv_use_var.
ENDIF.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
lcl_autostart=>run( ).