Search code examples
pythonsaprfcbapisap-basispyrfc

How to update email with BAPI_USER_CHANGE in pyrfc?


I was able to write the code to get the details from SAP thru BAPI_USER_GET_DETAIL, here is attached code to get the email from SAP backend:

import pyrfc
from pyrfc import Connection
setup= pyrfc.Connection(user=X , passwd=Y , mshost=Z , sysid=A , client=B , msserv= C , group=D )
result=setup.call(BAPI_USER_GET_DETAIL, USERNAME=abc)
print (result['ADDRESS']['E_MAIL'])

Expected Result: [email protected]

I am in need to update email address for particular user in SAP, after researching found that by using BAPI_USER_CHANGE we can update new mail address but tried many times with no luck!

Can anyone please help in getting the correct syntax to run BAPI_USER_CHANGE in Python?


Solution

  • First of all, your get BAPI is also a bit of incorrect, maybe on old versions of PyRFC it worked but now pyrfc module has Connection object, not connection and your code throws compilation error. It should be as follows:

    import pyrfc
    from pyrfc import Connection
    RIS=pyrfc.Connection(user='USER', passwd='pw', ashost='hostey.com', sysid='KEK', sysnr='00', client='200', lang='EN', trace='3')
    result=RIS.call("BAPI_USER_GET_DETAIL", USERNAME='MUELLER')
    print(result['ADDRESS']['FULLNAME'])
    

    Secondly, change BAPI is called the same as get BAPI, for me this code worked

    ADDR = { "E_MAIL": '[email protected]'}
    ADDX = { "E_MAIL": 'X'}
    changed=RIS.call("BAPI_USER_CHANGE", USERNAME='MUELLER', ADDRESS=ADDR, ADDRESSX=ADDX)
    print(changed["RETURN"])
    

    it should show you smth like this output if executed correctly

    [{'TYPE': 'S', 'ID': '01', 'NUMBER': '039', 'MESSAGE': 'User MUELLER has changed', 'LOG_NO': '', 'LOG_MSG_NO': '000000', 'MESSAGE_V1': 'MUELLER', 'MESSAGE_V2': '', 'MESSAGE_V3': '', 'MESSAGE_V4': '', 'PARAMETER': '', 'ROW': 0, 'FIELD': 'BNAME', 'SYSTEM': 'T90CLNT090'}]

    Weirdly new email does not showed neither with BAPI call nor in SE37, but perfectly showed in SU01.

    enter image description here

    I thinks this is because of the long char field in BAPI structure which prevents it from showing correctly. Maybe this was the reason why you considered your call unsuccessful?