实战 Deep Insert (SAP OData Service 实战系列)

实战 Deep Insert (SAP OData Service 实战系列)

在实际开发中我们经常会遇到需要保存多层实体结构到后台系统的场景,比如SAP各种凭证都是抬头和行项目一起保存,我今天就介绍一下创建深度插入OData实体的详细步骤。

今天的例子是创建客户发票凭证,需要同时保存凭证抬头和行项目,SAP提供的标准BAPI 需要同时输入凭证的行项目和抬头信息。

 CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
        EXPORTING
          documentheader    = documentheader
        IMPORTING
          obj_key           = obj_key
        TABLES
          accountgl         = accountgl
          accountreceivable = accountreceivable
          accounttax        = accounttax
          currencyamount    = currencyamount
          return            return.
而我们的OData的设计是这样的:

实战 Deep Insert (SAP OData Service 实战系列)

所以我们就需要使用SAP OData Service提供的Deep Insert方法,下面是相关的详细步骤。

1.创建DocumentHeader和DocumentItem的关联

在事务码SEGW中创建关联,如下图:

 实战 Deep Insert (SAP OData Service 实战系列)

 实战 Deep Insert (SAP OData Service 实战系列)

 点击实战 Deep Insert (SAP OData Service 实战系列)创建运行时对象。

这时候系统自动创建了凭证抬头和行项目之间的关联,我们为了验证系统正确的生成了关联,我们在事务码:/IWFND/GW_CLIENT中进行验证,如下图我们在OData Service的URL中加入$metadata,比如:/sap/opu/odata/BAO/FI_INVCUS_SRV/$metadata

实战 Deep Insert (SAP OData Service 实战系列)

2.重新定义方法/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_DEEP_ENTITY

首先我们需要定义deep entity的Type, 代码如下:

    DATA BEGIN OF ls_invoice_data.
        INCLUDE  TYPE /bao/cl_fi_invcus_mpc_ext=>ts_documentheader.
       DATA: documentitemset TYPE /bao/cl_fi_invcus_mpc_ext=>tt_documentitem,
         END OF ls_invoice_data.

然后我们通过以下语句来读取前台传来的entity信息:

io_data_provider->read_entry_dataIMPORTING es_data = ls_invoice_data ).

这里需要注意在定义deep entity的时候, documentitemset的命名一定要是 line item的entityset的名字,否不能生成正确的deep entity。

 3. 关于 Create Deep entity的测试

我们当然可以使用前台的UI5代码进行测试,但是在集成测试之前使用 SAP Gateway Client进行测试。首先我们通过get expand获得deep entity的信息,然后点击 Use as Request。

 实战 Deep Insert (SAP OData Service 实战系列)

实战 Deep Insert (SAP OData Service 实战系列)

更多精彩,请关注公众号:环宇的后花园 

实战 Deep Insert (SAP OData Service 实战系列)