Show TOC

Static Field ControlLocate this document in the navigation structure

Here we explain how you can implement static field control in CDS views.

Overview

In a typical transactional scenario, you have to implement so-called business object CDS views when modeling business data. Such views are required for business object generation on the basis of the BOPF framework. To provide static field control for your application, you have to add the appropriate CDS annotations in the corresponding business object view - in fact, for both the VIEW and the ELEMENT scope. Using annotations with the VIEW scope, you define the field control that is related to the whole entity. You thus specify at the business object node level whether each node instance is enabled for creation, update, or deletion. When you add annotations with the ELEMENT scope, you have the option to define the field control for each individual view field and specify the field control at the attribute level of the corresponding business object node. If no such field control annotations are explicitly added to the view, the default value (read-only) is assumed.

Field control annotations can be restricted in the consumption view. This means you can overwrite read-only value and change it from false (as defined in the BO view) to true (in the consumption view).

Defining Static Field Control in Business Object Views

Entity Control at Business Object‘s Node Instance Level

The following annotations are relevant at the CDS view level:

Annotation and Value

Effect

@ObjectModel.createEnabled: true/false Specifies whether new instances of a business object node that correspond to the underlying CDS view can be created

If this annotation has the value true, creation of new instances of business object nodes is allowed.

@ObjectModel.updateEnabled: true/false Specifies whether existing instances of a business object node that corresponds to the underlying CDS view can be updated

If this annotation has the value true, updating existing instances is allowed.

@ObjectModel.deleteEnabled: true/false Specifies whether existing instances of a business object node that corresponds to the underlying CDS view can be deleted

If this annotation has the value true, updating existing instances is allowed.

Example

The annotations mentioned above are used in a business object CDS view (in our example: ZDEMO_I_SalesOrder_TX) to define that instances of a business object node can be created and updated, but not deleted.

	...
	@ObjectModel.transactionalProcessingEnabled: true  
	@ObjectModel.writeActivePersistence: 'ZTAB_SO'
	
	@ObjectModel.createEnabled: true
	@ObjectModel.deleteEnabled: false 
	@ObjectModel.updateEnabled: true
	
	
	define view ZDEMO_I_SalesOrder_TX 
	
	      as select from ztab_so as SalesOrder  
	association...
	{   
	      ...
	}  
	
Effect on the Generated Business Object (in case of BOPF BOs):

The business object CDS view from the sample code above is used for generating the corresponding sales order business object. As you can see in the figure below, the field control that is related to the whole entity is defined accordingly at the business object’s node level by the properties Create/Update/Delete Enabled.

BO editor with properties of the business object node corresponding to the CDS view
Figure 1: BO editor with properties of the business object node corresponding to the CDS view

Static Field Control at the Business Object’s Attribute Level

The annotations (required) at element level are:

Annotation and Values

Effect

@ObjectModel.readOnly: true/false If this annotation has the value true, the annotated element must not be updated by the consumer.

The BOPF runtime rejects modification requests when updating fields that provide a value true.

@ObjectModel.mandatory: true Defines that the element is mandatory

If this annotation has value true, the field must be filled by the consumer when executing a modification.

Example

For the business object CDS view from the sales order example above, some field control annotations are used to restrict properties of particular fields. Here, the element BusinessPartner cannot be modified, whereas the value of the elements CurrencyCode, GrossAmount, NetAmount, and BillingStatus can be created or updated. The key element SalesOrder is mandatory, that is, the property must contain a value.

	...
	@ObjectModel.updateEnabled: true
	
	
	define view ZDEMO_I_SalesOrder_TX 
	
		as select from ztab_so as SalesOrder  
	    association...
	{   
		@ObjectModel.mandatory: true
		key SalesOrder.salesorder           as SalesOrder, 
	
		@ObjectModel.readOnly: true 
		SalesOrder.businesspartner          as BusinessPartner,       
	
		@ObjectModel.readOnly: false  
		SalesOrder.currencycode             as CurrencyCode, 
	
		@ObjectModel.readOnly: false
		SalesOrder.grossamount              as GrossAmount, 
	
		@ObjectModel.readOnly: false
		SalesOrder.netamount                as NetAmount, 
	
		@ObjectModel.readOnly: true
		SalesOrder.billingstatus            as BillingStatus,     
		...
	}  
	

Effect to the Generated Business Object (in case of BOPF BOs):

The field control annotations restrict the attributes of the corresponding business object node. In the BO editor, these attributes are listed in the Properties tab.

BO editor with individual attributes (Properties tab) of the business object node
Figure 2: BO editor with individual attributes (Properties tab) of the business object node
Restricting Field Control at the Consumption View Level

You can use field control annotations in the consumption view to restrict the values for static field control that have been specified at the BO view level. In other words, you can overwrite the read-only value and change it from false (in the BO view) to true (in the consumption view).

Remember CDS Rule: Remember to double-maintain the annotations that have the VIEW scope. In CDS views, only the annotations with ELEMENT scope are inherited from the business object view.

Example: Adding Annotations for Non-Editable Fields

For the sales order scenario above, the consumption view ZDEMO_C_SalesOrder_TX adds the annotation @ObjectModel.readOnly: true to mark the fields GrossAmount and NetAmount as non-editable for the consumer – as not designated by the BO view.

	...
	-- Repeated annotations from BO view – VIEW scope
	@ObjectModel.createEnabled: true
	@ObjectModel.deleteEnabled: false 
	@ObjectModel.updateEnabled: true
	
	-- Consumption view
	define view ZDEMO_C_SalesOrder_TX 
	
	    as select from ZDEMO_I_SalesOrder_TX as Document  
	    ...
	{   
		@UI...     
		key Document.SalesOrder,       -- inherits field control value
	
		@UI...
		Document.BusinessPartner,      -- inherits field control value
	
		@UI...
		Document.CurrencyCode,         -- inherits field control value
	
		@UI...
		@ObjectModel.readOnly: true    -- restricts to non-editable field
		Document.GrossAmount,
	
		@UI...
		@ObjectModel.readOnly: true    -- restricts to non-editable field
		Document.NetAmount,
	
		@UI...
		Document.BillingStatus
	}