cancel
Showing results for 
Search instead for 
Did you mean: 

how to update a table from front end by ui5 in SAP gateway ?

Former Member
0 Kudos

hi all,

    when I want to update the table in gateway data provider class----method entityname_update_entity ,i only can get  only one row modified data,is there any other method i can get the modified table data, so i can modify the table data...

Accepted Solutions (0)

Answers (1)

Answers (1)

AbhishekSharma
Active Contributor

Hi Dangli,

There are couple of ways you can update data in DB table using SAPUI5 application.

  1. Using OneWay binding (Create, Update)
  2. Using TwoWay binding (submitChanges)

I am assuming below things in order to work suggested solution:

  1. You have created Gateway Service and have implemented its methods (atlease Update_Entity method)
  2. You have created frontend form from where you are going to pickup values to save.

Assuming you have OneWay binding which is always default binding.

First create one Object which will contain all of your data which you want to save using Gateway Services  oEntry is that Object which contains two fields prodName, prodDesc (same as your table values)

var oEntry = {};

// Reading values from frontend form as below

var prodName = sap.ui.getCore().byId("txtProdName1").getValue();

var prodDesc = sap.ui.getCore().byId("txtProdDesc1").getValue();

oEntry.ProdId = 10; //This is your table Primary key this you need to handel as per requirement

// Adding values from frontend to Object we created to send

oEntry.ProdName = prodName;

oEntry.ProdDesc = prodDesc;

var oModel = new sap.ui.model.odata.ODataModel("/sap/opu/odata/sap/<your service name>/"); 

oModel.setProperty("/ProdName", oEntry.ProdName);

oModel.setProperty("/ProdName", oEntry.ProdDesc);

// If you want to create new entry in DB table then use oModel.create method.

// If you want to updatenew entry in DB table then use oModel.update method.

oModel.update("/<your entity set name>", oEntry, null, function() {

  sap.ui.commons.MessageBox.alert("Record Updated Successfully!", '', "Success");

}, function() {

  sap.ui.commons.MessageBox.alert("Record Update Error!", '', "Error");

});

oModel.refresh();

Now you need to read these values in your DPC_EXT class where you need to write Modify Statement to update table entries.

In XXXXX_UPDATE_ENTITY() method write below code :

DATA: lwa_key_tab  TYPE /iwbep/s_mgw_name_value_pair.

READ TABLE it_key_tab INTO lwa_key_tab WITH KEY name = 'ProdId'.

IF sy-subrc = 0.

     lv_ProdId = lwa_key_tab-value.

ENDIF.

// Now read entry which you have sent from frontend

io_data_provider->read_entry_data( IMPORTING es_data = ls_prodDetails ).

Modify Z_prodDetails from ls_prodDetails where prodid = lv_ProdId.

This is done ...

I tried most of things which come in my mind so that you know the concept but still I will suggest you to go through below link:

Instantiating an OData Model

Thanks-

Abhishek

Former Member
0 Kudos

hi Abhishek,

   Thanks for your reply.may be my question is not clear,

    Firstly, we used TwoWay binding (submitChanges), and the problem is in DPC_EXT class,when i update data,

  

   io_data_provider->read_entry_data( IMPORTING es_data = ls_prodDetails ).

  this code only can get one row data, we need to get mulit-rows data, so I can update mulit-rows data,

is there any method can implementation requirement, any idea?

Thanks

Dangli

AbhishekSharma
Active Contributor
0 Kudos

Hi Dangli,

No problem there is one method available to save multiple rows in DB in one shot. Please see below how :

For updating multiple entries into db table using SAPUI5 and Gateway you got method in Model called submitBatch.

Every row that you want to update you need to first retrieve that row and need to change values init and then need to put it back in Entity object.

This you need to do for all the rows which you will be getting based on Checkbox selection (assuming you are already getting functionality working).

So how to bundle this information and save it in onego:

For example you got columns from your Entry as FNM,LNM,ADDR.

your entity will look like as below:

info1={FNM:'aa',LNM:'bb','ADDR:'cc''}

info2={FNM:'aa1',LNM:'bb2','ADDR:'cc3''}


you got these two entity with updated values now we need to send this data to Gateway for save.

Now to send this full data we need to add this in an Array.


FULLDATA[];


FULLDATA.push(oModel.createBatchOperation("USERINFO", "<HTTP METHOD>", info1) );

FULLDATA.push(oModel.createBatchOperation("USERINFO", "<HTTP METHOD>", info2) );

<HTTP METHOD> we need to use eighter PUT or POST method.

now your data is available in FULLDATA array we just need to send it to Gateway Service.

now we will submit these changes:

oModel.addBatchChangeOperations(FULLDATA);

oModel.submitBatch(function(){alert('success');},function(){alert('failed');});

now in DPC_EXT method inside UPDATE_ENTITYSET you need to read this data and just call ABAP Modify statement.

Thanks-

Abhishek

Former Member
0 Kudos

Hi Abhishek,

   what you said is completely right in logic,but there is no method called UPDATE_ENTITYSET   in DPC_EXT ???,only UPDATE_ENTITY.

Thanks

Dangli

AbhishekSharma
Active Contributor
0 Kudos

HI Dangli,

that at means you have not created entity set for this entity. When you create an entity in SEGW you see a checkbox which says create entity set for this entity, so it automatically creates one set also.

now you need to manually create entity set for that.

no worry it's easy just right click and create.

ONce you generate entityset it will create that method also then you just need to redefine it.

after that you need to again generate service and need to clear cache of your service option is available in menu I can not recall exact name of that.

just do this it will surely work.

Thanks-

Abhishek