cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to do create with Odata V4 with Guid on Item with association

duncanbarre
Participant
0 Likes
933

I have 2 CDS views a header and an item and it's Odata v4. The item has an association with the header. Because of the association I am not allowed to directly perform a Create on the item in the front-end. Because of the association I am not allowed to turn on Create in the behaviour definition in the item. It seems like I have to do the create via the header (not sure?) and then to the item. In addition I get an error message on MissionNeedsID. This is a GUID. My question is:

how do I do a create on an item that has an association?

how do I pass a GUID to the back-end in the right way? 

Front-end:

 const binding = oExtensionApi
        .getModel()
        .bindList("/MissionNeeds/MissionNeedsItem");

      const payLoad = {
        MissionNeedsID: selectedLine.MissionNeedsID,
                 _Item: { MissionNeedsID: selectedLine.MissionNeedsID, 
                          Product: selectedLine.Product, 
                          Quantity: "2.000", 
                          Unit: "ST",
                 },
      };

      binding.create(payLoad);

      binding.attachCreateCompleted((oEvent) => {
        console.log(oEvent);
      });

 Behaviour definition:

managed implementation in class zcl_mission_needs_behv unique; 
define behavior for ZR_MN_MissionNeeds alias MissionNeeds
persistent table zmn_missneeds
etag master LocalLastChangedAt
lock master
authorization master ( instance )
 
{
 
  field ( numbering : managed ) MissionNeedsID;
 
  field ( readonly )
  MissionNeedsID,
 
  Status,
 LoanHolderUser,
 
  CreatedAt,
  CreatedBy,
  LastChangedAt,
  LocalLastChangedAt,
  LocalLastChangedBy;
 
  create;
  update;
  delete;
 
  determination setStatus on modify { create; }
  determination setLoanHolderUser on modify { create; }
  determination setLastChangedAt on modify { create; }
 

  association _Item { create; } 
 
  mapping for ZMN_MISSNEEDS
  {
    MissionNeedsID = MISSION_NEEDS_ID;
    FromPlant = FROM_PLANT;
    FromStorageLocation = FROM_STORAGE_LOCATION;
    ToPlant = TO_PLANT;
    ToStorageLocation = TO_STORAGE_LOCATION;
    Status = STATUS;
    LoanHolderUser = LOAN_HOLDER_USER;
    Picker = picker;
    MissionName = mission_name;
    CreatedBy = CREATED_BY;
    CreatedAt = CREATED_AT;
    LocalLastChangedBy = LOCAL_LAST_CHANGED_BY;
    LocalLastChangedAt = LOCAL_LAST_CHANGED_AT;
    LastChangedAt = LAST_CHANGED_AT;
  }
}
 
define behavior for ZR_MN_MissionNeedsItem alias MissionNeedsItem
persistent table zmn_missnd_item
lock dependent by _MissionNeeds
authorization dependent by _MissionNeeds
 
{
  field ( numbering : managed ) MissionNeedsItemID;
 
  field ( readonly )
  MissionNeedsItemID,
  MissionNeedsID,
  Product,
  Quantity,
  Unit,
  CreatedAt,
  CreatedBy,
  LastChangedAt,
  LocalLastChangedAt,
  LocalLastChangedBy;
 
 
//    create;
  update;
  delete; 
 
  association _MissionNeeds { }
 
  mapping for ZMN_MISSND_ITEM
  {
    MissionNeedsItemID = MISSION_NEEDS_ITEM_ID;
    MissionNeedsID = MISSION_NEEDS_ID;
    Product = PRODUCT;
    Quantity = QUANTITY;
    Unit = UNIT;
    CreatedBy = CREATED_BY;
    CreatedAt = CREATED_AT;
    LocalLastChangedBy = LOCAL_LAST_CHANGED_BY;
    LocalLastChangedAt = LOCAL_LAST_CHANGED_AT;
    LastChangedAt = LAST_CHANGED_AT;
  }
}

 

 

Accepted Solutions (0)

Answers (1)

Answers (1)

satellarknighty
Explorer
0 Likes

I never tried to create both the header and an item at once, only the header first and then the item. You can try the following:

const binding = oExtensionApi
    .getModel()
    .bindList("/MissionNeeds/MissionNeedsItem");
const payLoad = {
    "MissionNeedsID" : selectedLine.MissionNeedsID
    };

binding.create(payLoad);

// instead of attachCreateCompleted i use created
binding.created().then(() => {
    const itemList = oExtensionApi.getModel()
        .bindList("/MissionNeeds/MissionNeedsItem('" + selectedLine.MissionNeedsID + "')/_Item");
    const itemBinding = itemList.create(
        { 
            "MissionNeedsID" : selectedLine.MissionNeedsID, 
            "Product": selectedLine.Product, 
            "Quantity": "2.000", 
            "Unit": "ST",
        });
});

 However with your current Behavior Definition, this won't work because fields of your child entity being readonly. Only MissionNeedsID will be saved, the other fields will have default value. You can fix this by using readonly : update

field ( readonly : update )
  MissionNeedsItemID,
  MissionNeedsID,
  Product,
  Quantity,
  Unit,
  CreatedAt,
  CreatedBy,
  LastChangedAt,
  LocalLastChangedAt,
  LocalLastChangedBy;