‎2013 May 21 3:44 PM
Hi everyone!
I'm learning abap and I would like to have some help. I want to create a dynamic parameters field on the screen, that changes according to user's preferences. For example, I've tried this code below:
DATA: BEGIN OF wa,
field TYPE i,
END OF wa.
DATA: it LIKE TABLE OF wa.
PARAMETERS: pa_total TYPE i.
DO pa_total TIMES.
PARAMETERS: pa_each TYPE i.
wa-field = pa_each.
APPEND wa TO it.
CLEAR pa_each.
ENDDO.
LOOP AT it INTO wa.
WRITE: / wa-field.
ENDLOOP.
I've tried this code, but it didn't work. I put value 5 in pa_total and value 4 in pa_each, and the result as a list was:
4
0
0
0
0
How can I do It so that parameters executes dynamically during the execution? I just want the user to send a initial value (pa_total) and then execute parameters field such times, and in each time, having a different value (given by the user). Basically I want to do this to calc a clas's age average.
The result should be something like this:
The user gives a initial value (the quantity of students) and then, the user would give the age of each student. At the end, we should have all the student's ages as a list.
‎2013 May 22 8:37 AM
Dear Peirrre,
try this code. It will work fine.
types : begin of st,
no type i,
age type i,
end of st.
parameters : pa_total type i.
parameters : pa_each type i MODIF ID md1 .
data : count type i value 1,
avg type p DECIMALS 2,
total type i,
n type i,
count_c(3) type c,
text(30) type c.
data : i_st type TABLE OF st,
wa_st like line of i_st.
AT SELECTION-SCREEN.
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
if screen-group1 = 'MD1'.
* screen-invisible = 1.
screen-active = 0.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
if pa_total is initial.
message 'Enter total no of students' type 'S'.
exit.
endif.
if count LE pa_total.
if count LT pa_total.
LOOP AT SCREEN.
if screen-group1 = 'MD1'.
SCREEN-active = 1.
screen-invisible = 0.
screen-input = 1.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
endif.
count_c = count.
IF PA_EACH IS INITIAL.
CONCATENATE 'Enter age of student ' count_c into text.
MESSAGE text type 'I'.
exit.
endif.
wa_st-no = count.
wa_st-age = pa_each.
APPEND wa_st to i_st.
clear : wa_st, pa_each.
add 1 to count.
count_c = count.
if count LE pa_total.
CONCATENATE 'Enter age of student ' count_c into text.
MESSAGE text type 'S'.
endif.
endif.
if count GT pa_total.
n = pa_total.
clear : pa_total, pa_each.
message 'Click Execute to get average' type 'I'.
endif.
START-OF-SELECTION.
write : / 'No ', 'Age'.
loop at i_st into wa_st.
write 😕 wa_st-no, wa_st-age.
total = wa_st-age + total.
endloop.
avg = total / n.
write 😕 'Average Age : ', avg.
‎2013 May 21 4:06 PM
Don't put parameters statement in a loop, it is wrong. ( do enddo). Put them in a selection screen.
Also the CLEAR pa_each empties your pa_each after the first loop, therefore first 4 and the rest is 0.
Also try to use the events. (selection-screen, at selection screen, initialization, start-of-selection etcetera)
I think the following code should work:
DATA: BEGIN OF wa,
field TYPE i,
END OF wa.
DATA: it LIKE TABLE OF wa.
selection-screen begin of block select.
PARAMETERS: pa_total TYPE i,
pa_each TYPE i.
selection-screen end of block select.
start-of-selection.
DO pa_total TIMES.
wa-field = pa_each.
APPEND wa TO it.
clear wa.
ENDDO.
end-of-selection.
LOOP AT it INTO wa.
WRITE: / wa-field.
ENDLOOP.
this will give the output (pa_total = 5 and pa_each = 4):
4
4
4
4
4
‎2013 May 21 6:06 PM
If you want the user to fill in a value everytime within the loop (do enddo) then you could call a popup (there are function modules, search for *popup* in se37) to fill the value and write it.
‎2013 May 21 4:39 PM
Hi Pierre,
Your requirement is not easily achievable, please refer the post -
http://scn.sap.com/thread/3265040.
If you want to go for it, i think you need to create Module Pool Program for the same.
Thanks,
Ankit Maskara.
‎2013 May 21 8:15 PM
hi,
Your requirement might not possible,
because we can not use parameters in loop
....
....
Endloop
‎2013 May 22 8:10 AM
Hi Pierre, Sudheer,
It is possible to create parameters dynamically. To achieve this it is necessary to create report dynamically. From the main report create dynamic report and call the same report. That's it.
And I am sure this solution will solve you problem.
Please follow the steps below:
1. First execute report (containing code mentioned later).
2. Give number of parameters to be created and execute.
3. You will get one more selection screen with number of parameter fields that were specified. Ex If you give 2 at step 2, then 2 input field will be available.
4. Just give the input in these parameters and execute.
5. After executing go back, that's it and now you can access these values.
The code is here with proper comments:
PARAMETERS:
p_total TYPE i.
DATA: code TYPE TABLE OF rssource-line, " Holds the report lines
lv_temp TYPE string,
lt_value TYPE TABLE OF i. " Holds values that are specified in parameters
**** Create and Write the report manually
APPEND 'REPORT ZDYNAMIC_PARAMETERS.' TO code.
**** Add number of parameter that are required
DO p_total TIMES.
lv_temp = sy-index.
CONCATENATE 'PARAMETERS: p_each' lv_temp INTO lv_temp.
CONCATENATE lv_temp 'TYPE i.' INTO lv_temp SEPARATED BY space.
APPEND lv_temp TO code.
ENDDO.
**** Create internal table to store the values entered in parameters
APPEND 'DATA lt_value TYPE TABLE OF i.' TO code.
**** Store the values entered in internal table
APPEND 'START-OF-SELECTION.' TO code.
DO p_total TIMES.
lv_temp = sy-index.
CONCATENATE 'APPEND P_EACH' lv_temp 'TO lt_value.' INTO lv_temp.
APPEND lv_temp TO code.
ENDDO.
**** Export internal table to abap membory
APPEND 'EXPORT lt_value TO MEMORY ID ''ABC''.' TO code.
**** Finally create an report using code internal table
INSERT REPORT 'ZDYNAMIC_PARAMETERS' FROM code.
**** Execute the report that was created dynamically
SUBMIT zdynamic_parameters VIA SELECTION-SCREEN AND RETURN.
**** Import the exported internal table
IMPORT lt_value FROM MEMORY ID 'ABC'.
DATA: lv_values TYPE i.
LOOP AT lt_value INTO lv_values.
WRITE: /, lv_values.
ENDLOOP.
‎2013 May 22 8:37 AM
Hi Satish,
Thanks a lot for answering the question. I have verified and it successfully creates dynamic selection screen. I would suggest you to post a wiki/how to using the code nugget.
Only one constraint which i got was the number of parametere,pa_total cannot be more than 99.If you give more than 99, you get Dump with following message.
The parameter name ("P_EACH100" here) can be up to 8 characters long."
It is a standard SAP constraint that Parameter names cannot exceed 8 characters and the dump can be avoided by shortening the parameter name.
Thanks,
Ankit Maskara.
‎2013 May 22 8:53 AM
Hi Ankit,
Yes Ankit what you told is correct. You can avoid this problem by shortening the parameter name. And thanks for your opinion I have created document on how to create dynamic report.
‎2013 May 22 10:01 AM
Hi Satish,
THANKS FOR CORRECTING ME , I HAD EXECUTED AND DEBUGGED THE CODE,
I UNDERSTOOD REPORT ,THANKS ONCE AGAIN
‎2013 May 22 10:43 AM
Nice concept.
Just a suggestion. Add the line ," append 'leave program.' to code. " for the smooth transition of screens.
APPEND 'EXPORT lt_value TO MEMORY ID ''ABC''.' TO code.
append 'leave program.' to code.
INSERT REPORT 'ZDYNAMIC_PARAMETERS' FROM code.
‎2013 May 22 10:50 AM
Hi Susmitha,
But while calling the report I am using return.
SUBMIT zdynamic_parameters VIA SELECTION-SCREEN AND RETURN.
So once the execution ends the program control returns back to the main program.
Can you explain why to use leave program? I did't understand the use of it.
‎2013 May 22 11:02 AM
When I executed, I had to click the back button to see the output of the first program, even though the submit and return statement was there. Was it not happening when you ran the program?
When I added leave program, as soon as i executed the dynamic report it would go directly to the output screen without having to click back button.
‎2013 May 22 11:15 AM
Okay I got it and I used to press back after execution. Nice.
‎2013 May 22 3:58 PM
Hi Satish, thanks by your code, It was really helpfull. I'm gonna study all the codes posted here in order to learn more Abap.
Thanks,
Pierre Francelino
‎2013 May 22 8:37 AM
Dear Peirrre,
try this code. It will work fine.
types : begin of st,
no type i,
age type i,
end of st.
parameters : pa_total type i.
parameters : pa_each type i MODIF ID md1 .
data : count type i value 1,
avg type p DECIMALS 2,
total type i,
n type i,
count_c(3) type c,
text(30) type c.
data : i_st type TABLE OF st,
wa_st like line of i_st.
AT SELECTION-SCREEN.
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
if screen-group1 = 'MD1'.
* screen-invisible = 1.
screen-active = 0.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
if pa_total is initial.
message 'Enter total no of students' type 'S'.
exit.
endif.
if count LE pa_total.
if count LT pa_total.
LOOP AT SCREEN.
if screen-group1 = 'MD1'.
SCREEN-active = 1.
screen-invisible = 0.
screen-input = 1.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
endif.
count_c = count.
IF PA_EACH IS INITIAL.
CONCATENATE 'Enter age of student ' count_c into text.
MESSAGE text type 'I'.
exit.
endif.
wa_st-no = count.
wa_st-age = pa_each.
APPEND wa_st to i_st.
clear : wa_st, pa_each.
add 1 to count.
count_c = count.
if count LE pa_total.
CONCATENATE 'Enter age of student ' count_c into text.
MESSAGE text type 'S'.
endif.
endif.
if count GT pa_total.
n = pa_total.
clear : pa_total, pa_each.
message 'Click Execute to get average' type 'I'.
endif.
START-OF-SELECTION.
write : / 'No ', 'Age'.
loop at i_st into wa_st.
write 😕 wa_st-no, wa_st-age.
total = wa_st-age + total.
endloop.
avg = total / n.
write 😕 'Average Age : ', avg.
‎2013 May 22 3:48 PM
Hi Susmitha! Your code has done exactly what I wanted. Thanks a lot .I'll study this code to understand It more. Thank you!
‎2013 May 22 9:14 AM
Try this code. It works real nice:
DATA: BEGIN OF wa,
field TYPE i,
END OF wa.
DATA: it LIKE TABLE OF wa.
SELECTION-SCREEN BEGIN OF BLOCK select.
PARAMETERS: pa_total TYPE i.
SELECTION-SCREEN END OF BLOCK select.
START-OF-SELECTION.
DO pa_total TIMES.
CALL FUNCTION 'GRM_POPUP_TO_GET_ONE_VALUE'
EXPORTING
textline1 = 'Supply age'
* TEXTLINE2 = ' '
* TEXTLINE3 = ' '
titel = 'Supply age'
valuelength = 2
valueobligatory = 'X'
start_column = 25
start_row = 6
CHANGING
value = wa-field
EXCEPTIONS
action_canceled_by_user = 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.
ELSE.
APPEND wa TO it.
CLEAR wa.
ENDIF.
ENDDO.
END-OF-SELECTION.
LOOP AT it INTO wa.
WRITE: / wa-field.
ENDLOOP.
‎2013 May 22 3:45 PM
Thanks a lot Peter, speccially by the tips. As I'm a student and I'm just starting to learn abap, I didn't know it was wrong to put parameters statement in a loop. This code was really useful for me. Thank you!