Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Fetch users from USR02 based on validity date

manp12
Explorer
0 Likes
4,557

I am working on SAP Report, wherein I am fetching user data from the USR02 Table, My requirement is I want to display users based on
1. User name

2. User Type: These two are working Fine

3. Valid from - (Date)

4. Valid To - (Date)

In Valid from - (Date) & Valid To - (Date) only those user should be displayed who are valid on the specified date (Parameter/ Select Options anything will do)

For Eg: A user is valid from 01/01/2020 to 31/12/2021
And when I insert a date 12/03/2021, User info should be displayed,
But I am getting an inappropriate output:
When I enter the exact valid date i.e 01/01/2020 then output comes as desired,
But when I enter the date as 12/03/2021 or any date between 01/01/2020 to 31/12/2021, no data comes

Here's the code which I tried:

REPORT Report_name

TABLES: USR02.

TYPE-POOLS: SLIS.

DATA: FIELDCATALOG TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,

* GD_LAYOUT TYPE SLIS_LAYOUT_ALV,

GD_REPID LIKE SY-REPID.

TYPES: BEGIN OF ITAB,

BNAME TYPE USR02-BNAME,

GLTGV TYPE USR02-GLTGV,

GLTGB TYPE USR02-GLTGB,

USTYP TYPE USR02-USTYP,

END OF ITAB.

SELECTION-SCREEN: BEGIN OF BLOCK one WITH FRAME TITLE TEXT-001.

SELECT-OPTIONS: s_BNAME for USR02-BNAME,

s_GLTGV for USR02-GLTGV NO-EXTENSION NO INTERVALS,

* s_GLTGB for USR02-GLTGB DEFAULT '1500000000' TO '1599999999' ,

* s_GLTGV for USR02-GLTGV NO-EXTENSION NO INTERVALS,

s_GLTGB for USR02-GLTGB NO-EXTENSION NO INTERVALS,

s_USTYP for USR02-USTYP NO-EXTENSION NO INTERVALS.

DATA: wa_na TYPE ITAB,

it_na TYPE STANDARD TABLE OF ITAB.

select BNAME GLTGV GLTGB USTYP FROM USR02 INTO TABLE it_na

WHERE BNAME in s_BNAME and

GLTGV in s_GLTGV and

* GLTGV <='2021-04-11' and

GLTGB in s_GLTGB and

* GLTGV BETWEEN s_GLTGV AND s_GLTGB.

USTYP in s_USTYP.

SELECTION-SCREEN: End of BLOCK one.

FIELDCATALOG-FIELDNAME = 'BNAME'.

FIELDCATALOG-SELTEXT_M = 'USER NAME'.

FIELDCATALOG-COL_POS = 0.

APPEND FIELDCATALOG TO FIELDCATALOG.

CLEAR FIELDCATALOG.

FIELDCATALOG-FIELDNAME = 'GLTGV'.

FIELDCATALOG-SELTEXT_M = 'VALID FROM'.

FIELDCATALOG-COL_POS = 1.

APPEND FIELDCATALOG TO FIELDCATALOG.

CLEAR FIELDCATALOG.

FIELDCATALOG-FIELDNAME = 'GLTGB'.

FIELDCATALOG-SELTEXT_M = 'VALID TO'.

FIELDCATALOG-COL_POS = 2.

APPEND FIELDCATALOG TO FIELDCATALOG.

CLEAR FIELDCATALOG.

FIELDCATALOG-FIELDNAME = 'USTYP'.

FIELDCATALOG-SELTEXT_M = 'USER TYPE'.

FIELDCATALOG-COL_POS = 3.

APPEND FIELDCATALOG TO FIELDCATALOG.

CLEAR FIELDCATALOG.

GD_REPID = SY-REPID.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = GD_REPID

* I_CALLBACK_TOP_OF_PAGE = 'TOP-OF-PAGE' "see FORM

* I_CALLBACK_USER_COMMAND = 'USER_COMMAND'

IT_FIELDCAT = FIELDCATALOG[]

I_SAVE = 'X'

* IS_VARIANT = G_VARIANT

TABLES

T_OUTTAB = it_na

EXCEPTIONS

PROGRAM_ERROR = 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.

ENDIF.






Your suggestions will help me
Thankyou in advance!

5 REPLIES 5
Read only

Sandra_Rossi
Active Contributor
3,533

Which error do you get?

Please edit your question, select your code and press the button [CODE], which makes the code appear colored/indented, it will be easier for people to look at it. Thank you!

NB: you'd better use CL_SALV_TABLE which has deprecated REUSE_ALV_GRID_DISPLAY more than ten years ago.

Read only

abo
Active Contributor
3,533

Please format the code using the CODE button and explain which error you're getting.

Read only

Sandra_Rossi
Active Contributor
0 Likes
3,533

Based on same question at Fetch users from USR02 based on validity date - SAP ABAP - Stack Overflow:

the issue is:

"For Eg: A user is valid from 01/01/2020 to 31/12/2021 And when I insert a date 12/03/2021, User info should be displayed, But I am getting an inappropriate output: When I enter the exact valid date i.e 01/01/2020 then output comes as desired, But when I enter the date as of 12/03/2021 or any date between 01/01/2020 to 31/12/2021, no data comes"

Read only

Sandra_Rossi
Active Contributor
0 Likes
3,533

You'd better simplify your selection criteria: don't you need only one selection parameter to enter just one date, instead of 2 selection criteria for start and end validity dates?

Like this:

PARAMETERS p_date TYPE d.
...
select BNAME GLTGV GLTGB USTYP FROM USR02 INTO TABLE it_na
    WHERE ( GLTGV <= P_DATE AND 
          GLTGB >= P_DATE ).
Read only

RaymondGiuseppi
Active Contributor
0 Likes
3,533

What exact rule do you want to apply

* For user valid at a single input date
select BNAME GLTGV GLTGB USTYP FROM USR02 INTO TABLE it_na
    WHERE ( GLTGV <= P_DATE AND GLTGB >= P_DATE ).
* For user valid during the whole input range
select BNAME GLTGV GLTGB USTYP FROM USR02 INTO TABLE it_na
    WHERE ( GLTGV <= P_DATE_LOW AND GLTGB >= P_DATE_HIGH ).
* For user valid for a subset of the input range	
select BNAME GLTGV GLTGB USTYP FROM USR02 INTO TABLE it_na
    WHERE ( GLTGV <= P_DATE_HIGH AND GLTGB >= P_DATE_LOW ).

(just insure that P_DATE_HIGH >= P_DATE_LOW)