Skip to Content
avatar image
-1
Former Member

How to post table data from front-end (SAPUI5) to back-end (OData/ERP)?

Hello experts,

I really need your help beacuse this SAPUI5-App is part of a project with completion date 30.06.2017!

The business scenario is to create a material document with more than one item line, see following example:

Therefor I need to post multiple array lines (JSONModel) at once to the backend. With clicking the button "Post Goods Receipt" the material document should be created.

My code for posting in javascript looks as follows:

var oModel = new sap.ui.model.odata.ODataModel("/sap/opu/odata/sap/Z_MGM_PG_SRV/");

oModel.create("/PGR_DD_Set", oTableModel.getProperty("/"), null, function(oData, oResponse){
  alert ("OK");
},
 function(err) {
//Error Callback
 alert("Error");
});

//oTableModel has the following structure
{"items":[

{"Matnr":"55034222","Bismt":"10191/00701/M01N","Maktx":"Dongle Blanco","Meins":"ST","Budat":"2017-06-13T00:00:00","Werks":"1270","Lgort":"0001"},

{"Matnr":"55034220","Bismt":"30171/031701/M002","Maktx":"PCB Contadores Mecanicos","Meins":"ST"}
]}

I tried nearly everything starting with $batch operation and ending with deep inserts regarding the redefiniton of the CREATE_DEEP_ENTITY method like in this step-by-step guide:

Step by Step development for CREATE_DEEP_ENTITY operation

I read something about how the odata service distinguishes between CREATE and CREATE_DEEP_ENTITY method. So if there is "inline data" the Service will trigger the CREATE_DEEP_ENTITY method but this doesn't work for me. My payload looks as follows:

So although i got this inline data the Service chooses the CREATE_ENTITY method. During debugging I can see the CREATE_DEEP_ENTITY method is not triggered because of the empty expand string. Does this relate to my problem?

I have no idea how to fix this issue. Please help me.

If you need any other information don't be afraid to comment below.

Many thanks in advance!

Steffen

payload.png (76.0 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • Best Answer
    Jun 14, 2017 at 04:02 AM

    Hi ,

    Your entity 'PGR_DD_Set' , is it a kind of complex entity ? Deep insert will work perfectly if you have parent and child relationship between your entities. From screenshot i can see that , Material is Parent entity and items are child.

    When you are calling Create method , you should pass Material object as a payload , and through Navigation Property , you should add items to your Material that object , there by it will post Material Header object and Item array object together to backend as follows -

    var materialObject = {};

    materialObject.property1 = value1;

    materialObject.navigationProperty = [array of items entity objects]

    oModel.create("/MaterialEntityObject", materialObject , null, function(oData, oResponse){
      alert ("OK");},function(err){//Error Callback
     alert("Error");});

    Hope it helps.

    Thanks ,

    Mahesh Z.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Thank you so much for your quick response but I guess I didn't get the parent and child part.

      How do I have to implement my OData service?

      At the moment I have the entity PGR_DD_Set which has no navigation property. For this entity I want to use the create method so that my wrapper fm for BAPI_GOODSMVT_CREATE will receive all the items form front-end. My mapping looks as follows:

      IT_PG_MD is the table parameter of my fm and is type of a flat structure.

      I guess my problem is the missing navigation property respectively the missing parent child relation. Do I have to implement another entity set where I only have material number as an import parameter? Do I have to adjust my JSONModel in front-end? Could you please give me an example about this. Like you see I am struggeling with the create method which I never used before.

      Many thanks in advance!

      Steffen

      mapping.jpg (130.1 kB)
  • Jun 14, 2017 at 04:24 AM

    You can either use "$batch with changesets" or "Deep Create". Calm down, understand the prerequisites of each of these and proceed. :)

    If you want to use Deep Create, ensure that you have a Header Entity, Dependent Entity and create a navigation between them. Mahesh's answer helps you to build the request body for it.

    In either case, you are failing to create the right request body (or your OData model is not right).

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 14, 2017 at 03:15 PM

    I think in your oData and JSON structure should be adjusted to have header and line items. In case oData you will need two Entityset one for header and other for line items and have association between the entitysets. This will allow create deep entity. Also in your JSON structure will also need corresponding {Headers: {"A", "B"}, "items":[{"Matnr":"55034222","Bismt":"10191/00701/M01N","Maktx":"Dongle Blanco","Meins":"ST","Budat":"2017-06-13T00:00:00","Werks":"1270","Lgort":"0001"},{"Matnr":"55034220","Bismt":"30171/031701/M002","Maktx":"PCB Contadores Mecanicos","Meins":"ST"}]}

    Refer how to use create_deep_entity Blog.

    https://blogs.sap.com/2014/04/27/step-by-step-development-guide-for-createdeepentity-operation/

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 20, 2017 at 01:30 PM

    Hi all,

    Thank you so much for your help. I solved the problem with the CREATE_DEEP_ENTITY method. I implemented the entity PGR_DD_Header with my BUDAT (which is a header information for BAPI_GOODSMVT_CREATE as well) and got the data via the navigation property "items". So in the end i have a deep structure with the BUDAT and my line items as a table. Now I have to focus on how i get the return date from the BAPI to the front-end. If you have solutions for this, too don't be afraid to post it :-)

    Many thanks in advance.

    Steffen

    Add comment
    10|10000 characters needed characters exceeded

  • Jun 13, 2017 at 10:16 AM

    Hi ,

    Use Batch request of oData .. check out the following thread for the help.

    https://archive.sap.com/discussions/thread/3833815

    thanks

    VIplove

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 16, 2017 at 07:41 AM

    Thank you so much for your quick response but I guess I didn't get the parent and child part.

    How do I have to implement my OData service?

    At the moment I have the entity PGR_DD_Set which has no navigation property. For this entity I want to use the create method so that my wrapper fm for BAPI_GOODSMVT_CREATE will receive all the items form front-end. My mapping looks as follows:

    IT_PG_MD is the table parameter of my fm and is type of a flat structure.

    I guess my problem is the missing navigation property respectively the missing parent child relation. Do I have to implement another entity set where I only have material number as an import parameter? Do I have to adjust my JSONModel in front-end? Could you please give me an example about this. Like you see I am struggeling with the create method which I never used before.

    Many thanks in advance!

    Steffen

    Add comment
    10|10000 characters needed characters exceeded