Generic extractor to get SAP username and their email

Applies to:

SAP BW 7.x system. For more information, visit the Business Intelligence homepage https://help.sap.com/

saphelp_nw73ehp1/helpdata/en/b2/e50138fede083de10000009b38f8cf/frameset.htm

Summary

This Document describes how to get the SAP user names and their email, via generic extractor.

Author        : Lakshminarasimhan Narasimhamurthy

Created on : 01/NOV/2015

Author Bio

Lakshminarasimhan Narasimhamurthy is a BW certified and ABAP certified consultant and worked on multiple implementation, development and support project’s within India and outside of India. He has worked for fortune 500 clients like Nestle, Warner Bros, GCC, General Electric etc. He is strong in HANA modeling, BODS and BO/BI tools like WEBI, Dashboard, IDT, Lumira.

Details

There is a requirement to get the list of users in SAP source system(ECC & CRM) along with their email address. Unfortunately there is no table as far as I know, where we can get this data. So I have created a generic DataSource to extract the information.

The primary is table USR04 from where we can get all of the usernames in the system.

OPEN CURSOR WITH HOLD S_CURSOR FOR SELECT BNAME FROM USR04 WHERE BNAME IN LR_BNAME” All user list

To get the full name of the user we need to rely on the FM SUSR_USER_ADDRESS_READ_ARRAY

    CALL FUNCTION ‘SUSR_USER_ADDRESS_READ_ARRAY’  ” Get the full name of the users
EXPORTING
ALL_USERS             
= ‘ ‘   “  Default value
IMPORTING
RETURNCODE            
= L_RETURN_CODE 
TABLES
USER_NAME_TAB         
= LT_USERS “ All BNAME from USR04 table
*       USER_ADDRESS_TAB       =
USER_NAME_ADDRESS_TAB 
= LT_USER_DATA 
EXCEPTIONS
INTERNAL_ERROR        
= 1
USER_NAME_TAB_IS_EMPTY
= 2
OTHERS                 = 3.

You need to pass all the BNAME from USR04 to the FM and the FM returns the full name via the internal table LT_USER_DATA, the field NAME_TEXT gives you the full name of the user.

To get the email address, you need to call the primary FM BAPI_USER_GET_DETAIL.

CALL FUNCTION ‘BAPI_USER_GET_DETAIL’
EXPORTING
USERNAME            
= <LFS_ZBW_USER_DETAILS>BNAME  ” User Name
*           CACHE_RESULTS        = ‘X’
*         IMPORTING
*           LOGONDATA            =
*           DEFAULTS             =
*           ADDRESS              =
*           COMPANY              =
*           SNC                  =
*           REF_USER             =
*           ALIAS                =
*           UCLASS               =
*           LASTMODIFIED         =
*           ISLOCKED             =
*           IDENTITY             =
*           ADMINDATA            =
TABLES
*           PARAMETER            =
*           PROFILES             =
*           ACTIVITYGROUPS       =
RETURN               = LIT_BAPIRET2  “ Deafult value
*           ADDTEL               =
*           ADDFAX               =
*           ADDTTX               =
*           ADDTLX               =
ADDSMTP             
= LIT_ADDSMTP ” Email address
*           ADDRML               =
*           ADDX400              =
*           ADDRFC               =
*           ADDPRT               =
*           ADDSSF               =
*           ADDURI               =
*           ADDPAG               =
*           ADDCOMREM            =
*           PARAMETER1           =
*           GROUPS               =
*           UCLASSSYS            =
*           EXTIDHEAD            =
*           EXTIDPART            =
*           SYSTEMS              =

                        .

The field “E_MAIL” gives the email address of the user and the field “EMAIL_SRCH” gives the email address in capital letters. This will help us in easily loading the data to InfoObject without checking the “lowercase letter” checkbox.

Then via RSO2 the generic DataSource is created. There is no delta enabled on this datasource because the users list will be only few hundreds or thousand’s only. 

The entire code is given below,

FUNCTION ZRSAX_BIW_GET_USER_DETAILS.
*”———————————————————————-
*”*”Local Interface:
*”  IMPORTING
*”     VALUE(I_REQUNR) TYPE  SRSC_S_IF_SIMPLE-REQUNR
*”     VALUE(I_DSOURCE) TYPE  SRSC_S_IF_SIMPLE-DSOURCE OPTIONAL
*”     VALUE(I_MAXSIZE) TYPE  SRSC_S_IF_SIMPLE-MAXSIZE OPTIONAL
*”     VALUE(I_INITFLAG) TYPE  SRSC_S_IF_SIMPLE-INITFLAG OPTIONAL
*”     VALUE(I_READ_ONLY) TYPE  SRSC_S_IF_SIMPLE-READONLY OPTIONAL
*”     VALUE(I_REMOTE_CALL) TYPE  SBIWA_FLAG DEFAULT SBIWA_C_FLAG_OFF
*”  TABLES
*”      I_T_SELECT TYPE  SRSC_S_IF_SIMPLE-T_SELECT OPTIONAL
*”      I_T_FIELDS TYPE  SRSC_S_IF_SIMPLE-T_FIELDS OPTIONAL
*”      E_T_DATA STRUCTURE  ZBW_USER_DETAILS OPTIONAL
*”  EXCEPTIONS
*”      NO_MORE_DATA
*”      ERROR_PASSED_TO_MESS_HANDLER
*”———————————————————————-

******************************************************************************************
* Developer          – Lakshminarasimhan Narasimhamurthy
* Purpose            – To get the users and their email address

DATA : LIT_BAPIRET2           TYPE STANDARD TABLE OF BAPIRET2,
LIT_ADDSMTP           
TYPE STANDARD TABLE OF BAPIADSMTP,
LWA_ADDSMTP           
TYPE BAPIADSMTP,
L_RETURN_CODE         
TYPE AD_RETCODE,
LT_USERS              
TYPE TABLE OF USUSERS,
LT_USER_DATA          
TYPE TABLE OF USADDR3,
LA_USER_DATA          
TYPE USADDR3.

FIELD-SYMBOLS  : <LFS_ZBW_USER_DETAILS> TYPE ZBW_USER_DETAILS.

* Auxiliary Selection criteria structure
DATA: L_S_SELECT TYPE SRSC_S_SELECT.

* Maximum number of lines for DB table
STATICS: S_S_IF TYPE SRSC_S_IF_SIMPLE,

* counter
S_COUNTER_DATAPAKID
LIKE SYTABIX,

* cursor
S_CURSOR
TYPE CURSOR.
* Select ranges
RANGES: LR_BNAME FOR USR04BNAME.

FIELD-SYMBOLS : <ZBW_WORKITEM_DET> TYPE ZBW_USER_DETAILS.

* Initialization mode (first call by SAPI) or data transfer mode
* (following calls) ?
IF I_INITFLAG = SBIWA_C_FLAG_ON.

************************************************************************
* Initialization: check input parameters
*                 buffer input parameters
*                 prepare data selection
************************************************************************

* Check DataSource validity
CASE I_DSOURCE.

WHEN ‘ZBW_USER_DETAILS’.

WHEN OTHERS.
IF 1 = 2. MESSAGE E009(R3). ENDIF.
* this is a typical log call. Please write every error message like this
LOG_WRITE
‘E’                  “message type
‘R3’                 “message class
‘009’                “message number
I_DSOURCE  
“message variable 1
‘ ‘.                 “message variable 2
RAISE ERROR_PASSED_TO_MESS_HANDLER.

ENDCASE.

APPEND LINES OF I_T_SELECT TO S_S_IFT_SELECT.

* Fill parameter buffer for data extraction calls
S_S_IF
REQUNR    = I_REQUNR.
S_S_IF
DSOURCE   = I_DSOURCE.
S_S_IF
MAXSIZE   = I_MAXSIZE.

* Fill field list table for an optimized select statement
* (in case that there is no 1:1 relation between InfoSource fields
* and database table fields this may be far from beeing trivial)
APPEND LINES OF I_T_FIELDS TO S_S_IFT_FIELDS.

ELSE.                 “Initialization mode or data extraction ?

************************************************************************
* Data transfer: First Call      OPEN CURSOR + FETCH
*                Following Calls FETCH only
************************************************************************

* First data package -> OPEN CURSOR
IF S_COUNTER_DATAPAKID = 0.

CLEAR : L_S_SELECT.

LOOP AT S_S_IFT_SELECT INTO L_S_SELECT WHERE FIELDNM = ‘BNAME’.
MOVE-CORRESPONDING L_S_SELECT TO LR_BNAME.
APPEND LR_BNAME.
ENDLOOP.

OPEN CURSOR WITH HOLD S_CURSOR FOR SELECT BNAME FROM USR04 WHERE BNAME IN LR_BNAME” All user list

ENDIF.                             “First data package ?

* Fetch records into interface table.
*   named E_T_DATA ‘Name of extract structure’.
FETCH NEXT CURSOR S_CURSOR
APPENDING CORRESPONDING FIELDS
OF TABLE E_T_DATA
PACKAGE
SIZE S_S_IFMAXSIZE.

IF SYSUBRC <> 0.
CLOSE CURSOR S_CURSOR.
RAISE NO_MORE_DATA.
ENDIF.

CLEAR : L_RETURN_CODE,LT_USERS,LT_USER_DATA.

LT_USERS[] = E_T_DATA[].

CALL FUNCTION ‘SUSR_USER_ADDRESS_READ_ARRAY’  ” Get the full name of the users
EXPORTING
ALL_USERS             
= ‘ ‘
IMPORTING
RETURNCODE            
= L_RETURN_CODE
TABLES
USER_NAME_TAB         
= LT_USERS
*       USER_ADDRESS_TAB       =
USER_NAME_ADDRESS_TAB 
= LT_USER_DATA
EXCEPTIONS
INTERNAL_ERROR        
= 1
USER_NAME_TAB_IS_EMPTY
= 2
OTHERS                 = 3.

IF  E_T_DATA[] IS NOT INITIAL.

LOOP AT E_T_DATA ASSIGNING <LFS_ZBW_USER_DETAILS>.

CLEAR : LA_USER_DATA.

READ TABLE LT_USER_DATA INTO LA_USER_DATA WITH KEY BNAME = <LFS_ZBW_USER_DETAILS>BNAME.

IF SYSUBRC = 0.
<LFS_ZBW_USER_DETAILS>
NAME = LA_USER_DATANAME_TEXT” Full name of the user
ENDIF.

CLEAR : LIT_BAPIRET2, LIT_ADDSMTP.

CALL FUNCTION ‘BAPI_USER_GET_DETAIL’
EXPORTING
USERNAME            
= <LFS_ZBW_USER_DETAILS>BNAME  ” User Name
*           CACHE_RESULTS        = ‘X’
*         IMPORTING
*           LOGONDATA            =
*           DEFAULTS             =
*           ADDRESS              =
*           COMPANY              =
*           SNC                  =
*           REF_USER             =
*           ALIAS                =
*           UCLASS               =
*           LASTMODIFIED         =
*           ISLOCKED             =
*           IDENTITY             =
*           ADMINDATA            =
TABLES
*           PARAMETER            =
*           PROFILES             =
*           ACTIVITYGROUPS       =
RETURN               = LIT_BAPIRET2
*           ADDTEL               =
*           ADDFAX               =
*           ADDTTX               =
*           ADDTLX               =
ADDSMTP             
= LIT_ADDSMTP ” Email address
*           ADDRML               =
*           ADDX400              =
*           ADDRFC               =
*           ADDPRT               =
*           ADDSSF               =
*           ADDURI               =
*           ADDPAG               =
*           ADDCOMREM            =
*           PARAMETER1           =
*           GROUPS               =
*           UCLASSSYS            =
*           EXTIDHEAD            =
*           EXTIDPART            =
*           SYSTEMS              =
.
IF SYSUBRC  = 0.
CLEAR : LWA_ADDSMTP.

READ TABLE LIT_ADDSMTP INTO LWA_ADDSMTP INDEX 1.

IF SYSUBRC = 0.
<LFS_ZBW_USER_DETAILS>
EMAIL_AADDR = LWA_ADDSMTPE_MAIL“Email address of the user
ENDIF.

ENDIF.

ENDLOOP.

ENDIF.

S_COUNTER_DATAPAKID = S_COUNTER_DATAPAKID + 1.

ENDIF.              “Initialization mode or data extraction ?

ENDFUNCTION.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply