SAP ABAP 调用其他程序如 ALV 报表程序的源码

1:可以一个创建接口,方法见博客
https://blog.csdn.net/qq_41757790/article/details/126301085?spm=1001.2014.3001.5501

2:定义一个与报表输出表字段相同的结构体及内表。
若报表输入框为多项选择,输入函数可以选择表形式。
PS:传入参数的类型尽量不要过于复杂,可能会不便于其他系统的开发人员调用。

3:示例:
创建一个接口读取报表程序查询的值返回给外部系统(通过这个方法,传递与报表同样的数据不用再复写一遍逻辑)。

报表程序界面如下:
SAP ABAP 调用其他程序如 ALV 报表程序的源码_第1张图片

SAP ABAP 调用其他程序如 ALV 报表程序的源码_第2张图片
接口程序:
SAP ABAP 调用其他程序如 ALV 报表程序的源码_第3张图片
SAP ABAP 调用其他程序如 ALV 报表程序的源码_第4张图片

方法 1 ——通过 submit 的形式调用 ALV 报表程序,源码如下:

FUNCTION ZFM_GET_ZPP005.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(IS_IN) TYPE  ZPP005IN
*"  EXPORTING
*"     VALUE(LS_RETURN) TYPE  ZBCS_RETURN
*"  TABLES
*"      IT_IN STRUCTURE  ZPP05_IN
*"      IT_OUT STRUCTURE  ZPP05_OUT
*"----------------------------------------------------------------------
"定义查询选项参数
ranges:r_werks for t024f-werks,
       r_aufnr FOR afko-aufnr,
       r_fevor FOR t024f-fevor,
       r_matnr FOR afpo-matnr,
       r_matnr2 FOR resb-matnr,  
       r_ftrmi FOR afko-ftrmi.
       
DATA: ls_r_werks LIKE LINE OF r_werks,
      ls_r_aufnr LIKE LINE OF r_aufnr,
      ls_r_fevor LIKE LINE OF r_fevor,
      ls_r_matnr LIKE LINE OF r_matnr,
      ls_r_matnr2 LIKE LINE OF r_matnr2,
      ls_r_ftrmi LIKE LINE OF r_ftrmi.

DATA:ls_in type zpp05_in,
     ls_out TYPE zpp05_out.

DATA: LS_DATA TYPE REF TO DATA.
FIELD-SYMBOLS: <lt_data> TYPE table,
                 <l_data>  TYPE data.

"定义与报表相同的输出内表结构"

TYPES: BEGIN OF ty_alv,
         sel             TYPE char1,
         aufnr           TYPE aufnr, 
         matnr_cp        TYPE matnr, 
         maktx_cp        TYPE maktx, 
         werks           TYPE werks_d,
         lgort           TYPE lgort_d, 
       END OF  ty_alv.

DATA:GT_OUTTAB TYPE TABLE OF ty_alv,
      GS_OUTTAB LIKE LINE OF GT_OUTTAB,
      it_alv TYPE TABLE OF ty_alv WITH HEADER LINE.
 "通过循环传入参数表将查询内容添加到范围里"     
LOOP AT it_in into ls_in.
"转换pn"
     CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
      EXPORTING
        input        = ls_in-matnr
      IMPORTING
        output       = ls_in-matnr.
      IF ls_in-matnr IS NOT INITIAL.
       CLEAR:ls_r_matnr.
       ls_r_matnr-option = 'EQ'.
       ls_r_matnr-sign = 'I'.
       ls_r_matnr-low = ls_in-matnr.
       APPEND ls_r_matnr TO r_matnr.
     ENDIF.


     IF ls_in-werks IS NOT INITIAL.
       CLEAR:ls_r_werks.
       ls_r_werks-option = 'EQ'.
       ls_r_werks-sign = 'I'.
       ls_r_werks-low = ls_in-werks.
       APPEND ls_r_werks TO r_werks.
     ENDIF.
ENDLOOP.     

"设置调用 alv 不显示屏幕"
  cl_salv_bs_runtime_info=>set(
        display = ''
        metadata = ''
        data    = 'X' ).
 
 "提交执行报表程序"
  SUBMIT ZPPR005
     WITH s_matnr IN r_matnr
     WITH s_werks IN r_werks
     WITH s_aufnr IN r_aufnr
     WITH s_fevor IN r_fevor
     WITH s_matnr2 IN r_matnr2
     WITH s_ftrmi IN r_ftrmi
     WITH R1 = is_in-STATUS_ADD
     WITH R2 = is_in-STATUS_ISSUED
     WITH R3 = is_in-STATUS_OVER
     WITH R4 = is_in-STATUS_DEL
   AND RETURN  .
  TRY .
      cl_salv_bs_runtime_info=>get_data_ref(
      IMPORTING r_data = ls_data ).

      ASSIGN ls_data->* TO <lt_data>.
    CATCH cx_salv_bs_sc_runtime_info.
      ls_return-message = '查询无结果' .
      ls_return-type = 'E'.
  ENDTRY.
  cl_salv_bs_runtime_info=>clear_all( ).

"如果查询有值且无异常,将查询出的数据添加到输出内表"
  IF  <lt_data> IS  ASSIGNED.
     LOOP AT <lt_data> ASSIGNING <l_data> .
      CLEAR:ls_out.
       MOVE-CORRESPONDING <l_data> TO ls_out.
       APPEND ls_out TO it_out .
    ENDLOOP.
  ENDIF.
ENDFUNCTION.

你可能感兴趣的:(ABAP,后端)