Skip to Content
Author's profile photo Ankit Maskara

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.

Assigned Tags

      10 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Suriya Pandiyan
      Suriya Pandiyan

      Nice one!

      Author's profile photo Ankit Maskara
      Ankit Maskara
      Blog Post Author

      Thanks Suriya

      Author's profile photo Marat Akhmetzyanov
      Marat Akhmetzyanov

      Very helpful!

      Thank you!

      Author's profile photo Ankit Maskara
      Ankit Maskara
      Blog Post Author

      Welcome Marat.

      Author's profile photo Gerhard Rink
      Gerhard Rink

      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 🙂

       

       

       

      Author's profile photo Ankit Maskara
      Ankit Maskara
      Blog Post Author

      Thanks Gerhard.Never tried that.

      Author's profile photo Steven Liu
      Steven Liu

      Hi Gerhard,

      Thanks for your option. However, in my function import, I have no idea why it is not editable.

      Best Regards,

      Steven

       

       

      Author's profile photo Daniel Möller
      Daniel Möller

      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.

      Log-dbg.js:415 2020-03-17 10:50:12.914169 The following problem occurred: HTTP request failed400,Bad Request,{"error":{"code":"005056A509B11EE1B9A8FEC11C22378E","message":{"lang":"de","value":"Ungültiges Schlüsselprädikat"},"innererror":{"transactionid":"442BD4043D2C00C0E005E612B7F99456","timestamp":"20200317095014.3772460","Error_Resolution":{"SAP_Transaction":"For backend administrators: run transaction /IWFND/ERROR_LOG on SAP Gateway hub system and search for entries with the timestamp above for more details","SAP_Note":"See SAP Note 1797736 for error analysis (https://service.sap.com/sap/support/notes/1797736)"}}}} -  

      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?

      Author's profile photo Ankit Maskara
      Ankit Maskara
      Blog Post Author

      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.

      Author's profile photo FILIPPO AMBROSINI
      FILIPPO AMBROSINI

      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