gpt4 book ai didi

abap - 从 ABAP CDS View 调用类方法?

转载 作者:行者123 更新时间:2023-12-05 01:33:52 26 4
gpt4 key购买 nike

我想从现有 CDS View 中的类中调用一个方法来获取某些数据,这些数据只能通过该方法调用。所以我想从 CDS View 调用一个方法。

最简单的方法是什么?

如果有代码示例,我会非常高兴。

最佳答案

注意:您的 abap 逻辑只会在 CDS View 被处理注释 的东西使用时执行,即作为 Odata 服务公开,但不会在事务 SE16N 或 Eclipse 中的预览中执行。其实有两种方式。第一个选项:

@AbapCatalog.sqlViewName: 'ZV_TEST_ABAP'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'ABAP code in cds'
@OData.publish: true
define view YCDS_WITH_ABAP as select from sflight
{

//sflight
key carrid,
key connid,
key fldate,
seatsocc_f,
@ObjectModel.readOnly: true
@ObjectModel.virtualElement: true
@ObjectModel.virtualElementCalculatedBy: 'ABAP:YCL_CDS_FUNCTION' //ycl_cds_function
cast( '' as abap.char(255)) as text

}where connid = '0017'

你的 abap 类:

CLASS ycl_cds_function DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .



PUBLIC SECTION.
INTERFACES: if_sadl_exit_calc_element_read.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.



CLASS ycl_cds_function IMPLEMENTATION.

METHOD if_sadl_exit_calc_element_read~calculate.

DATA:
lt_calculated_data TYPE STANDARD TABLE OF ZV_TEST_ABAP .

" it_original_data -> data that comes from cds
" lt_calculated_data -> data that you will manipulate
MOVE-CORRESPONDING it_original_data TO lt_calculated_data.

" do your extra logic and append/update your cds view data
LOOP AT lt_calculated_data ASSIGNING FIELD-SYMBOL(<fs_data>).
<fs_data>-text = 'hello from abap!'.
ENDLOOP.

MOVE-CORRESPONDING lt_calculated_data TO ct_calculated_data.

ENDMETHOD.

METHOD if_sadl_exit_calc_element_read~get_calculation_info.

ENDMETHOD.

ENDCLASS.

在eclipse中查看: enter image description here在odata中查看:

  ...,{
"__metadata": {
"id": "lalala')",
"uri": "blablabla')",
"type": "YCDS_WITH_ABAP_CDS.YCDS_WITH_ABAPType"
},
"carrid": "AA",
"connid": "0017",
"fldate": "/Date(1535068800000)/",
"seatsocc_f": 20,
"text": "hello from abap!"
},...

第二个选项:

第二种方法是用您的自定义类更改服务的 DPC 类,它继承了这个标准类并在那些重新定义的方法中执行您的自定义逻辑。就个人而言,我会选择第二个选项。我没有尝试过,但我觉得性能会更好。 :)

enter image description here

更多信息请查看:https://blogs.sap.com/2020/05/11/abap-code-exits-in-cds-views/

关于abap - 从 ABAP CDS View 调用类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64256483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com