How to Implement Value Help using Smartfield and Odata Annotation

In this blog I’ll show you how you can implement value help using smartfield and annotation from Odata. Also, I’ll show you how to implement get entity set method to full fill queries from frontend.

I have field Customer in Entity NotificationHeader on which I’ll implement search help in UI. In UI view smartfield control is used for Customer field.

<smartField:SmartLabel labelFor="idCustomer"/>
<smartField:SmartField value="{Customer}" id="idCustomer"/>

Preparing OData

First thing, add another Entity CustomerSrchHelp which will provide values for Customer Search help. I am going to using search help DEBIA to full fill customer search help values so I have defined fields in entity from view M_DEBIA.

Also, note that ‘Sortable’ and ‘Filterable’ column will also affect your Value help. UI will allow you to sort on columns which you have marked as ‘Sorttable’. And ‘Filterable’ columns will appear as inputs in value help dialog. Notice Postal Code and City fields below.

For UI to use labels for columns and filter input we will have to define labels in class. (Don’t know why it does not take value from Odata)

Create a class and defined text symbols in that class. Now come back to OData and click on ‘Label’ Column select ‘Class’ in Reference type field, specify the class name and key (which is text symbol number).

Assign text symbol to all fields which you plan to use in value help.

Adding Annotation

Open up DPC_EXT class and redefine DEFINE method. In code first, make call to SUPER->DEFINE() . I am going to use helper class CL_FIS_SHLP_ANNOTATION to create annotation. Refer code below.

METHOD define.

DATA : lv_namespace  TYPE string,
lr_annotation TYPE REF TO cl_fis_shlp_annotation.

super->define( ) .

model->set_soft_state_enabled( iv_soft_state_enabled = abap_true ).
model->get_schema_namespace( IMPORTING ev_namespace = lv_namespace ).

"Customer Search Help Annotation
lr_annotation = cl_fis_shlp_annotation=>create(
io_odata_model               = model
io_vocan_model               = vocab_anno_model
iv_namespace                 = lv_namespace
iv_entitytype                = 'NotificationHeader'
iv_property                  = 'Customer'
iv_search_help               = 'DEBIA'
iv_search_help_field         = 'KUNNR'
iv_valuelist_entityset       = 'CustomerSrchHelpSet'
iv_valuelist_property        = 'Customer' ).

lr_annotation->add_display_parameter( iv_valuelist_property  = 'Name' ) .
lr_annotation->add_display_parameter( iv_valuelist_property  = 'Country' ) .
lr_annotation->add_display_parameter( iv_valuelist_property  = 'PostCode' ) .
lr_annotation->add_display_parameter( iv_valuelist_property  = 'City' ) .

ENDMETHOD.

Read more about soft state

Explanation of what is what.

(A)NotificationHeader is entity which has field (B)Customer where we I like to add value help. (C)DEBIA is the name of search help and (C)KUNNR is the field in search help which will return key value, which is customer number. (D)CustomerSrchHelpSet is the entity set which will provide data for search help list and (E)Customer is the field in CustomerSrchHelpSet which will contain value for customer number.

You might ask if CustomerSrchHelpSet will implement data retrieval then what’s point of specifying search help name here is. Helper class goes in the definition of search help and if it supports search then in annotation ‘SearchSuppoted’ is added.

Fields are added using method ADD_DISPLAY_PARAMETER to be included in suggestion and value list of value help dialog.

After above code, if you check metadata you should see annotation.

<Annotations Target="EAM_NTF_CREATE.NotificationHeader/Customer"
xmlns="http://docs.oasis-open.org/odata/ns/edm">
<Annotation Term="com.sap.vocabularies.Common.v1.ValueList">
<Record>
<PropertyValue Property="Label" String="Customers (General)"/>
<PropertyValue Property="CollectionPath" String="CustomerSrchHelpSet"/>
<PropertyValue Property="SearchSupported" Bool="true"/>
<PropertyValue Property="Parameters">
<Collection>
<Record Type="com.sap.vocabularies.Common.v1.ValueListParameterInOut">
<PropertyValue Property="LocalDataProperty" PropertyPath="Customer"/>
<PropertyValue Property="ValueListProperty" String="Customer"/>
</Record>
<Record Type="com.sap.vocabularies.Common.v1.ValueListParameterDisplayOnly">
<PropertyValue Property="ValueListProperty" String="Name"/>
</Record>
<Record Type="com.sap.vocabularies.Common.v1.ValueListParameterDisplayOnly">
<PropertyValue Property="ValueListProperty" String="Country"/>
</Record>
<Record Type="com.sap.vocabularies.Common.v1.ValueListParameterDisplayOnly">
<PropertyValue Property="ValueListProperty" String="PostCode"/>
</Record>
<Record Type="com.sap.vocabularies.Common.v1.ValueListParameterDisplayOnly">
<PropertyValue Property="ValueListProperty" String="City"/>
</Record>
</Collection>
</PropertyValue>
</Record>
</Annotation>
</Annotations>

Notice label ‘Customer (General)’ and SearchSupported properties. These are coming from definition of search help DEBIA.
After this value help button appears next to input.

Implement GET_ENTITYSET method to provide values

Go to DPC_EXT class and redefine *_GET_ENTITYSET method of the entity, in this case, CUSTOMERSRCH_GET_ENTITYSET.
There is another help class CL_FIS_SHLP_PROCESSOR which will help us get values. Check the code below.

METHOD customersrch_get_entityset.

DATA: lt_entityset  TYPE zcl_zpm_eam_ntf_create_mpc=>tt_customersrchhelp .

cl_fis_shlp_processor=>get_values(
EXPORTING
iv_search_help               = 'DEBIA'
iv_search_help_field         = 'KUNNR'
io_tech_request_context      = io_tech_request_context
IMPORTING
et_entityset                 = lt_entityset
es_response_context          = es_response_context ).

et_entityset = lt_entityset .

ENDMETHOD.

Search help is now fully functional in UI.

Input in Customer field execute query to the backend

GET CustomerSrchHelpSet?&search-focus=Customer&search=42100&

And this request is handled well by helper class. Note that search is working on all columns. Also, value help dialog working with filters.

This search based on filter values execute below query to the backend. Try implementing that manually 🙂

GET CustomerSrchHelpSet?$skip=0&$top=108&$orderby=Customer%20asc&
$filter=((substringof(%277501%27,PostCode))%20and%20(City%20eq%20%27PARIS%27))
&search-focus=Customer&search=&$select=Customer%2cName%2cCountry%2c
PostCode%2cCity&$inlinecount=allpages HTTP/1.1

9 Replies to “How to Implement Value Help using Smartfield and Odata Annotation

  1. How to use VL_SH entity for using the structure from search helps without creating an explicit entity type.

  2. Have you found a way to use the ValueList annotation for a Value Help that is dependent on another property? For example, I have a home-grown Customer entity with the properties Country, Region, and City. The Value Help for the Region property should be limited to only those regions within the selected Country, and the same goes for the Value Help of the City property. So far, I have not had any luck.

  3. This search based on filter values execute below query to the back end. Try implementing that manually .—- Do we need to implement logic manually or search help is taking care of it.

    I have tried but search is not working and result set columns are not displaying.

Leave a Reply