Skip to Content

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

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    Jan 12, 2015 at 05:54 AM

    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#callFunction

    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

    Add comment
    10|10000 characters needed characters exceeded

    • 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