Skip to Content

In the blog Step by Step to create CDS view through SmartTemplate + WebIDE and Create a CRM Service Order Fiori application within a couple of minutes we get an Fiori application generated which needs several fine-tuning on appearance. For example, the status field is by default rendered as a pure input field with technical status code like “OPEN”, “PROC” displayed. It is better to render it as a drop down list with human readable text like “Open”, “In process” displayed as drop down list item.

/wp-content/uploads/2016/04/clipboard1_931399.png

I searched in SCN and could not find a working solution for it, so I spent some time for research and would like to share with you here.

Step1 Create a simple CDS view to hold status code and status description

@AbapCatalog.sqlViewName: 'zstatusfixed'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'fixed value'
define view Z_C_Status_Fixedvalue as select from zstatus_fixedval {
   @EndUserText.label: 'status key for fixed value'
   key zstatus_fixedval.status_code,
   @EndUserText.label: 'status description for fixed value'
   zstatus_fixedval.status_text
}

Previewed as below:

/wp-content/uploads/2016/04/clipboard2_931403.png

Here below is the table definition on top of which the CDS view above is created.

/wp-content/uploads/2016/04/clipboard3_931404.png

For demonstration purpose I create a Z table and only inserted three status items:

/wp-content/uploads/2016/04/clipboard4_931405.png

Step2 link the status field to the CDS view created in previous step

I have defined the consumption view Z_C_Service_Order_View with below source code:

define view Z_C_Service_Order_View as select from Z_i_Order_View
...
@UI.identification: [ { position: 50 } ]
@Consumption.valueHelp: '_statusfixedvalue'
@ObjectModel: { foreignKey.association: '_statusfixedvalue', mandatory: true }
Z_i_Order_View.txt04,
...

Where does the definition of “_statusfixedvalue” come from?

It is defined in view Z_i_Over_View:

define view Z_i_Over_View as select from XXXX
association [0..1] to Z_C_Status_Fixedvalue as _statusfixedvalue
  on  $projection.txt04 = _statusfixedvalue.status_code
{
   ...
   _status_text.txt04,
  ...
   @ObjectModel.association.type: #TO_COMPOSITION_CHILD
   _statusfixedvalue
}

So far the work on CDS side is done.

Step3 create necessary annotation in ABAP code

Prerequisite: you should first create a project using tcode SEGW and then include your CDS consumption view via the context menu as below:

/wp-content/uploads/2016/04/clipboard1_931399.png

Redefine method DEFINE of your MPC_EXT class with following source code:

    super->define( ).
    DATA lo_annotation TYPE REF TO /iwbep/if_mgw_odata_annotation.
    DATA  lo_property TYPE REF TO /iwbep/if_mgw_odata_property.
    DATA  lo_entity_set TYPE REF TO /iwbep/if_mgw_odata_entity_set.
    lo_entity_set = model->get_entity_set( 'Z_C_Service_Order_View' ).
    lo_annotation = lo_entity_set->create_annotation( 'sap' ).
    lo_annotation->add( iv_key = 'semantics' iv_value = 'fixed-values').
    DATA(lo_entitytype) = model->get_entity_type( 'Z_C_Service_Order_ViewType' ).
    lo_entitytype->set_is_value_list( abap_true ).
    data(lo_txt_property) = model->get_entity_type( 'Z_C_Service_Order_ViewType' )->get_property( 'txt04' ).
    lo_txt_property->set_value_list( /iwbep/if_mgw_odata_property=>gcs_value_list_type_property-fixed_values ).
    data(lo_text_anno) = lo_txt_property->/iwbep/if_mgw_odata_annotatabl~create_annotation( 'sap' ).
    lo_text_anno->add( iv_key = 'text' iv_value = 'to_statusfixedvalue/status_text').
    lo_txt_property = model->get_entity_type( 'Z_C_Status_FixedvalueType' )->get_property( 'status_code' ).
    lo_txt_property->set_value_list( /iwbep/if_mgw_odata_property=>gcs_value_list_type_property-fixed_values ).
    lo_text_anno = lo_txt_property->/iwbep/if_mgw_odata_annotatabl~create_annotation( 'sap' ).
    lo_text_anno->add( iv_key = 'text' iv_value = 'status_text').

Note: those ABAP code is necessary, or else you will only get an ugly drop down list: only status code is displayed:

/wp-content/uploads/2016/04/clipboard5_931406.png

Final result

In display mode and in edit mode, the status description is displayed:

/wp-content/uploads/2016/04/clipboard6_931407.png

/wp-content/uploads/2016/04/clipboard7_931408.png

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

  1. Beat Birrer

    Hi Jerry,

    is it also possible to set the annotation “sap:semantics: fixed-values” for the entity set directly in the CDS-View? I use the “OData.publish-Annotation” for the generation of the oData-Service and in this case there is no mpc_ext class available in the Service . At the moment I have a value list dialog instead of a drop down list.

    Regards,

    Beat

    (0) 

Leave a Reply