cancel
Showing results for 
Search instead for 
Did you mean: 

odata create entity action in SAP MDK

0 Kudos

Hi, Posted below is my metadata for 2 entities:

<EntityType Name="CompanyProfile">
                <Key>
                    <PropertyRef Name="Id"></PropertyRef>
                </Key>
                <Property Name="Address" Type="Edm.Int64" Nullable="true"></Property>
                <Property Name="ContactNumber" Type="Edm.String" Nullable="true" MaxLength="255"></Property>
                <Property Name="Id" Type="Edm.Int64" Nullable="false"></Property>
                <Property Name="Version" Type="Edm.Int64"></Property>
                <Property Name="Name" Type="Edm.String"></Property>
                <NavigationProperty Name="LocationDetails" Relationship="default.CompanyProfile_Location_Many_ZeroToOne0" FromRole="CompanyProfile" ToRole="Location"></NavigationProperty>
</EntityType>
<EntityType Name="Location">
                <Key>
                    <PropertyRef Name="Id"></PropertyRef>
                </Key>
                <Property Name="City" Type="Edm.String" Nullable="true"></Property>
                <Property Name="Id" Type="Edm.Int64" Nullable="false"></Property>
                <Property Name="Province" Type="Edm.String" Nullable="true"></Property>
                <Property Name="Region" Type="Edm.Region" Nullable="true"></Property>
                <Property Name="Version" Type="Edm.Int64" Nullable="true"></Property>
</EntityType> 

It has been simplified a bit from the original, but the problem remains the same anyway. As you can see, the CompanyProfile entity has a relation to the Location entity. Also the Address property in CompanyProfile is in fact also the id for the related Location entity. Given this, I try to create a new CompanyProfile related with an existing Location using:

{
    "Version": "1",
    "ContactNumber": "1245",
    "Name": "OIL Tycoon corporation",
    "Status": "OK",
    "Address":  "2"
}

However, that doesn't work. What works is

{    
    "Version": "1",
    "ContactNumber": "1245",
    "Name": "OIL Tycoon corporation",
    "Status": "OK",
    "LocationDetails" : {
    "__metadata": {
                "id": "http://localhost:8080/odata/Locations(2L)",
                "uri": "http://localhost:8080/odata/Locations(2L)",
                "type": "default.Location"
            }
  }
} 

Now the reason I ask for this is because the request being generated by SAP MDK odata action has the data structure of the first, so I get an error with it. The second structure is what I need, but then I have no idea if my odata api is the one thats wrong and the first structure is still correct and should be accepted by my apis(It was created using spring-boot and olingo). Here is the code of the odata action in case it is useful

"CreateLinks": [
{
"Property": "LocationDetails",
"Target": {
"EntitySet": "Locations",
"QueryOptions": "$filter=Id eq 2"
},
"_Enabled": true
}
],
"Properties": {
               "ContactNumber": "1245",
               "Name": "OIL Tycoon corporation",
               "Status": "OK"
       "Version": "1"
},
"Target": {
"EntitySet": "CompanyProfiles",
"Service": "/DOEDocumentMonitoring/Services/DOEDocumentMonitoring.service"
},
"_Type": "Action.Type.ODataService.CreateEntity"
}

Sorry the question is a bit long. I hope I was clear with my explanation of the problem.

0 Kudos

So it turns out this seems to be a quirk of the server. What also works is

{
    "Version": "1",
    "ContactNumber": "1245",
    "Name": "OIL Tycoon corporation",
    "Status": "OK",
    "LocationDetails":{
       "Id" : "2"

    }
}

This is called deep entity I think? Is there anyway to do this type of create in SAP MDK?

Jitendra_Kansal
Product and Topic Expert
Product and Topic Expert
0 Kudos

bryanasuncion

Were you able to resolve your issue? If any of the below responses helped you, then please mark the helpful answer by accepting it OR post an answer so others might benefit from your findings.

Regards, Jitendra (SAP Community Moderator)

Accepted Solutions (0)

Answers (1)

Answers (1)

rishabhanand25
Explorer
0 Kudos

Kindly specify Id also in the Create payload, since it is a mandatory field(nullable = false) :

{"Version": "1","ContactNumber": "1245","Id" : "2", "Name": "OIL Tycoon corporation","Status": "OK","Address":  "2"}
0 Kudos

Hi Rishabh,

The Id in this case is created automatically. I'd like to clarify that basically this is bare minimum for this api, except that Address is not being recognized in the first example that I gave. The api recognizes the second example (Address is also LocationDetails, only in link form or something. I'm sorry I don't really know the specifics of what its called)