cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Using a Function Module in SAP RAP managed

gzghidi
Explorer
0 Likes
3,041

Hello Experts,

I'm trying my best to use the FM BAPI_USER_CREATE1 so i'll be able to create a user on S/4, since i'm working in a managed scenario i have implemented a determination on save so when a new record is created a method that is using BAPI_USER_CREATE1 will be triggered as shown below

determination createUser on save {create;}

 

METHOD CreateUser.
  READ ENTITIES OF zi_user1_rh IN LOCAL MODE
    ENTITY User
    ALL FIELDS WITH CORRESPONDING #( keys )
    RESULT DATA(entites).

    LOOP AT entites INTO DATA(entity).
      DATA: ls_address TYPE bapiaddr3.
      ls_address-firstname = entity-Firstname.
      ls_address-lastname = entity-Lastname.
      DATA: ls_logondata TYPE bapilogond.
      ls_logondata-ustyp = 'A'.
      DATA: lv_password TYPE bapipwd VALUE 'passworD123@@',
            lv_username TYPE bapibname-bapibname.
      lv_username = entity-Username.

    ENDLOOP.
    CALL FUNCTION 'BAPI_USER_CREATE1'
      IN UPDATE TASK
      EXPORTING
        username  = lv_username
        logondata = ls_logondata
        password  = lv_password
        address   = ls_address.

  ENDMETHOD.

PS: Please ignore the password that is being mentioned directly in the code as i'm still testing 
Unfortunately that didn't work and i got the below st22 dump.

gzghidi_0-1745319219181.png

So i decided to take a different approach, so i create a different method which the same logic as the previous one and made it executable via an action button as shown below

action CreateUser result[1] $self;

However i'm still getting exactly the same dump, i came across many blogs like the below one which indicates that a validation or a determination should work with a FM in a managed scenario.
https://community.sap.com/t5/technology-q-a/adding-custom-code-function-module-in-managed-scenario-s... 
And recommendations please ?

 

Accepted Solutions (1)

Accepted Solutions (1)

gzghidi
Explorer
0 Likes

Hello everyone,

I have solved the issue by wrapping BAPI_USER_CREATE1 logic in a remote-enabled custom function module

I have created a custom function module with the below parameter but first make sure to enable remote module as shown below

gzghidi_2-1745330560937.png

 

Next for the import i used the below ofc you can add more attributes if you want to use more just use BAPI_USER_CREATE1 as your reference. 

gzghidi_0-1745330335308.png

As for the source code i used the below

gzghidi_1-1745330373460.png

and at last i called the custom FM in my method as shown below

METHOD add_user.
  READ ENTITIES OF zi_user1_rh IN LOCAL MODE
    ENTITY User
    ALL FIELDS WITH CORRESPONDING #( keys )
    RESULT DATA(users).

  LOOP AT users INTO DATA(user).
    CALL FUNCTION 'Z_CREATE_SU01_USER'
      STARTING NEW TASK 'USRCREATE'
      DESTINATION IN GROUP DEFAULT
      EXPORTING
        iv_username  = user-username
        iv_firstname = user-firstname
        iv_lastname  = user-lastname
        iv_password  = 'passworD123@@'.
  ENDLOOP.

    READ ENTITIES OF zi_user1_rh IN LOCAL MODE
    ENTITY User
    ALL FIELDS WITH CORRESPONDING #( keys )
    RESULT DATA(userlist).
    result = VALUE #( FOR user1 IN userlist ( %tky = user-%tky %param = user  ) ).


  ENDMETHOD.

 

Answers (3)

Answers (3)

AbhishekSharma
Active Contributor
0 Likes

Hi @gzghidi I understood you are able to resolve your issue. I also agree with @suzanne_alivand suggestions to call FM with Destination 'NONE' which was suggested approach from SAP because RAP does not support COMMIT WORK call, because ABAP RAP handles all COMMITS.

But now SAP has come up with solution to this problem or situation.  SAP is now suggesting if you need to call any FM or BAPI which has COMMIT statement inside somewhere in standard code, you should generate Proxy class for that BAPI/FM.

A new transaction code is available now /nACO_PROXY which helps you generate Proxy class and Interfaces. For more details how to use this please follow below post:

https://community.sap.com/t5/technology-blogs-by-sap/how-to-generate-a-wrapper-for-function-modules-... 

Hope this helps...

Thanks-

 

gzghidi
Explorer
0 Likes

WOW, that's even better i will definitely consider applying it on my next FM usage

suzanne_alivand
Explorer
0 Likes

Try below note, hope it helps:
https://me.sap.com/notes/0003115094 

suzanne_alivand
Explorer
0 Likes

Hi,
The error occurs because the RAP framework manages its own database commits. When you call BAPI_USER_CREATE1 directly within a determination or action, the BAPI’s internal COMMIT WORK conflicts with the framework’s implicit commit handling, causing a nested commit error. Here’s the solution:

Call BAPI_USER_CREATE1 via RFC FM in a separate LUW:

CALL FUNCTION 'ZCreateUser'
      STARTING NEW TASK 'UPDATE'
      DESTINATION 'NONE'
.... 

This would definitely resolve the dump you are getting.
RAP the Bapi in an RFC FM. 
 

gzghidi
Explorer
0 Likes
great answer, i'm now able to use BAPI_USER_CREATE1 but it's forcing me to change password on first login is there a way to disable that ?
M-K
Active Participant
You can check out the import-parameter "SELF_REGISTER" if it works for you in this case.
gzghidi
Explorer
0 Likes
@M-K thanks that worked i have set SELF_REGISTER to 'X'