Introduction
In this blog you will understand how to create Odata annotations in SEGW with example and consume the service in visual studio and display the output in fiori launchpad.
When ever you are adding annotations to odata service in segw we are suppose to write the coding in MPC_EXT class inside DEFINE METHOD( you have to redefine this method).
Usually we write annotations in CDS view and then consume it in odata service but in this blog you will see an example of creating annotations in segw.
Example 1 : Make filter mandatory in List Report Launchpad
Create Odata project in SEGW with few fields as shown below in Fig 1
Fig 1
While importing DDIC structure check on checkbox to create entityset as well.
Save and generate runtime artifacts.
Enable filter option as shown in Fig 2 in the entity type property so that filter option will be enabled in fiori launch pad.
Fig 2
Once the artifacts are generated redefine get_entity in DPC_EXT class to fetch multiple records.
Fig 3
Further we are going to write logic in MPC_EXT class for creating annotations.
Redefine DEFINE method in MPC_EXT to create annotations.
In this example we are making a field as required by using Odata annotations.
Fig 4
super() keyword is mandatory if you don't specify to will throw an error in the output screen like no entity found.
This below code it used to make any one of the field as mandatory in fiori launchpad output screen.
I am making Matnr field as mandatory in this example.
CODE :-
method DEFINE.
data : lo_annotation type ref to /iwbep/if_mgw_odata_annotation,
lo_property type ref to /iwbep/if_mgw_odata_property,
lo_entity_set type ref to /iwbep/if_mgw_odata_entity_set,
lo_entity_type type ref to /iwbep/if_mgw_odata_entity_typ.
try .
"make matnr field mandatory and single value
lo_entity_type = model->get_entity_type( iv_entity_name = gc_material ).
lo_property = lo_entity_type->get_property( iv_property_name = 'Matnr' ).
lo_annotation = lo_property->/iwbep/if_mgw_odata_annotatabl~create_annotation( 'sap' ).
lo_annotation->add( iv_key = 'required-in-filter'
iv_value = 'true' ).
lo_annotation->add( iv_key = 'filter-restriction'
iv_value = 'single-value' ).
catch /iwbep/cx_mgw_med_exception .
endtry.
endmethod.
Activate all methods and regenerate the runtime artifacts.
Next you have to consume this odata service in visual studio.
consume the service created in visual studio.
Create one new folder and type cmd in the folder path.
Fig 5
Click on enter, command prompt will open. Type code . in command prompt and click on enter.
Fig 6
It will redirect to visual studio code.
Fig 7
Click on view and select command palette. Choose Fiori : Open Application Generator
Fig 8
Choose SAP fiori elements and select list report page and click on next.
Fig 9
Make the connection as shown below and click on next and choose your odata service.
Fig 10
Once the connection is successful it will show the entityset name in next screen
Fig 11
Click next and finish.
Once you click on finish it will install the dependencies. Once it is done it will create node module in your project folder.
After that right click on project and click on preview application.
Select fiori start run and press enter.
Fig 12
The execution will start and will be redirected to the browser page.
Click on adapt filters and select fields to be displayed and click on OK.
Fig 13
Output example 1
Now you observe the expected output.
Fig 14
Example 2 : Display Drop Down instead of Value help in new window
To display dropdown in the window you just have to add extra code in DEFINE method of MPC_EXT class and rest all steps will be same.
In this example I am try to add dropdown for the Ernam field.
Find the below code in DEFINE method for dropdown
Fig 15
CODE :-
method DEFINE.
super->define( ).
data : lo_annotation type ref to /iwbep/if_mgw_odata_annotation,
lo_property type ref to /iwbep/if_mgw_odata_property,
lo_entity_set type ref to /iwbep/if_mgw_odata_entity_set,
lo_entity_type type ref to /iwbep/if_mgw_odata_entity_typ.
"Adding dropdown for Ernam field
try .
lo_entity_type = model->get_entity_type( iv_entity_name = gc_material ).
lo_property = lo_entity_type->get_property( iv_property_name = 'Ernam' ).
lo_property->set_value_list( /iwbep/if_mgw_odata_property=>gcs_value_list_type_property-FIXED_VALUES ) .
catch /iwbep/cx_mgw_med_exception .
endtry.
endmethod.
Output example 2
Fig 16
In the above output 2 screenshot you can see that Matnr field is marked mandatory and for Ernam field it is showing dropdown instead of search help, that is what we have acheived through annotations in this particular example.
Just for the reference you can see the below screenshot where Matnr field and Ernam field is as normal as others before using annotations.
Fig 17
Example 3 : The filter-restriction makes date filter to display calendar control where user can specify from and to date.
In this example for date field you control the input from the user to specify from and to date.
Find the below to acheive this in DEFINE method of MPC_EXT class.
Fig 18
CODE :-
method DEFINE.
super->define( ).
data : lo_annotation type ref to /iwbep/if_mgw_odata_annotation,
lo_property type ref to /iwbep/if_mgw_odata_property,
lo_entity_set type ref to /iwbep/if_mgw_odata_entity_set,
lo_entity_type type ref to /iwbep/if_mgw_odata_entity_typ.
try .
"Setting date interval for Ersda field
lo_entity_type = model->get_entity_type( iv_entity_name = gc_material ).
lo_property = lo_entity_type->get_property( iv_property_name = 'Ersda' ).
lo_annotation = lo_property->/iwbep/if_mgw_odata_annotatabl~create_annotation( 'sap' ).
lo_annotation->add( iv_key = 'required-in-filter' iv_value = 'Ersda' ).
lo_annotation->add( iv_key = 'filter-restriction' iv_value = 'interval' ).
catch /iwbep/cx_mgx_med_exception.
endtry.
endmethod.
Output example 3
Fig 19
Conclusion
By using annotations in SEGW not only mandatory field can be created and dropdown can be created for a field, any other validations can be performed.