0
votes

I have written the below ABAP program which can fetch the roles of two users from AGR_USERS, which are specified by user on the selection screen.

Output screen should have User id as well as roles assigned to the user. Program is working well for one user but cannot fetch the roles for the second user.

REPORT Z_REFERNCE_USER_PROG.
TYPE-POOLS: slis.  " SLIS contains all the ALV data types
TYPE-POOLS: SLIS,KCDE.
DATA: username(20) TYPE C,
      G_REPID TYPE SY-REPID,
      IT_EVENTS TYPE SLIS_T_EVENT,
      IT_FIELD TYPE SLIS_T_FIELDCAT_ALV,
      WA_FIELD TYPE SLIS_FIELDCAT_ALV,
      IT_SORT TYPE SLIS_T_SORTINFO_ALV.
INITIALIZATION.
G_REPID = SY-REPID.

DATA:BEGIN OF itab OCCURS 0,
       UNAME type AGR_USERS-UNAME,
       AGR_NAME  type AGR_USERS-AGR_NAME,
      UNAME1 type AGR_USERS-UNAME,
       AGR_NAME1  type AGR_USERS-AGR_NAME,
      END of itab.

start-of-selection.
     SELECT-OPTIONS AFF_USER FOR USERNAME NO INTERVALS.
     SELECT-OPTIONS REF_USER FOR USERNAME NO INTERVALS.
end-of-selection.

perform tosql.
perform listshow.
*--------------------------------
* form tosql
*--------------------------------
form tosql.
SELECT DISTINCT AGR_USERS~AGR_NAME    AGR_USERS~UNAME
         INTO corresponding fields of table itab
        FROM AGR_USERS
        where AGR_USERS~UNAME in AFF_USER.

SELECT DISTINCT AGR_USERS~AGR_NAME    AGR_USERS~UNAME
         INTO corresponding fields of table itab
        FROM AGR_USERS
        where AGR_USERS~UNAME in REF_USER.

endform.
*--------------------------------
* form listshow
*--------------------------------
form listshow.
  DEFINE ADD_FIELD.
    WA_FIELD-FIELDNAME = &1.
    WA_FIELD-REPTEXT_DDIC = &2.
    WA_FIELD-NO_ZERO = 'X'.
    APPEND WA_FIELD TO IT_FIELD.
  END-OF-DEFINITION.
     ADD_FIELD 'UNAME' 'Affected user'.
     ADD_FIELD 'AGR_NAME' 'Role'.
     ADD_FIELD 'UNAME1' 'Reference user'.
     ADD_FIELD 'AGR_NAME1' 'Role'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'"
       EXPORTING
            I_CALLBACK_PROGRAM = G_REPID
            I_BACKGROUND_ID   = 'ALV_BACKGROUND'
            IT_FIELDCAT        = IT_FIELD
*            IS_LAYOUT          = GS_LAYOUT
*            IT_SORT            = IT_SORT
            I_SAVE             = 'A'
            IT_EVENTS          = IT_EVENTS[]
       TABLES
            T_OUTTAB           = itab
       EXCEPTIONS
            PROGRAM_ERROR = 1
            OTHERS        = 2.
endform.

Input: enter image description here Output: enter image description here

I want to get all values from first SELECT into table 1 and all values of second select into table 2 and then combine them in table 3. I just want to see the roles of both the users adjacent to each other, the order does not matter.

2
The select option REF_USER is not used in the program for selecting the data...József Szikszai
@JózsefSzikszai : I have updated the code with your inputs. however,this still doesn't solve the issue. Roles are not showing up for the second user idACSO
Now it works, but for the second user only. There are several problems with the code, one of them is you are using two SELECTs. On this case the second select deletes the internal table (i. e. the data which was selected for the first user). Also read the documentation of START-OF-SELECTION and END-OF-SELECTION to understand how to use them.József Szikszai
It seems to me like you somehow want to get the data from two SELECTs into one table, with the results of the first SELECT in the first two columns and the results of the second SELECT in the last two columns. Is that correct? If yes, what is the rule for associating entries from select one to select two? Or in other words, how do you want to decide which values of select 1 appear next to which values of select 2?Philipp
@Philipp : Thanks Philipp to revert. You are very correct. That's what I want to achieve. I want to get all values from Select 1 in table 1 and all values of select 2 in table 2 and then combine all of them in table 3.ACSO

2 Answers

1
votes

SELECT ... INTO overwrites the content of the table with the results. So when you want to have the results of both SELECTS, you need two tables:

SELECT DISTINCT agr_name, uname
        INTO TABLE @DATA(lt_aff)
        FROM agr_users
        WHERE uname IN @aff_user.

SELECT DISTINCT agr_name, uname
        INTO TABLE @DATA(lt_ref)
        FROM agr_users
        WHERE uname IN @ref_user.

Now how do you get those two results into one table?

The problem is that you don't know which one of these two tables will be longer. So we need to use a WHILE loop with a counter which counts up while the counter is smaller than the line count of either table. Then within that loop, we need to get the lines of the two tables which correspond to the current value of the counter using READ TABLE. If successful, we need to move its values to a new line for the output table itab, which we then append to it:

DATA lv_counter TYPE i.
DATA ls_result LIKE LINE OF itab.

lv_counter = 0.

WHILE lv_counter < lines( lt_aff ) OR lv_counter < lines( lt_ref ).
   lv_counter = lv_counter + 1.
   CLEAR ls_result.

   READ TABLE lt_aff INDEX lv_counter INTO DATA(ls_aff).
   IF sy-subrc = 0.
      ls_result-uname = ls_aff-uname.
      ls_result-agr_name = ls_aff-agr_name.
   ENDIF.

   READ TABLE lt_ref INDEX lv_counter INTO DATA(ls_ref).
   IF sy-subrc = 0.
      ls_result-uname1 = ls_ref-uname.
      ls_result-agr_name1 = ls_ref-agr_name.
   ENDIF.

   INSERT ls_result INTO TABLE itab.
ENDWHILE.
0
votes

Actually you don't need SELECT-OPTIONS, as per your task definition you compare only two users

PARAMETERS: aff_user TYPE xubname DEFAULT 'NEUMANAR',
            ref_user TYPE xubname DEFAULT 'KAJAVESH'.

Here is how you can do it in a single database SELECT and a single table:

DATA ls_roles TYPE agr_users.

itab-uname = aff_user.
itab-uname1 = ref_user.
SELECT agr_name uname
  FROM agr_users
  INTO (ls_roles-agr_name,ls_roles-uname)
  WHERE uname = aff_user OR uname = ref_user.

  READ TABLE itab ASSIGNING FIELD-SYMBOL(<fs_itab>) WITH KEY agr_name = space.
  IF sy-subrc = 0 AND ls_roles-uname = aff_user.
    <fs_itab>-agr_name = ls_roles-agr_name.
    ELSEIF ls_roles-uname = ref_user.
      UNASSIGN <fs_itab>.
      READ TABLE itab ASSIGNING <fs_itab> WITH KEY agr_name1 = space.
      IF sy-subrc = 0.
        <fs_itab>-agr_name1 = ls_roles-agr_name.
      ENDIF.
  ENDIF.

  IF <fs_itab> IS NOT ASSIGNED.
    IF ls_roles-uname = aff_user.
      itab-agr_name = ls_roles-agr_name.
    ELSE.
     itab-agr_name1 = ls_roles-agr_name.
    ENDIF.
    APPEND itab.
  ENDIF.
ENDSELECT.

It supposed to be doing what you want

enter image description here