Implementing optional parameters in the function import
It’s a common knowledge that those actions which cannot be directly modelled via the CRUDQ operations are implemented via function imports. Examples like – On display screen data retrieval of a different entity not directly in context of bound entity but dependent on few values of bound entity, updating the status of some items (and not entire item), on demand fetching of data from third party service, etc.
While creating function imports via the SEGW (project builder) transaction, we don’t have a provision of marking parameters as optional. We may have use cases where same function import behaves differently with different parameters which are all not mandatory. To achieve this, we can override the definition of function import in the DEFINE method of the respective model provider extension class.
Sample implementation is demonstrated below.
Figure 1: oData Service with Function Import
Sample code is
METHOD define.
*----------------------------------------------------------------------
* Title:
* A. Also make the function import(Fetch User Configurations) parameters as optional
*---------------------------------------------------------------------
* A. Also make the function import(Fetch User Configurations) parameters as optional
*----------------------------------------------------------------------
" Data Declarations
DATA:
lo_entity_type TYPE REF TO /iwbep/if_mgw_odata_entity_typ,
lo_property TYPE REF TO /iwbep/if_mgw_odata_property,
lo_action TYPE REF TO /iwbep/if_mgw_odata_action.
" Start of Code
CALL METHOD super->define( ).
" Mark the parameters as optional in case of 'FetchUserConfigurations' function import
lo_action = model->get_action( iv_action_name = ‘FetchUserConfigurations’ ).
lo_property = lo_action->get_input_parameter( iv_name = ‘EmployeeUser’ ).
lo_property->set_nullable( iv_nullable = abap_true ).
" End of Code
ENDMETHOD.
Code Snippet 1: Optional parameters implementation
Note: 1. Pagination cannot be implemented in any Function Import as the IO_TECH_CONTEXT parameter for function import and GET_ENTITYSET methods inherits from different classes even though their name is same.
2. Also note that above properties are in Camel case and not all upper case.
Please share feedback and questions.
Nice one!
Thanks Suriya
Very helpful!
Thank you!
Welcome Marat.
Well, I had the same issue, but the solution was quite easy and works without any coding.
There's a hidden field in the ALV-View of the parameter List. Just Change the layout, Show the column 'Nullable' and tick it as you want 🙂
Thanks Gerhard.Never tried that.
Hi Gerhard,
Thanks for your option. However, in my function import, I have no idea why it is not editable.
Best Regards,
Steven
Thanks Ankit and Gerhard for your explanation.
Despite the implementation of your suggested solutions (both, in method DEFINE and Nullable in SEGW) I get the error when I try to transfer an "Edm.DateTime" with the value NULL.
I can only transfer dummy values to the back end. But that's not a very nice solution in my opinion.
Do you have any suggestion how to resolve this?
Hi Daniel,
You should format the Null value based on below specification.
https://www.odata.org/documentation/odata-version-2-0/overview/
It should be like below-
datetime'0000-00-00T00:00'
Please check and revert.
Best Regards.
Hi Ankit,
I have got a similar issue to Daniel, but the solution you proposed is not working .
if the parameter is settled as type DateTime, than the standard GW is not accepting null nor datetime'0000-00-00T00:00:00' ...
I've applied even the MDP_EXT change on the Define method, but this is not called at all once you use a DateTime parameter in the Function Import.
Maybe this is a bug of the standard GW.
cheers
Filippo