cancel
Showing results for 
Search instead for 
Did you mean: 

Gateway for simple function module

Former Member
0 Kudos

Hi there,

my background is ABAP and I've now started development with SAPUI5.

My scenario:

I want to write a simple SAPUI5 application allowing to change a user password.

Therefore I've created a RFC-enabled function module 'ZBC_USER_SET_NEW_PASSWORD' in the backend system:


FUNCTION zbc_user_set_new_password.

*"----------------------------------------------------------------------

*"*"Local Interface:

*"  IMPORTING

*"     VALUE(I_UNAME) TYPE  SYUNAME

*"     VALUE(I_PASSWORD) TYPE  STRINGVAL

*"----------------------------------------------------------------------

   DATA:

     ls_return             TYPE bapiret2,

     lt_return             TYPE STANDARD TABLE OF bapiret2.

   CALL FUNCTION 'BAPI_USER_CHANGE'

     EXPORTING

       username  = i_uname

       password  = i_password

       passwordx = abap_true

     TABLES

       return    = lt_return.

   READ TABLE lt_return INTO ls_return

     WITH KEY

       type = 'E'.

   IF sy-subrc IS INITIAL.

*     Error occured

     EXIT.

   ENDIF.

ENDFUNCTION.

Now I would like to call that FM from a SAPUI5 application running on the same ABAP server.

I've tried to create a gateway service that only calls this function module, but I'm struggling with the GetEntitySet and GetEntity methods.

What is the easiest way to create a gateway service to call the above listed function module?

Please note that I don't want to retrieve any data. I just would like to send a new password to the backend system.

If gateway is not appropriate for this, would it be better to expose the FM as web service and directly call it via SOAP request?

Thanks in advance.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

I've now decided to call the function module directly: It has been released as a web service on the ABAP server.

The method changePassword located in the controller js calls the web service:


changePassword : function(user, password) {

var soapRequest =

  '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ' +

  'xmlns:urn="urn:sap-com:document:sap:rfc:functions">' +

  '<soapenv:Header></soapenv:Header><soapenv:Body>' +

      '<urn:ZBC_USER_SET_NEW_PASSWORD>' +

      '<I_PASSWORD>' + password + '</I_PASSWORD>' +

      '<I_UNAME>' + user + '</I_UNAME>' +

      '</urn:ZBC_USER_SET_NEW_PASSWORD>' +

      '</soapenv:Body></soapenv:Envelope>';

var endpoint = '/sap/bc/srt/rfc/sap/zbc_user_set_new_password/' +

                     '010/zbc_user_set_new_password/zbc_user_set_new_password';

var sServiceUrl = getUrl(endpoint);

var xmlhttp = new XMLHttpRequest();

xmlhttp.open('POST', sServiceUrl, false, '<USER>', '<PASSWORD>');

xmlhttp.setRequestHeader('Content-Type', 'text/xml');

var result = xmlhttp.send(soapRequest);

console.log(result);

}

This is the code of the getUrl method that I've put in my utils/utils.js file:


function getUrl(sParam) {

  var sPageURL = window.location.href;

  return window.location.protocol + "//" + window.location.host + sParam;

}

Maybe it helps you...

Andre_Fischer
Product and Topic Expert
Product and Topic Expert
0 Kudos

In the user self service that we deliver we use a function import as well.

http://scn.sap.com/community/gateway/blog/2013/09/05/user-self-service-in-gateway

Best Regards,

Andre

Answers (2)

Answers (2)

Jitendra_Kansal
Product and Topic Expert
Product and Topic Expert
0 Kudos

Please close this thread if it's been resolved.

Rgrds,

JK

ChandraMahajan
Active Contributor
0 Kudos

Hi,

you better follow the code based approach. for your scenario, you need to redefine update_entity method. You can refer my blog and check the details of Update operation.

Regards,

Chandra

Former Member
0 Kudos

Hi Chandra,

thanks for your answer.

I've already mapped the update method with my function module:

But I don't know exactly how to call the OData service in a way that it performs this update method.

What do I have to specify in the URL?

Somethink like ( IUname eq 'USER1' and IPassword eq 'init123' and IUnlockUser eq ' ')?

Is it a POST or PUT request?

Thanks in advance.

ChandraMahajan
Active Contributor
0 Kudos

Hi,

it will be PUT request and you need to pass values as (IUname='USER1',IPassword='init123',IUnlockUser=' ')

also please put external breakpoint in update_entity method of your DPC_EXT class to ensure that call is reaching there and executing your code.

I guess you are using GW client transaction to test your update operation as explained in my blog.

Regards,

Chandra

Former Member
0 Kudos

Thanks, Chandra!

When I try to perform a PUT request in the GW client, I get the error message "The specified HTTP method is not allowed for the resource identified by the Data Service Request URI":

Do I also have to create an implementation for the GetEntitySet (Query) and GetEntity (Read) methods?

When reading your blog, it seemd that you have first to perform a GET request before and update can be done via PUT.

Is that correct?

ChandraMahajan
Active Contributor
0 Kudos

Hi,

1) your HTTP request payload is not correct. for this reason, you should have atleast implemented get_entity and then after executing it, you can copy the response to request  using Use as Request button. you can refer my reply in thread where I mentioned how to pass request payload if you do not implement get_entity.

2) (IUname='USER1',IPassword='init123',IUnlockUser=' ') needs to be passed in request uri

Regards,

Chandra

kammaje_cis
Active Contributor
0 Kudos

Kolz,

You might want to check how to use the GW client for PUT requests here.

regards

Krishna

kammaje_cis
Active Contributor
0 Kudos

Also see this blog if you want to consider options out of OData.

Former Member
0 Kudos

Hi Chandra,

I've managed now to perform the PUT request with the follwing XML payload:


<?xml version="1.0" encoding="utf-8"?>

<entry xml:base="http://xxx:8000/sap/opu/odata/sap/ZBC_USER_SRV/" xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">

  <id>http://xxx:8000/sap/opu/odata/sap/ZBC_USER_SRV/UserListSet('TESTUSER')</id>

  <content type="application/xml">

   <m:properties>

    <d:IUname>TESTUSER</d:IUname>

    <d:IPassword>init1</d:IPassword>

    <d:IUnlockUser>X</d:IUnlockUser>

   </m:properties>

  </content>

</entry>



The request query URI looks like that:


/sap/opu/odata/sap/ZBC_USER_SET_PASSWORD_SRV/USER_PASSWORDSet(IUname='TESTUSER')

The HTTP method is PUT.

Now I just need to find out how to do the same in SAPUI5!

Thanks a lot.

ChandraMahajan
Active Contributor
0 Kudos

That's really great!

you need to put below code in SAPUI5 application to execute Update operation.


var sURI = getUrl( '/sap/opu/odata/sap/ZBC_USER_SET_PASSWORD_SRV/');

oModel = new sap.ui.model.odata.ODataModel(sURI, false),

sap.ui.getCore().setModel(oModel);

var oEntry = {};

  

oEntry.IUname= ‘TESTUSER’;

oEntry.IPassword= ‘init1’;

oEntry.IUnlockUser = 'X’;

           

var oParams = {};

oParams.fnSuccess = function(){ alert("Update successful");};

oParams.fnError = function(){alert("Update failed");};

oParams.bMerge = true;

oModel.update("/USER_PASSWORDSet(IUname='TESTUSER')", oEntry, oParams);

console.log(oParams);

alert(JSON.stringify(oParams));

alert(JSON.stringify(oEntry));

I hope this solves your question.

Regards,

Chandra

ChandraMahajan
Active Contributor
0 Kudos

Hi,

could you please update us if your question got answered or are you looking for any more information.

Please mark this Discussion with a Correct Answer and Helpful Answer where appropriate.  See    Even if you discovered the solution without any outside contributions, it helps others to understand what the solution turned out to be.


Regards,

Chandra

Former Member
0 Kudos

Hi Krishna,

after further investigation I'm also of the opinion that it's much simpler to call a web service created from a function module than to rebuild the same logic in the gateway...

Best regards,

Thorsten.

Andre_Fischer
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Thorsten,

for such a request you would use a function import.

A function import is useful if you want to cover a funcitionality that is not covered by simple CRUD methods on an entity.

Best Regards,

Andre