Add role to SAP user account - BAPI_USER_GET_DETAIL

Uses function module BAPI_USER_GET_DETAIL to add a new role to a SAP user account.

*...............................................................
*: Report:  ZADD_SAP_USER_ROLE                                 :
*:                                                             :
*: Author:  www.SAP Development                                   :
*:                                                             :
*: Date  :  2014                                               :
*:                                                             :
*: Description: Add role to SAP user account (SU01)            :
*:              has assigned to them in the current system     :
*:                                                             :
*:.............................................................:
REPORT  ZADD_SAP_USER_ROLE.
PARAMETERS: p_uname type sy-uname OBLIGATORY,
            p_role  type BAPIAGR-AGR_NAME default 'FI_ALL'.
DATA: ld_uname type sy-uname,
      it_return type STANDARD TABLE OF BAPIRET2,
      wa_return like line of it_return,
      it_ag type STANDARD TABLE OF BAPIAGR,
      wa_ag like line of it_ag,
      wa_logondata type BAPILOGOND ,
      wa_LOGONDATAX type BAPILOGONX.
****************************************************************
*START-OF-SELECTION.
START-OF-SELECTION.
*Get current list of roles assigned to user
  CALL FUNCTION 'BAPI_USER_GET_DETAIL'
    EXPORTING
      USERNAME       = p_uname
    IMPORTING
      logondata      = wa_logondata
    TABLES
      ACTIVITYGROUPS = it_ag
      RETURN         = it_return.
* Add role to it_ag table
  wa_ag-AGR_NAME = p_role.
  wa_ag-FROM_DAT = sy-datum.
  wa_ag-TO_DAT   = sy-datum.
  append wa_ag to it_ag.
* Write list of roles back to user, including new one
  CALL FUNCTION 'BAPI_USER_ACTGROUPS_ASSIGN'
    EXPORTING
      USERNAME       = p_uname
    TABLES
      ACTIVITYGROUPS = it_ag
      RETURN         = it_return.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      WAIT = 'X'.
*User should now have the new role, assuming no error or
*authority issues have occured

Return to Authorisations Home

Related Articles

SAP/ABAP Authority checks - Information on SAP authority checking functionality
Authorisation trace analysis
Transaction code authority check
ABAP code to Copy a SAP user account using BDC and email new password
Bespoke ABAP program to get and Display SAP user roles for username or pernr