Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Hello,

last time I had to create a lot of users and lock them on few systems. I wrote a program that is using BAPI_USER_CREATE1 and BAPI_USER_LOCK. Program is expecting local .csv file separated by semicolons with Username, FirstName, LastName and ValidationDate. Program is importing csv file onto server and for every row is creating and then locking user. I used also RFC connection to run this program on other systems. How to do it just look below the source code.

Mass user creation

REPORT  Z_MASS_USER_CREATION.

data: paa_file type SAPB-SAPPFAD,

      pafile type SAPB-SAPPFAD,

      paa_file2 TYPE string.

PARAMETERS: p_file(70) TYPE c.

pafile = p_file.

paa_file2 = 'V2.csv'.

paa_file = paa_file2.

OPEN DATASET paa_file FOR OUTPUT IN TEXT MODE ENCODING UTF-8.

CLOSE DATASET paa_file.

CALL FUNCTION 'ARCHIVFILE_CLIENT_TO_SERVER'

  EXPORTING

    PATH             = pafile

   TARGETPATH       = paa_file

* EXCEPTIONS

*   ERROR_FILE       = 1

*   OTHERS           = 2

          .

IF SY-SUBRC <> 0.

* Implement suitable error handling here

ENDIF.

DATA: text type table of string,

      csvstr type string,

      pa_file type string,

      curpos TYPE i,

      endpos TYPE i.

pa_file = paa_file2.

TYPES: BEGIN OF user,

        uname type BAPIBNAME-BAPIBNAME,

        logond type BAPILOGOND,

        pwd type BAPIPWD,

        addr type BAPIADDR3,

       End of user.

DATA: wa_user type user.

  TRY .

      OPEN DATASET pa_file FOR INPUt in TEXT MODE ENCODING UTF-8.

      SET DATASET pa_file POSITION END OF FILE .

      GET DATASET pa_file POSITION endpos.

      SET DATASET pa_file POSITION 0.

      WHILE curpos <> endpos.

        READ DATASET pa_file INTO csvstr.

        APPEND csvstr TO text.

        GET DATASET pa_file POSITION curpos.

          SPLIT csvstr AT ';' INTO wa_user-uname wa_user-addr-FIRSTNAME wa_user-addr-LASTNAME wa_user-logond-GLTGB."wa_zsl-hub wa_zsl-dest wa_zsl-dist wa_zsl-cost.

        wa_user-pwd = 'Welcome123'.

        wa_user-logond-USTYP = 'A'.

        CONCATENATE wa_user-addr-FIRSTNAME wa_user-addr-LASTNAME INTO wa_user-addr-FULLNAME.

CALL FUNCTION 'BAPI_USER_CREATE1'

  EXPORTING

    USERNAME                      = wa_user-uname

*   NAME_IN                       =

    LOGONDATA                     = wa_user-logond

    PASSWORD                      = wa_user-pwd

*   DEFAULTS                      =

    ADDRESS                       = wa_user-addr

*   COMPANY                       =

*   SNC                           =

*   REF_USER                      =

*   ALIAS                         =

*   EX_ADDRESS                    =

*   UCLASS                        =

*   FORCE_SYSTEM_ASSIGNMENT       =

*   SELF_REGISTER                 = ' '

*   TECH_USER                     =

* TABLES

*   PARAMETER                     =

*   RETURN                        =

*   ADDTEL                        =

*   ADDFAX                        =

*   ADDTTX                        =

*   ADDTLX                        =

*   ADDSMTP                       =

*   ADDRML                        =

*   ADDX400                       =

*   ADDRFC                        =

*   ADDPRT                        =

*   ADDSSF                        =

*   ADDURI                        =

*   ADDPAG                        =

*   ADDCOMREM                     =

*   GROUPS                        =

*   PARAMETER1                    =

*   EXTIDHEAD                     =

*   EXTIDPART                     =

          .

DATA RETURN_DUMMY LIKE BAPIRET2 OCCURS 0.

CALL FUNCTION 'BAPI_USER_LOCK'

  EXPORTING

    USERNAME       = wa_user-uname

  TABLES

    RETURN         = RETURN_DUMMY

          .

      ENDWHILE.

      CLOSE DATASET pa_file.

    CATCH cx_sy_file_open_mode.

      APPEND 'No file has been found.' TO text.

  ENDTRY.

If you want to run this program via RFC on target system just change two lines of this code before executing:

CALL FUNCTION 'BAPI_USER_CREATE1'

to

CALL FUNCTION 'BAPI_USER_CREATE1' DESTINATION 'target_rfc_name'

and

CALL FUNCTION 'BAPI_USER_LOCK'

to

CALL FUNCTION 'BAPI_USER_LOCK' DESTINATION 'target_rfc_name'

where target_rfc_name is a name of RFC to target system.

I hope that will help someone and save a lot of time by creating users from file instead of manually creating

Regards

Gabriel

1 Comment