cancel
Showing results for 
Search instead for 
Did you mean: 

CAP + Typescript: Draft UPDATE is updating readonly fields -- a bug?

shf
Explorer
0 Kudos
247

Hello CAP and Typescript experts,

I have defined an draft-enabled entity with additional computed fields as follows:

 

 

entity BaseSystemTypes : managed {
        @title: '{i18n>Paltr_SystemTypes_ID_label}'
    key ID       : types.SystemType;

        @title: '{i18n>Paltr_Common_Name}'
        @mandatory
        techName : types.TechnicalName;

        @title: '{i18n>Paltr_Common_title}'
        @mandatory
        title    : types.Title;

        @title: '{i18n>Paltr_Common_isUsable_label}'
        isUsable : Boolean default true;

        myComputedField: String = ('');
}

 

Within the service SystemTypes as projection on BaseSystemTypes, I let calculate the field myComputedField after READ:

 

srv.after('READ', SystemTypes, async (req) => {
      if (!req) return;       
      
      ...       

      req.forEach((singleEntity: any) => {
        singleEntity.myComputedField = 'Test';
      }
}

 

In the Fiori App, if I create a new draft instance or change one of the fields manually of an existing record, the draft cannot be saved because "Property myComputedField does not exist in myservice.SystemTypes". If I comment the singleEntity.myComputedField = 'Test', the changes on drafts are working.

If I check the database table BaseSystemTypes, I cannot find the calculated field and I can see the calculated field within the draft views and draft table regarding myService_BaseSystemTypes_drafts. This is correct as far as I know.

But getting an error during creation or updating a draft , is this a bug now inside the UI5 framework ? Or do I have to delete now the changes using the srv.on events for CREATE/UPDATE ? I assumed calculated fields are ignored during any updates. 

View Entire Topic
patricebender
Product and Topic Expert
Product and Topic Expert
0 Kudos

In the cds model, a computed field looks like this:'

"foo": { "@Core.Computed": true, "value": { "val": "bar" } }

I'm not sure what you are trying to achieve but if you want to adapt the value of a computed field,
you'd need to manipulate the object properly: `<entity>.foo.value.val = 'YOUR VALUE'`

catano
Active Participant
0 Kudos

Hi @patricebender , by the setup I think @shf  maybe refers to Calculated Elements

catano
Active Participant
0 Kudos

But I do agree that maybe what it needs to be considered is the use of a Virtual Element.