Skip to Content
Author's profile photo Venkatachala C.K

sending data to the back-end using JSON format

Hi,

Introduction:

  In UI5 we can send multiple records using batch operation. Here i have a application which can send data to the back-end using array of objects and i also gone through other blogs in SCN which describes about batch operations but they used static objects for sending data. In this application you can able to send more number of records using dynamic object creation by converting data into JSON format.

Description:

  The application contains some input fields and table for storing data for performing operations like add, change and delete.After completion of data operations if you want send multiple records to the back-end using batch operations,first you have convert data into JSON format.

Below code snippet describes how to get table data and pushing into an array.

var items = [];// intializing an array

var aTableData = oTable.getModel().getData();// getting table data

for (i = 0; i < oTableData.length; i++) {

items.push( {

“ItemKey”: aTableData[i].Item,

“ItempartGrp”: oTableData[i].itemcodegroup,

  “ItempartCod”: oTableData[i].itemcode,

  “ItemdefectGrp”: oTableData[i].problemcodegroup,

  “ItemdefectCod”: oTableData[i].problemcode,

  “ItemdefectShtxt”: oTableData[i].problemdescription,

  “CauseGrp”: oTableData[i].causegroup,

  “CauseCod”: oTableData[i].causecode,

  “CauseShtxt”: oSel[i].causedescription,

  });

}

// oEntry is the main object to send data using batch operation.

//Pushing table data with some other values(i,e NotifType,FunctionLoc).

  var oEntry = {

  “NotifType”: xtitle,

  “Qmnum”: ” “,

  “NotifShorttxt”: shrttext,

  “FunctionLoc”: FuncLocation,

  “Equipment”: Equipmentno,

“NavNotifItems”: JSON.parse(JSON.stringify(items)),

  //  converting data into json format

// string type will converted into json object by parsing it.

};

  var sServiceUrl = “http://your url SRV/”; // ODATA URL

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

//creating batch operation

  oDModel.create(“/url”, oEntry, null, function(oData, oResponse)

  {

  //function openDialog() {

  var oDialog1 = new sap.m.Dialog();

  oDialog1.setTitle(“Success Message”);

  var ovalue = new sap.m.Text({

  text: “Notification” + oData.Qmnum + ” is Created”

  });

  var oVmain = new sap.m.FlexBox(“Dialog”, {

  items: [ovalue]

  });

  oDialog1.addContent(oVmain);

  oDialog1.addButton(new sap.m.Button({

  text: “OK”,

  press: function() {

  oDialog1.close();

  }

  }));

  oDialog1.open();

  // };

  console.log(oResponse);

                sap.ui.getCore().byId(“detailPage”).rerender();

  },

  function(err) {

     //Error Callback

   jQuery.sap.require(“sap.m.MessageBox”);

      sap.m.MessageBox.show((JSON.parse(err.response.body).error.message.value));

    }

table screen shot for performing data operations

/wp-content/uploads/2015/10/tablesample_809091.png

NOTE :  Upto my knowledge this is the best way to adding  table data to the back-end using batch operation by converting data into JSON format.

             if any doubts regarding this hit a comment and if any better methods  please let me know.

Thanks and Regards

Venkatachala C.K

Assigned Tags

      24 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Naga Sanjeeva Rao Singadi
      Naga Sanjeeva Rao Singadi

      Hi Venkat,

          It's very useful blog.

      Author's profile photo Vijay Kumar Kalluri
      Vijay Kumar Kalluri

      looks good.

      Author's profile photo Venkatachala C.K
      Venkatachala C.K
      Blog Post Author

      thank you

      Author's profile photo GOPAL KUMAR SINGH
      GOPAL KUMAR SINGH

      Hi Venkat ,

      its really nice blog very helpful.

      Author's profile photo Karthik Arjun
      Karthik Arjun

      Hi Venkatachala,

      Nice try, But I have some query.

      1. Creating Batch operation - SAPUI5-Odata Model has batch operation property to send the series of jobs into database. But here you are using for loop with oDATA call for oTableData.length number of time, is it any specific reason?

      Gateway Batch Calls from SAPUI5

      JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui.model.odata.ODataModel

      2. Correct me if I am wrong, If oDATAModel calls occurs oTableData.length number of times, It might give a performance issue and reduce the database health.

      Assume my table has 10,000 records, will this code calls 10,000 times to trigger oDATAModel class?

      Thanks,

      Karthik A

      Author's profile photo Venkatachala C.K
      Venkatachala C.K
      Blog Post Author

      Hi Karthik,

      Sorry i forgot to close for loop.(i corrected now)

      actually i'm pushing all data to an array(items) and i'm putting array in to an object(oEntry).

      Here oDATAModel call occurs only one time using batch operation and i'm taking oTableData.length to push all table items at time.

      thanks for correcting me

      Author's profile photo Sashko Janev
      Sashko Janev

      Hi Venkatachala,

      Your code is returning an error for me:Error while parsing an XML stream

      How did you set your Entity in the gateway so that one parameter can receive a table?

      The problem is when I call the service with the parameter: "NavNotifItems": JSON.parse(JSON.stringify(items)).

      Thanks

      Author's profile photo Venkatachala C.K
      Venkatachala C.K
      Blog Post Author

      Hi Sashko Janev,

      if possble can u post your code.

      I will try to correct it

      thanks

      Venkat

      Author's profile photo Sashko Janev
      Sashko Janev

      Thanks for the reply, i figured it out.

      Regards

      Author's profile photo Venkatachala C.K
      Venkatachala C.K
      Blog Post Author

      Is there any thing wrong in this code?

      Author's profile photo Sashko Janev
      Sashko Janev

      No, perfectly fine. The problem was when I tried passing integers and the code couldn't parse them to string for some reason.

      However here's my working code for future reference:

      for(i=0;i<3;i++){

        items.push({

        "Qmnum": '11',

        "Kunum": '22'

         });

      };

      var oEntry = {

         "Qmnum": '1',

         "Kunum": '2',

         "inTab": JSON.parse(JSON.stringify(items)),

        };

      oModel.create("/oDeepInsertSet", oEntry, null, function(oData, oResponse)

        {

        oJSONModel.setData(oData.inTab);

        },

        function(err) {

        // Error Callback

        jQuery.sap.require("sap.m.MessageBox");

        sap.m.MessageBox.show((JSON.parse(err.response.body).error.message.value));

        });

        this.setModel(oJSONModel, 'theModel');

      Author's profile photo Venkatachala C.K
      Venkatachala C.K
      Blog Post Author

      ok thank you.

      Author's profile photo Former Member
      Former Member

      Nice blog:)

      But i have another doubt..

      I have created a matrix layout to get user inputs and trying to manually show that inputs getting added to table (just to display) on screen by adding those inputs,,and then i want to post  the table data to back end array using batch operation..

      Can u help me on how to add user inputs to my table on screen???

      Author's profile photo Michael Appleby
      Michael Appleby

      Please create a new Discussion marked as a Question.  The Comments section of a Blog (or Document) is not the right vehicle for asking questions as the results are not easily searchable.  Once your issue is solved, a Discussion with the solution (and marked with Correct Answer) makes the results visible to others experiencing a similar problem.  If a blog or document is related, put in a link.

      NOTE: Getting the link is easy enough for both the author and Blog.  Simply MouseOver the item, Right Click, and select Copy Shortcut.  Paste it into your Discussion.  You can also click on the url after pasting.  Click on the A to expand the options and select T (on the right) to Auto-Title the url.

      Thanks, Mike (Moderator)

      SAP Technology RIG

      Author's profile photo Christopher Solomon
      Christopher Solomon

      I need to copy that whole reply to use SEVERAL places elsewhere. 😛

      Author's profile photo Michael Appleby
      Michael Appleby

      Hi Christopher,

      Feel free.  I keep the text file (just sent to you in an email) open all day with mostly canned responses for just this kind of situation.  Run Simple! after all.

      Cheers, Mike

      SAP Technology RIG

      Author's profile photo Chandan Jha
      Chandan Jha

      Hi venkatachala,

      Helpful post but for me this code ran into some issues.

      Where you're pushing table data with some other values(i,e NotifType,FunctionLoc) there are variables used like xTitle for NotifType, FuncLoc for Function location and so on. Are these mandatory fields to be declared? also if it is then what do we put in these fields?

      For e.g. what do I put in the variable xTitle that is assigning value to NotifType?

      Because I am not having the right data for this variable I am getting the following error-

      Property 'NotifType' is invalid
      Your help would be highly appreciated.
      Thanks,
      CJ

       

      Author's profile photo Chandan Jha
      Chandan Jha
      Looks like these fields are not available in my gateway service. Can you please tell me how is it related from on the SAP Gateway side? What do we look for and implement in SEGW? if there is anything related to this.
      Thanks,
      CJ

       

       

      Author's profile photo Venkatachala C.K
      Venkatachala C.K
      Blog Post Author

      Hello Chandan,

      This is just a sample data in JSON format for posting, and the same payload is not mandatory for all the scenarios. If i know your scenario may be i can help you with that.

      Thanks

      Venkat

       

       

       

      Author's profile photo David Raven
      David Raven

       

      Why do I need the oEntry?

      Another question please: what do I have to implement on sap gateway side?

       

      Thank you very much.

       

      Regards.

      Author's profile photo Venkatachala C.K
      Venkatachala C.K
      Blog Post Author

      Hello David Raven,

       

      oEntry is the payload structure to post data into backend using oData create operation.

      Thanks

      Venkat

       

       

       

      Author's profile photo Chandan Jha
      Chandan Jha

      Hi Venkat,

      Thanks for your response.

      In my case now that I have made my own oEntry payload with just one field for e.g. "Jobs"

      When I execute the code I get the error that the property "Jobs" is invalid.

      something like this-

      "message":{"lang":"en","value":"Property 'Jobs' is invalid"}

      I feel there should be something maintained at the backend side? isn't it? at the gateway maybe?

      What is NavNotifItems in your backend code (in SAP)?

       

      Regards,

      Chandan

      Author's profile photo David Raven
      David Raven

      Hi again,

       

      And where did you create NavNotifItems ?

       

      Thank you.

      Author's profile photo Thangaraj Swaminathan
      Thangaraj Swaminathan

      Hi David, I hope you got the answer to Property ‘NotifType’ is invalid. 

      I am also getting the same error message. either Venkadachalam or any helper can help me as I am new in OData? My requirement is not a deep insert to push master and details data. I just wanted to push simple only one table rows. thx. - Swami