Defining Static Field Control and Value Helps

Objectives

After completing this lesson, you will be able to:
  • Enable editing.
  • Set input fields to read-only and mandatory.
  • Define value help for input fields.

Standard Operation UPDATE

Standard operations in behavior definition

In RAP, update is one of the standard operations. Standard operations are also known as CRUD operations, which is an acronym for create, read, update, delete.

While the read operation is always implicitly enabled for each entity listed in a CDS behavior, the modifying operations have to be listed explicitly to be available.

To enable standard operation update for an entity, add the update; statement to the entity behavior body (curly brackets after statement define behavior for …). To disable the operation, remove the statement or turn it into a comment.

In a managed scenario, the standard operations do not require an ABAP behavior pool, because they are completely handled by the RAP BO framework. In an unmanaged scenario, however, the standard operations must be implemented in the ABAP behavior pool.

Some interesting additions to statement update are as follows:

Internal

Prefix which disables the operation for external consumers of the business object.

Features: instance or features: global

Enable dynamic feature control. The decision, where and when the operation is enabled, is made dynamically in a behavior implementation.

Precheck

A method is called before the update request to prevent unwanted changes from reaching the application buffer.

UPDATE in the behavior projection

To make a standard operation update available in the OData Service, it has to be exposed in the behavior definition on projection level (behavior projection).

To include standard operation update in the OData service, add statement use update; to the entity behavior body (curly brackets after statement define behavior for …). To disable the operation, remove the statement or turn it into a comment.

Note

Behavior projections can have their own implementations, which can be used to augment the implementation of a standard operation or provide more prechecks. It is a special case that we don't cover in this class.

When the standard operation Update is available in the OData service, the service preview displays an Edit button on the Object Page for the related RAP BO entity. By choosing this button, the user enters an edit mode for the data displayed on the page.

Hint

After editing the behavior projection, you can press Ctrl + F5 to perform a hard refresh of the service preview before you can see the new button. Sometimes, you even have to clear your browsing data. (Press Ctrl + Shift + Del for that).

Static Field Control

Read-Only Fields

Different variants of the read-only input field

The field statement is used in behavior definitions, to specify the characteristics of fields. The statement is always located between the curly brackets after the statement define behavior (the entity behavior body). You can use commas to classify multiple fields in the same way.

For the readonly characteristic of fields, the following variants exist:

field ( readonly )
  • Values of the specified fields must not be be created or updated by the RAP BO consumer.
  • This is independent from the standard operation the consumers wants to perform (update, create).
field ( readonly : update)
  • Defines a field as read-only during update operations.
  • An external consumer can set the fields in question during create, but cannot change them later during update.
  • Technically, this is a dynamic field attribute, but not implementation in the behavior pool is required.

Note

The readonly and readonly:update property only applies to external access. EML statements using the IN LOCAL MODE addition bypass the static field control. Thus, the behavior implementation of the business object itself is always able to change all fields.

To include the field characteristics into the OData service, it is not necessary to add the field statements in the behavior projection. The related information is directly propagated into the OData service. The SAP Fiori elements UI uses this metadata for UI hints, that is, depending on the operation, the fields are displayed as editable or read-only.

Mandatory Fields

Different variants of the mandatory input field

For the mandatory characteristic of fields, the following variants exist:

Field ( mandatory )
  • The RAP BO always requires a value for the specified field(s) before persisting them on the database.
  • This is independent from the standard operation the consumers wants to perform (update, create).

There is no runtime check for mandatory fields and no runtime error occurs if a mandatory field is not filled. If a runtime check is required, the application developer must implement it using a validation.

Field ( mandatory : create )
  • Defines a field as mandatory during create operations.
  • An external consumer must set the fields in question only during create.
  • If a consumer tries to create a new instance without entering a value into a field defines as mandatory:create, a runtime error occurs.
  • Technically, this is a dynamic field attribute, but not implementation in the behavior pool is required.

The SAP Fiori elements UI uses the metadata to display fields as mandatory.

Suppressed Fields

An example of Suppress field

Field(suppress) can be used to remove a field from the derived types, field mapping, and all RAP APIs. It is possible for all fields except for key fields, foreign key fields, and fields used by the current behavior definition, such as ETag fields.

If a RAP BO consumer tries to modify a suppressed field, an error message occurs that tells the consumer that no field of this name exists.

Hidden Fields

An example of hidden field

Instead of excluding a field from the RAP BO behavior, you can exclude it only from consumption, for example from the OData service, or from the user interface. It is done using annotations in the CDS view definition or the CDS metadata extension.

Fields can be hidden by one of the following annotations:

@Consumption.hidden: true

The field is not exposed for any consumption. This means that it is part of the OData Service and, therefore, not available on the UI.

@UI.hidden: true

The field is not available on the UI. This means that it is not displayed and the user cannot make it visible using personalization. If, however, there is no additional annotation @Consumption.hidden: true, the field can still be part of the OData service.

If you want to hide a field that is needed in the OData service, you cannot use @Consumption.hidden: true. You have to use @UI.hidden: true instead. A good example is the exclusion of technical key fields and ETag fields from the UI.

Value Help Definition

An example of Value-help in SAP Fiori app

The implementation of a value help in CDS enables the end user to choose values from a predefined list for input fields on a user interface.

In SAP Fiori elements apps, value helps are invoked by choosing the value help button next to the input field or by pressing F4.

By default, the value help dialog consists of a filter dialog, which the user can hide and unhide, and a list with suggested values.

An example of Multiple Value Helps on One Input Field

It is possible to provide more than one value help on one input field. The end user can select which value help to use from a dropdown list.

Note

This is similar to collective search help in classical ABAP dialog programming techniques (Dynpro and Web Dynpro), but those collective search helps used a tabstrip for visualization instead of a dropdown list.

Steps to provide Value help in CDS-based OData service

To provide a value help for a given field, you first need a CDS view that contains the values for the value help. This view is referred to as the value help provider. Then, you annotate your field with @Consumption.valueHelpDefinition and provide the name of the value help provider and an element for the mapping in the annotation.

Note

It is also possible to provide value help based on associations, but some restrictions apply in this case. We don't discuss this option in this course.

You can use any CDS entity as a value help provider that contains the desired values of the element for the input field. However, developers often define dedicated value help views, because the layout and functionality of the value help dialog is derived from the metadata of the value help provider view.

Note

If you are looking for a CDS entity that you can use as a value help provider, it can be helpful to look for a foreign key relation in the underlying table or an association in the CDS-based data model. Note that the existence of foreign keys or associations is not a prerequisite for providing a value help.

An overview of the simple value help

To provide a simple value help for a field, edit the data definition of the source view and annotate the field with the following annotation:

Code Snippet
12345
@Consumption.valueHelpDefinition: [ { entity.name: 'entityRef', entity.element: 'elementRef' } ]

Here, entityRef is the name of the CDS entity that is used as value help provider and elementRef the element of the value help provider that is used as output parameter of the value help.

When you expose the source view in an OData service, the value help provider view is automatically exposed with it. You do not have to list value help provider views in the service definition.

On the SAP Fiori Elements UI, choosing F4 in the selection field opens a search mask and the end user can filter by any field in the value help provider view. Selecting an entry transfers the value of the element that is referenced in the annotation to the annotated element in the source view.

An overview of value help with additional binding

You use additional binding to define more than one binding conditions between elements of the source view and elements of the value help provider view.

The additional bindings are defined with the subannotation additionalBindings of annotation @Consumption.valueHelpDefinition. The subannotation is followed by a pair of square brackets ([..]) with a comma-separated list of element bindings.

Each element binding connects exactly one element of the source view (localElement) and one element of the value help provider view (element).

In addition, it specifies a usage with one of the following values:

#FILTER

 The value of the referenced element in localElement is used as input for the value help. The value help proposes only entries that match this filter value.

#RESULT

The value of the referenced element in element is used as extra output. When an entry is selected in the value help, this value is transferred to the input field that is based on the referenced element in localElement.

#FILTER_AND_RESULT

The binding is used for input and output.

Maintain Static Field Properties and Value Help

In this exercise, you enable direct editing of data in the preview of the OData UI service. You extend the behavior definition with static field control and use CDS annotations to provide value help.

Note

In this exercise, replace ## with your group number.

Solution

Repository Object TypeRepository Object ID
CDS Behavior Definition (Model)/LRN/437C_R_TRAVEL
CDS Behavior Definition (Projection)/LRN/437C_C_TRAVEL
CDS Data Definition (Projection)/LRN/437C_C_TRAVEL

Task 1: Enable Direct Editing of Data

Enable direct editing on the object page of your transactional SAP Fiori elements app. To achieve this, make sure that the update operation is part of your business object and your OData service.

Steps

  1. Open the behavior definition for your business object Z##_R_Travel and make sure it defines the update operation. If the statement is missing, add it. If it is commented out, remove the comment signs.

    1. Make sure that the behavior definition contains the following statement:

      Code Snippet
      1
      update;
  2. Activate the behavior definition.

    1. Press Ctrl + F3 to activate the development object.

  3. Open the behavior projection, that is, the behavior definition on projection level (Z##_C_Travel) and make sure it exposes the update operation to the OData service. If the statement is missing, add it. If it is commented out, remove the comment signs.

    1. Make sure that the behavior projection contains the following statement:

      Code Snippet
      1
      use update;
  4. Activate the behavior projection.

    1. Press Ctrl + F3 to activate the development object.

  5. Test the preview of your OData UI service to see the effect.

    1. Reopen the preview for the OData UI service.

    2. Select a travel for which you have change authorization, that is, for which the Cancel the Travel button is active.

    3. Choose this travel to navigate to the details page.

      Result

      You see an Edit button in the right upper corner.

Task 2: Add Static Field Control

In the behavior definition on data model level (Z##_R_Travel), add static field control. Disable any direct editing for the Status field and the administrative fields, that is, the ChangedAt field and the ChangedBy field. Ensure that direct editing of the key fields AgencyId and TravelId is not allowed during update but is mandatory during create. Set all other fields as mandatory.

Steps

  1. Edit the behavior definition on data model level. Add a FIELD statement for the view elements Status, ChangedAt, and ChangedBy. Inside the brackets, add the relevant keyword to disable editing in general.

    1. Add the following code:

      Code Snippet
      1234
      field ( readonly ) Status, ChangedAt, ChangedBy;
  2. For the view elements AgencyId and TravelId, disable editing during the update operation and enforce entries during the create operation. Add a corresponding FIELD statement to achieve this behavior or, if a FIELD statement already exists for these fields, adjust it accordingly.

    1. Make sure your code is similar to the following:

      Code Snippet
      123
      field ( readonly : update, mandatory : create ) AgencyId, TravelId;
  3. Add a FIELD statement, followed by view elements Description, CustomerId, BeginDate, and EndDate. Inside the brackets, add the relevant keyword to define the fields as mandatory.

    1. Add the following code:

      Code Snippet
      12345
      field ( mandatory ) Description, CustomerId, BeginDate, EndDate;
  4. Activate the behavior definition.

    1. Press Ctrl + F3 to activate the development object.

  5. Test the OData UI service to see the effect of static field control.

    1. Reopen the preview for the OData UI service.

    2. Select a travel for which you have change authorization, that is, for which the Cancel the Travel button is active.

    3. Choose this travel to navigate to the details page.

    4. Choose Edit.

      Result

      You see a red asterisk next to the labels Description, Customer ID, Starting Date, and End Date. The other input fields are disabled.
    5. Clear the input field Description and choose Save.

      Result

      Even though the field is shown as mandatory, you can save the data without a description. There is no implicit input check to prevent initial mandatory fields.

Task 3: Provide a Value Help

Add the necessary annotations to view element CustomerId to define a value help based on CDS view /DMO/I_Customer_StdVH.

Steps

  1. Edit the definition of your projection view entity Z##_C_Travel. Before view element CustomerId, add the necessary sub annotation of the @Consumptionannotation to define a value help for this field.

    1. Adjust the code as follows:

      Code Snippet
      123456789101112
      define root view entity Z##_C_Travel provider contract transactional_query as projection on Z##_R_Travel { key AgencyId, key TravelId, @Search.defaultSearchElement: true Description, @Search.defaultSearchElement: true @Consumption.valueHelpDefinition: [ { } ] CustomerId,
  2. Within the annotation, define a single value help based on the CDS view entity /DMO/I_Customer_StdVH and its key field CustomerID.

    1. Adjust the code as follows:

      Code Snippet
      12345678910111213
      define root view entity Z##_C_Travel provider contract transactional_query as projection on Z##_R_Travel { key AgencyId, key TravelId, @Search.defaultSearchElement: true Description, @Search.defaultSearchElement: true @Consumption.valueHelpDefinition: [ { entity: { name: '/DMO/I_Customer_StdVH', element: 'CustomerID' } } ] CustomerId,
  3. Activate the data definition.

    1. Press Ctrl + F3 to activate the development object.

  4. Run the preview of your OData UI service. Verify that there is now a value help for the customer field.

    1. Reopen the preview for the OData UI service.

    2. Select a travel for which you have change authorization, that is, for which the Cancel the Travel button is active.

    3. Choose this travel to navigate to the details page.

    4. Choose Edit.

    5. In the Customer ID field, enter a value that is not provided by the value help (for example 999999) and choose Save.

      Result

      Even though there is a value help with a list of existing customers, you can save the data referencing a customer that does not exist. There is no implicit input check to prevent inconsistent data.