cancel
Showing results for 
Search instead for 
Did you mean: 

how to pass a value to BAPI in sapui5 using odata services?

ashwin_narayan
Participant
0 Kudos

Hi ,

Require Help to understand how to pass a value through oData services to BAPI to fetch the required result.

Detail:

I am  trying to create a mobile app in sapui5 where Im consuming a odata service(Net Weaver gateway)...which is linked to a BAPI,

To Fetch few records from the BAPI, the user in the front end have to input a value...

This value i have to pass through the odata services to the BAPI.

Please Share Your Valuable Comments.

Thanks,

Ashwin

Accepted Solutions (1)

Accepted Solutions (1)

EkanshCapgemini
Active Contributor
0 Kudos

Hi Ashwin,

I think you need to search and go through the available material first. There are n number of documents and posts which can help you in understanding the technology as well as getting you started in this field. This is just to encourage you to nail the problem at your end first.

The way we retrieve data from OData service is:

  1. Read Operation (fetching single line of record based on key): Method: GET
  2. Query Operation (fetching multiple records based on filters): Method: GET

URI for Read: /sap/opu/.../ZMYSERVICE/MyEntitySet(Key1='Value1',Key2='Value2')

URI for Query: /sap/opu/.../ZMYSERVICE/MyEntitySet?$filter=filter1 eq 'value1' and filter2 eq 'value 2'

You can check the links provided in Venu's reply as well as other posts for UI5 specific code snippets.

Regards,
Ekansh

EkanshCapgemini
Active Contributor
0 Kudos

Hi,

Did you get your answer? If yes, please close this thread.

How to close a discussion and why

Firoz_Ashraf
Contributor
0 Kudos

Hi Eknash,

I have built my oData service to read a particular dataset based on the first options that you have suggested

URI for Read: /sap/opu/.../ZMYSERVICE/MyEntitySet(Key1='Value1',Key2='Value2')

my URI is /sap/opu/odata/SAP/ZMM_EMP_SRV/EmployeeSet('00011930')

This is giving me the correct result in Gateway Client


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

-<entry xml:base="http://XXX/sap/opu/odata/SAP/ZMM_EMP_SRV/"

  xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"

  xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"

  xmlns="http://www.w3.org/2005/Atom">

<id>http://XXX/sap/opu/odata/SAP/ZMM_EMP_SRV/EmployeeSet('00011930')</id>

<title type="text">EmployeeSet('00011930')</title>

<updated>2015-11-24T06:37:44Z</updated>

<category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" term="ZMM_EMP_SRV.Employee"/>

<link title="Employee" rel="edit" href="EmployeeSet('00011930')"/>

-<content type="application/xml">

-<m:properties>

<d:Empid>00011930</d:Empid>

<d:Empname>Alpha Barry</d:Empname>

<d:Empadd>New Jersey</d:Empadd>

<d:Empdes>ABAP ANALYST</d:Empdes>

</m:properties>

</content>

</entry>

However, when I try to fetch the same from SAPUI5 application as per the code given below it is giving me the error.

Uncaught TypeError: Cannot read property 'Empname' of undefined

Following is the controller code of my app.


onInit : function() {

  var sServiceUrl = "/sap/opu/odata/SAP/ZMM_EMP_SRV";

  var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl,true);

  var oJsonModel = new sap.ui.model.json.JSONModel();

oModel.read("/EmployeeSet('00011930')",null,null,true,function(oData,repsonse){

  oJsonModel.setData(oData);

  var results = oJsonModel.getProperty("/results/0");

  var empname = results.Empname;

  alert ("Employee Name is" +empname);

  });

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

  },

However, when I change the read code of line 6 from

oModel.read("/EmployeeSet('00011930')",null,null,true,function(oData,repsonse)

to

oModel.read("/EmployeeSet",null,null,true,function(oData,repsonse)

I get the result of the whole data set.

The issue is how to fetch only selected record in the dataset?

Any help would be highly appreciated.

Regards,

Firoz.

ashwin_narayan
Participant
0 Kudos

Hi Firoz,

I Guess, it might be because of the way your model is. Try debugging over line 06, or print your oJsonModel in console. This way you can understand how your data looks like.

This can solve your problem if you give the right path to your empname.

let me know if it helps.

Thanks,

Ashwin

Firoz_Ashraf
Contributor
0 Kudos

Hi Ashwin,

I have debugged on Chrome console and it gives me the error that I mentioned earlier.

Uncaught TypeError: Cannot read property 'Empname' of undefined


Result of my oData model in Gateway Client in JSON format is as follows:

/sap/opu/odata/SAP/ZMM_EMP_SRV/EmployeeSet?$format=json


{

  "d" : {

    "results" : [

      {

        "__metadata" : {

          "id" : "http://XXX:8000/sap/opu/odata/SAP/ZMM_EMP_SRV/EmployeeSet('00011034')",

          "uri" : "http://XXX:8000/sap/opu/odata/SAP/ZMM_EMP_SRV/EmployeeSet('00011034')",

          "type" : "ZMM_EMP_SRV.Employee"

        },

        "Empid" : "00011011",

        "Empname" : "Gamma",

        "Empadd" : "King Street",

        "Empdes" : "ERP DEVELOPER"

      },

      {

        "__metadata" : {

          "id" : "http://XXX:8000/sap/opu/odata/SAP/ZMM_EMP_SRV/EmployeeSet('00011240')",

          "uri" : "http://XXX:8000/sap/opu/odata/SAP/ZMM_EMP_SRV/EmployeeSet('00011240')",

          "type" : "ZMM_EMP_SRV.Employee"

        },

        "Empid" : "00011222",

        "Empname" : "Aldrin",

        "Empadd" : "IIS, RAKAH",

        "Empdes" : "UI5 DEVELOPER"

      },

      {

        "__metadata" : {

          "id" : "http://XXX:8000/sap/opu/odata/SAP/ZMM_EMP_SRV/EmployeeSet('00011930')",

          "uri" : "http://XXX:8000/sap/opu/odata/SAP/ZMM_EMP_SRV/EmployeeSet('00011930')",

          "type" : "ZMM_EMP_SRV.Employee"

        },

        "Empid" : "00011930",

        "Empname" : "Barry Alpha",

        "Empadd" : "New Jersey",

        "Empdes" : "ABAP ANALYST"

      }

    ]

  }

}

Could you please guide me how should I code for the right path?

Regards,

Firoz.

ashwin_narayan
Participant
0 Kudos

Hi Firoz,

What i guess is the model is not getting populated when your statement is executed as it is an asynchronous call.

  1. var sServiceUrl = "/sap/opu/odata/SAP/ZMM_EMP_SRV"
  2. var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl,true); 
  3. oModel.read("/EmployeeSet('00011930')",{
  4. async:false,
  5.   success : function(oData) {
  6.            var oJsonModel=new sap.ui.model.json.JSONModel();
  7.                                     oJsonModel.setData(oData);
  8.                       console.log(oJsonModel);
  9. },
  10. error:function(err){
  11.                    console.log(err)}
  12. })

Hope this should work for you, and try writing it in onAfterRenderingFunction() rather than init().

If it still does'nt work, you can have a fresh post and get help.

Thanks,

Ashwin

 

Firoz_Ashraf
Contributor
0 Kudos

Hi Ashwin,

Thanks it's working now.

  • Without any parameter, i,e oModel.read("/EmployeeSet",null,null,true,function(oData,repsonse)

        The statement ("/results/0") will work fine and we will get the value in variable  empname.

          var results = oJsonModel.getProperty("/results/0");

          var empname = results.Empname; 


  • With a parameter, i,e oModel.read("/EmployeeSet('00011930')",null,null,true,function(oData,repsonse)

          The statement ("/results/0") will NOT work. So, we have to change the code to

          var empname = oData.Empname;

          to get the value in empname



Many Thanks,

Firoz.

ashwin_narayan
Participant
0 Kudos

Great. congratz

Answers (2)

Answers (2)

former_member206574
Active Participant
0 Kudos
Former Member
0 Kudos

Hello Ashwin,

Please check the below link this should help

http://scn.sap.com/docs/DOC-47626

Regards

Sri

ashwin_narayan
Participant
0 Kudos

Hi Sri,

Thanks for the quick response...will go through the same.

Ashwin

ashwin_narayan
Participant
0 Kudos

Hi Sree,

What actually I'm Looking for is ..how to pass parameters from UI side to Odata to fetch results from BAPI

RameshShrestha
Contributor
0 Kudos

H Ashwin ,

You can directly attach parameter in the POST  call URL  that you make for the service.

See example in the given link. We are doing same in our project.

rest - Firefox Add-on RESTclient - How to input POST parameters? - Stack Overflow

ashwin_narayan
Participant
0 Kudos

Hi Ramesh,

Thanks fr your comments...I been trying to adapt it in Odata services....is it possible that you can show me the piece of code where you are performing the same.

RameshShrestha
Contributor
0 Kudos

Hi Ashwin,

Below is the sample code that we use for Batch operation for status change where we pass parameters in the service URL.

You have to implement the same service in the gateway system to accept the passed service.

You can consult with your SAP gateway service developer for further development.

var serviceObjUrl = "serverURL";
var action = new sap.ui.model.odata.ODataModel(serviceObjUrl, true);
action.setTokenHandlingEnabled(true);
action.setHeaders({"Content-Type" : "multipart/mixed"});
action.setUseBatch(true);

for(var count=0;count<a.length;count++){
var perNo = a[count];
action.addBatchChangeOperations([
                                        action.createBatchOperation("ChangeStatus?NewStatus='2'&NewSubstatus='B'&OldStatus='2'&OldSubstatus='A'&PersonnelNumber='"+ perNo+ "'", "POST") ]);
}
action.submitBatch(function(oData, response) {
console.log(oData);

}

ashwin_narayan
Participant
0 Kudos

Hi Ramesh,

Appreciate your efforts.

Thanks