‎2021 Sep 02 1:44 PM
Hi,
I have a requirement that, I need to get a field value in CDS view using a Function module.
What is the best approach for this other than using ODATA service?
‎2021 Sep 02 2:04 PM
If I've understood you correctly, you're looking for a virtual element in you CDS view. There you're specifying an ABAP class that then provides the value. And in this class you can for example call your function module.
E. g.:
@ObjectModel.virtualElement: true
@ObjectModel.virtualElementCalculatedBy: 'ZCL_CALC_CLASS'
cast( '' as abap.char(255)) as function_value
‎2021 Sep 02 1:44 PM
Welcome to the SAP Community! We wanted to give you the opportunity to take the tutorial to get started in SAP Community, as it provides tips for preparing questions that draw responses from our members.
Additionally, by adding a picture to your profile you encourage readers to respond to your question. Learn more about your profile using Profile Tutorial
Thank you!
‎2021 Sep 02 2:04 PM
If I've understood you correctly, you're looking for a virtual element in you CDS view. There you're specifying an ABAP class that then provides the value. And in this class you can for example call your function module.
E. g.:
@ObjectModel.virtualElement: true
@ObjectModel.virtualElementCalculatedBy: 'ZCL_CALC_CLASS'
cast( '' as abap.char(255)) as function_value
‎2021 Sep 02 3:52 PM
Hi Frank,
Thanks for you are response.
I tried adding the virtual element but the particular field value is always being blank.
Here is my code can you please check it once.
define view ZCDS_EXAMPLE as select from table1 as t1
{
key t1.field1 as f1,
t1.field2 as f2,
t1.feild3 as f3,
t1.field4 as f4,
t1.feild5 as f5 ,
t1.field6 as f6,
t1.field7 as f7,
@ObjectModel.virtualElement: true
@ObjectModel.virtualElementCalculatedBy: 'ZCL_CLASS1'
cast( '' as abap.char(255)) as final_date
}
And in class the the logic I want to implement is to fetch the date using 'NET_DUE_DATE_GET' function module.
and My ABAP class is..
class ZCL_CLAS1 definition
public
final
create public .
public section.
interfaces : if_sadl_exit_calc_element_read.
protected section.
private section.
ENDCLASS.
CLASS ZCL_CLASS1 IMPLEMENTATION.
method if_sadl_exit_calc_element_read~calculate.
data lt_original_data type table of ZCDS_EXAMPLE with default key.
lt_original_data = corresponding #( it_original_data ).
loop at lt_original_data assigning field-symbol(<ls_data>).
call function 'NET_DUE_DATE_GET'
EXPORTING
I_ZFBDT = <ls_data>-f2
I_ZBD1T = <ls_data>-f3
I_ZBD2T = <ls_data>-f4
I_ZBD3T = <ls_data>-f5
I_SHKZG = <ls_data>-f6
I_REBZG = <ls_data>-f7
IMPORTING
E_FAEDT = <ls_data>-final_date.
IF sy-subrc = 0.
APPEND <ls_data> TO lt_original_data.
ENDIF.
endloop.
ct_calculated_data = lt_original_data.
endmethod.
method if_sadl_exit_calc_element_read~GET_CALCULATION_INFO.
endmethod..
ENDCLASS.
Can you please tell me where the problem is?
‎2021 Sep 02 3:53 PM
HI frank,
Thanks for you are response.
I tried using the virtual element concept but that particular field value is always being blank.
Here is the same code template I followed Can you please check it once let me know where the problem is?
@AbapCatalog.sqlViewName: 'CDS1'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@Metadata.allowExtensions: true
@EndUserText.label: 'final cds'
define view ZCDS_EXAMPLE as select from table1 as t1
{
key t1.field1 as f1,
t1.field2 as f2,
t1.field3 as f3,
t1.field4 as f4,
t1.field5 as f5,
t1.field6 as f6,
t1.field7 as f7,
@ObjectModel.virtualElement: true
@ObjectModel.virtualElementCalculatedBy: 'ZCL_CLASS1'
cast( '' as abap.char(255)) as final_date
}
Here the final_date value need to be fetch from the function module 'NET_DUE_DATE_GET'.
So I used this FM in calculate method of ABAP class as follow.
class ZCL_CLASS1 definition
public
final
create public .
public section.
interfaces : if_sadl_exit_calc_element_read.
protected section.
private section.
ENDCLASS.
CLASS ZCL_CLASS1 IMPLEMENTATION.
method if_sadl_exit_calc_element_read~calculate.
data lt_original_data type table of ZCDS_EXAMPLE with default key.
lt_original_data = corresponding #( it_original_data ).
loop at lt_original_data assigning field-symbol(<ls_data>).
call function 'NET_DUE_DATE_GET'
EXPORTING
I_ZFBDT = <ls_data>-f2
I_ZBD1T = <ls_data>-f3
I_ZBD2T = <ls_data>-f4
I_ZBD3T = <ls_data>-f5
I_SHKZG = <ls_data>-f6
I_REBZG = <ls_data>-f7
IMPORTING
E_FAEDT = <ls_data>-final_date.
IF sy-subrc = 0.
APPEND <ls_data> TO lt_original_data.
ENDIF.
endloop.
ct_calculated_data = corresponding #( lt_original_data ).
endmethod.
method if_sadl_exit_calc_element_read~GET_CALCULATION_INFO.
endmethod.
ENDCLASS.
But I'm getting blank values as output in final_date.
‎2022 Oct 04 12:19 AM
Virtual fields cannot be viewed via Eclipse only via SADL framework, i.e. through an ODATA service, which may be the reason you are not seeing the field.
‎2023 Mar 09 10:26 AM
Any idea how the same can achieved in Netweaver 7.5? I am not able to use virtual elements there. I think it was introduced later on. I need to call a function module and change the value of the field.
‎2023 Mar 10 9:21 AM
As mentioned by Alexander, virtual elements could be one solution. Additionally you could fill the element on DPC_EXT level, if you have built the service using SEGW: https://help.sap.com/docs/SAP_NETWEAVER_750/cc0c305d2fab47bd808adcad3ca7ee9d/ed2ed4e2cbc54ffdba3aead...