cancel
Showing results for 
Search instead for 
Did you mean: 

How to call Function Import from SAP UI5 and get the return object in table?

laxmikant_soni
Explorer

HI,

      I am New to UI5 .i have created odata gateway service with function Import that will take single parameter as importing parameter and return the data in table.My problem is I am not getting how to consume data from function import in SAP UI5  and how i can display the returned data in ui5 table.

can Any Help me out regarding the issue.

Regards

LK

Accepted Solutions (1)

Accepted Solutions (1)

pfefferf
Active Contributor

Hello LK,

if you are using the service with an ODataModel you can use function "callFunction" of the ODataModel to call a Function Import.

The function has several parameters which allow you to specify the required information. For a detailed description of the parameters please have a look to the API reference.

https://sapui5.netweaver.ondemand.com/sdk/#docs/api/symbols/sap.ui.model.odata.ODataModel.html#callF...

Here is a simple example:


oDataModel.callFunction("TestFunctionImport", // function import name

                                        "POST", // http method

                                        {"parameter1" : "value1"  }, // function import parameters

                                        null,       

                                        function(oData, response) { }, // callback function for success

                                        function(oError){} ); // callback function for error

If the function import call was successful the "success callback function" is called. Within that you can access the returned data.

Regards,

Florian

sonali_jha
Explorer
0 Kudos

Thanks Florian,

and how to put the returned data to sap ui 5 table.

former_member191806
Active Participant
0 Kudos

Hello Sonali,

     As Florian stated, when using the oDataModel.callFunction you must specify a callback function which will be invoked when the Function Import succeeds. The first parameter of this callback function is oData = a js object which contains the result. For this particular situation (in which the Function Import return value cardinality is 0..n or 1...n , i.e. a table) the oData object will have the following structure:


{

     "d":

     {

          "__metadata":

          {

               "type":"Collection(<<complex type name>>)"

          },

          "results":

          [

               /* an array of data records */

          ]

     }

}

     So you will be interested only in oData.d.results array (which will contain the 'real' data retrieved from your backend system). My suggestion would be to create a sap.ui.model.json.JSONModel from this array and then use it for data binding within a list / table. You can do this directly inside the callback function's body. Example code:


var model = new sap.ui.model.json.JSONModel(oData.d.result);

var list = new sap.m.List();

  list.setModel(model);

  list.bindItems({

       path : "/",

       template : new sap.m.DisplayListItem({ label: "{name}" })

  });

     Here you can also see a working example based on the code above: http://jsbin.com/lofamamina/1/edit?html,output

Regards,

Serban

fenik84
Explorer

Hi!

Thanks for the answer 🙂 but the example code is not completely correct. This is the correct sintax:

oDataModel.callFunction("/TestFunctionImport", {    // function import name
method: "POST", // http method
urlParameters: {"parameter1" : "value1" }, // function import parameters
sucess: function(oData, response) { }, // callback function for success
error: function(oError){ } // callback function for error });
0 Kudos

Hi fenik84,

In the above function call the 'success' callback spelling is wrong. I spent some time wondering why it isn't coming to a success callback even though the network shows 200. For the people who will be referring to this hereafter, please alter it. Thank you 🙂

Answers (0)