‎2010 Aug 31 9:50 AM
Hi Experts,
I need to to validate a selection screen field .The field is mentioned as selection parameter and it is obligatory.
To validate this i have written the logic as follows:
AT SELECTON SCREEN ON <FIELD-NAME>.
SELECT STATEMENT.
IF SY-SUBRC NE 0.
ERROR MESSAGE.
endif.
But here my question is select-options acts like an intenal table,Inthis case do we need to loop to the select-options fields or else do we have any other alternative solution.
Edited by: vinay raj on Aug 31, 2010 10:51 AM
‎2010 Aug 31 9:56 AM
Hi Please refer the below sample code that I had developed in one of my assignments which involves SELECT OPTIONS validation.
Yes If there are multiple value you need to LOOP.
DATA : itab_lifnr TYPE STANDARD TABLE OF lfa1,
wa_lifnr LIKE LINE OF itab_lifnr.
IF el_ekorg-low IS NOT INITIAL AND el_ekorg-high IS INITIAL.
SELECT COUNT(*) FROM t024e UP TO 1 ROWS WHERE ekorg = el_ekorg-low.
IF sy-subrc NE 0.
SET CURSOR FIELD 'EL_EKORG-LOW'.
MESSAGE e012(e2) WITH el_ekorg-low .
ENDIF.
ENDIF.
IF el_ekorg-high IS INITIAL AND el_ekorg-low IS NOT INITIAL AND el_lifnr-low IS NOT INITIAL.
SELECT * FROM lfa1 INTO TABLE itab_lifnr WHERE lifnr IN el_lifnr.
LOOP AT itab_lifnr INTO wa_lifnr.
SELECT COUNT(*) FROM lfm1 UP TO 1 ROWS WHERE lifnr = wa_lifnr-lifnr AND ekorg = el_ekorg-low.
IF sy-subrc NE 0.
MESSAGE e102 WITH wa_lifnr-lifnr el_ekorg-low.
ENDIF.
ENDLOOP.
ENDIF.
Hope this solves your query.
Regards
Abhii
‎2010 Aug 31 9:56 AM
Hi Please refer the below sample code that I had developed in one of my assignments which involves SELECT OPTIONS validation.
Yes If there are multiple value you need to LOOP.
DATA : itab_lifnr TYPE STANDARD TABLE OF lfa1,
wa_lifnr LIKE LINE OF itab_lifnr.
IF el_ekorg-low IS NOT INITIAL AND el_ekorg-high IS INITIAL.
SELECT COUNT(*) FROM t024e UP TO 1 ROWS WHERE ekorg = el_ekorg-low.
IF sy-subrc NE 0.
SET CURSOR FIELD 'EL_EKORG-LOW'.
MESSAGE e012(e2) WITH el_ekorg-low .
ENDIF.
ENDIF.
IF el_ekorg-high IS INITIAL AND el_ekorg-low IS NOT INITIAL AND el_lifnr-low IS NOT INITIAL.
SELECT * FROM lfa1 INTO TABLE itab_lifnr WHERE lifnr IN el_lifnr.
LOOP AT itab_lifnr INTO wa_lifnr.
SELECT COUNT(*) FROM lfm1 UP TO 1 ROWS WHERE lifnr = wa_lifnr-lifnr AND ekorg = el_ekorg-low.
IF sy-subrc NE 0.
MESSAGE e102 WITH wa_lifnr-lifnr el_ekorg-low.
ENDIF.
ENDLOOP.
ENDIF.
Hope this solves your query.
Regards
Abhii
‎2010 Aug 31 10:36 AM
I have gone through your code .
LOOP AT itab_lifnr INTO wa_lifnr.
SELECT COUNT(*) FROM lfm1 UP TO 1 ROWS WHERE lifnr = wa_lifnr-lifnr AND ekorg = el_ekorg-low.
IF sy-subrc NE 0.
MESSAGE e102 WITH wa_lifnr-lifnr el_ekorg-low.
ENDIF.
ENDLOOP.
But here you are loopin to the internal table of the select query data.
Please clarify where you are looping to the select-options field.
‎2010 Aug 31 10:43 AM
Hi Vinay,
It was just an example code snippet, just do as below :-
LOOP AT S_VBELN.
SELECT COUNT(*) FROM VBAK UP TO 1 ROWS WHERE VBELN = S_VBELN-LOW.
IF sy-subrc NE 0.
MESSAGE e102 WITH S_VBELN-LOW.
ENDIF.
ENDLOOP.
.
Hey This works if there is only one value entered on Selection screen.
IF your S_VBELN contains many values then, You have to use IN operator & its the only feasible method instead of LOOP.
Hope this answers your query.
Regards
Abhii
Edited by: Abhii on Aug 31, 2010 11:43 AM
‎2010 Aug 31 10:51 AM
IF your S_VBELN contains many values then, You have to use IN operator & its the only feasible method instead of LOOP.
Hope this answers your query.
Can you tell me how we can achive it.
suppose i have passed 10 entries to the S_VBELN and in that 5 are exisitng in the table and 5 are not exisint .
When ever we write a select query for the select options we use IN operator.
And in this case already it has 5 entries which are exisitng in the table and it will show the SY-SUBRC values as 0.
What about the 5 entries which are not there in the table,How we will show the error message,
Please guide.
‎2010 Aug 31 10:53 AM
Try this,
REPORT ZTEST.
TABLES: mara.
SELECT-OPTIONS: s_matnr for mara-matnr.
data: begin of it_mara OCCURS 0,
matnr type matnr,
end of it_mara.
AT SELECTION-SCREEN.
START-OF-SELECTION.
data: v_lines type i.
DESCRIBE TABLE s_matnr LINES v_lines.
select matnr from mara into TABLE it_mara where matnr in s_matnr.
if sy-dbcnt NE v_lines.
MESSAGE: 'Invalid matnr ' TYPE 'S' DISPLAY LIKE 'E'.
exit.
endif.
Vikranth
‎2010 Aug 31 10:55 AM
Thankyou for your reference.
But here i have a problem,
Here i need to show the particular material number which is not valid in the selection screen.
So the user can manually correct it .
‎2010 Aug 31 10:57 AM
‎2010 Aug 31 11:04 AM
Hi Vik,
We should also check if S_MATNR is having any value before passing to SELECT query.
REPORT ZTEST.
TABLES: mara.
SELECT-OPTIONS: s_matnr for mara-matnr.
data: begin of it_mara OCCURS 0,
matnr type matnr,
end of it_mara.
AT SELECTION-SCREEN.
START-OF-SELECTION.
data: v_lines type i.
IF NOT S_MATNR IS INITIAL. "add check
DESCRIBE TABLE s_matnr LINES v_lines.
select matnr from mara into TABLE it_mara where matnr in s_matnr.
if sy-dbcnt NE v_lines.
MESSAGE: 'Invalid matnr ' TYPE 'S' DISPLAY LIKE 'E'.
exit.
ENDIF
ENDIF.
Regards
Abhii
Edited by: Abhii on Aug 31, 2010 12:04 PM
‎2010 Aug 31 11:26 AM
Hello ,
I guess the only option you have as i suggested earlier is to compare the result of the select query with that of the entries in the select-option and identify the invalid entries and put them in message. Shouldn't be too tough. Give a try and come back if you are stuck up anywhere.
Vikranth
‎2010 Aug 31 12:51 PM
‎2010 Aug 31 9:58 AM
Hi,
In select statment where clause ... use the IN operator ... if nothing is found then raise an error message.
AT SELECTON SCREEN ON <FIELD-NAME>.
SELECT STATEMENT .... where field in s_field.
IF SY-SUBRC NE 0.
ERROR MESSAGE.
endif.
Regards,
Srini.
‎2010 Aug 31 9:59 AM
Hello,
In the select statment where condition when you use IN operator all the values of the select option will be considered and you dont have to loop. If you have to validate each and every entry of the select-option, you might have to declare a temporary internal table and compare the results of the select statement with that of your select-option values .
Vikranth
‎2010 Aug 31 10:07 AM
I do agree that when ever we are writting a select Query for select options we use IN operator and it takes all the values.
But if any one of the record is not exisitng in that select options it wont through any error message right.
‎2010 Aug 31 10:13 AM
That is why use the method of LOOP.
Refer the code snippet I have posted.
Regards
Abhii
Edited by: Abhii on Aug 31, 2010 11:13 AM
‎2010 Aug 31 10:21 AM
Hi,
So how many error messages will the program throw.
Say you have 10 values and 5 of them are not in the table.
You want to throw 5 messages?
If you write select using IN all these 5 entries are filtered out.
Regards,
Srini.
‎2010 Aug 31 10:27 AM
Yes you are correct.
If at least one entry is not there then we need to show an error message.
‎2010 Aug 31 10:32 AM
Hi,
I am hereby sending a small piece of code which I wrote for validating the selection screen.
AT SELECTION-SCREEN ON SVBELN.
Perform validate_svbeln.
**********************
FORM validate_svbeln .
data : lvbeln like vbak-vbeln.
select single vbeln from vbak into lvbeln where vbeln in svbeln.
if sy-subrc ne 0.
message e000.
endif.
ENDFORM.
‎2010 Aug 31 10:38 AM
Hi,
Looping is not required for this validation. And as you have said select statement using IN oerator does not throw any error message if the value is not present in select-options.
AT SELECTON SCREEN ON <FIELD-NAME>.
SELECT STATEMENT .... where field in s_field.
IF SY-SUBRC NE 0.
ERROR MESSAGE.
endif.
But if you want to check whether the value entered in select-options is invalid then you can refer the below sample code
which can be used to check for the validity of the value of valuation class entered in the selection screen where valuation class is used as select-options. In the below code table T025 has all the values of valuation class. If the entered value does not exist in this then it indicates that the value entered in selection screen is invalid.
AT SELECTON SCREEN ON <FIELD-NAME>.
PERFORM check_valuation_class.
FORM check_valuation_class.
DATA : lv_bklas TYPE bklas.
IF NOT p_bklas-low IS INITIAL.
SELECT SINGLE COUNT(*) FROM t025
INTO (lv_bklas) WHERE bklas EQ p_bklas-low.
IF sy-subrc NE 0.
MESSAGE e021(zfm001).
ENDIF.
ENDIF.
IF NOT p_bklas-high IS INITIAL.
SELECT SINGLE COUNT(*) FROM t025
INTO (lv_bklas) WHERE bklas EQ p_bklas-high.
IF sy-subrc NE 0.
MESSAGE e021(zfm001).
ENDIF.
ENDIF.
ENDFORM. " check_valuation_class
‎2010 Aug 31 10:45 AM
Thanks for your reply.
Your code works when i will give low and high values in the selection screen.
But in case , if i choose the multiple select options and in the single values tab if i pass mutilple selection records.
‎2010 Aug 31 1:05 PM
Hello frnd,
IF u want to validate by the select statement only....
then u can use
select...............
endselect
if sy-subrc <> 0
error msg
end if
like if u want to check company code is present or not?
if present ...error(enter new company code)