Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create deep entity with oModel.createEntry

Tags:

odata

sapui5

Is there a way to create a "temporary" (or I guess it's called virtual) deep entity with using oModel.createEntity()?

I have a entity called Timesheet with an association to breaks, called ToBreaks.

Now I want to create a new entity in the frontend by using oModel.createEntity("/TimesheetSet").

Unfortunately in the so called virtual new entry all my associations are missing. Because I use the assications for binding tables, after creating the virtual new entry, a backend call is triggered TimesheetSet("id-04-123456789")/ToBreaks which leads to a "invalid key predicate" error.

Is there a way to do this with OData V2?

Update 09.08.2016:

You can still try by just using the properties parameter with a nested entity set. As long as your OData service supports the corresponding deep create it should work:

I've also tried something like this:

oModel.createEntry("/TimesheetEntry", {
    Pernr: sPernr,
    Begda: dBegda,
    ToBreaks: [{
        Pernr: sPernr,
        Begda: dBegda
    }]
});

ToBreaks is the name of the association.

In the virtual entry of the OData-Model the properties for the associations are still missing. I can create the new entry by using the code above, but afterwards there is no property called ToBreaks

On the Backend-Side I followed this tutorial for implementing the deep_create method: step-by-step-development-guide-for-createdeepentity-operation. I'm able to trigger the method from within the SAP Gateway Client.

like image 374
srz Avatar asked Mar 11 '23 12:03

srz


2 Answers

From the API docs:

Please note that deep creates (including data defined by navigationproperties) are not supported.

You can still try by just using the properties parameter with a nested entity set. As long as your OData service supports the corresponding deep create it should work:

properties could be an object which includes the desired properties and the values which should be used for the created entry.

like image 163
cschuff Avatar answered Mar 25 '23 10:03

cschuff


Yes you can execute deep entity creation with oData V2. In order to do so you need to build your request body according to your metadata structure. Let's assume that your metadata model is User and each user have multiple Communication. So we have a user entity and communication entity. We also have association of fromUserToCommunications and navigation property let's call it Communications

In order to execute a call to deep create a User entity you will need to do the following:

// Create the oData Model
var oModel = new sap.ui.model.odata.ODataModel("{YOUR_SERVICE_DOCUMENT_URL}");


// Create the user request body, please make sure that 
// all required fields are filled in and are according to the 
// user entity metadata
var userRequestBody = {
    id: "123",
    firstName: "Your First Name",
    lastName: "Your Last Name",
    address: "Your Address",
    communications: []  // contain multiple communications 
};

var comm1 = {
    id : "1",
    userId : "123", // foregin key to the user entity
    homePhone: "+134342445435" 
};

var comm2 = {
    id : "2",
    userId : "123", // foregin key to the user entity
    homePhone: "+134342445436" 
};    

// add the communications to the user entity 
userRequestBody.communications.push(comm1);
userRequestBody.communications.push(comm2);

oModel.create("/UserCollection",userRequestBody,{
    success: function(result){
        // everything is OK 
    },
    error: function(err){
        // some error occuerd 
    },
    async: true,  // execute async request to not stuck the main thread
    urlParameters: {}  // send URL parameters if required 
}); 
like image 21
Ran Hassid Avatar answered Mar 25 '23 08:03

Ran Hassid