动态创建内表示例
REPORT zdyn_test.
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_wa>,
<dyn_field>.
DATA: dy_table TYPE REF TO data,
it_structure TYPE lvc_t_fcat,
wa_structure TYPE lvc_s_fcat.
START-OF-SELECTION.
PERFORM create. " 定义内表的结构
PERFORM create_dynamic_table. " 按照定义的内表结构,产生一个内表
PERFORM write_data_to_dyntable. " 向动态内表中写数
PERFORM output_dyntable_data. " 从动态内表中取数,并写到屏幕
*&---------------------------------------------------------------------*
*& Form create_structure
*&---------------------------------------------------------------------*
FORM create_structure.
wa_structure-fieldname = 'COL1'. " 第一列列名
wa_structure-col_pos = 1. " 表示第一列 --- 可心省略,默认情况下,第一行对应到生产内表的第一列,如果指定,则按指定的列顺序生成内表
wa_structure-inttype = 'MENG13'. " 数据类型
wa_structure-intlen = 6. " 长度
APPEND wa_structure TO it_structure.
wa_structure-fieldname = 'COL2'. " 第二列列名
wa_structure-col_pos = 2. " 表示第二列--- 可心省略,默认情况下,第一行对应到生产内表的第一列,如果指定,则按指定的列顺序生成内表
wa_structure-inttype = 'C'. " 数据类型
wa_structure-intlen = 6. " 长度
APPEND wa_structure TO it_structure.
wa_structure-fieldname = 'COL3'. " 第三列名
wa_structure-col_pos = 3. " 表示第三列 --- 可心省略,默认情况下,第一行对应到生产内表的第一列,如果指定,则按指定的列顺序生成内表
wa_structure-inttype = 'C'. " 数据类型
wa_structure-intlen = 6. " 长度
APPEND wa_structure TO it_structure.
ENDFORM. " create_structure
*&---------------------------------------------------------------------*
*& Form create
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM create.
DO 4 TIMES.
DATA: col(15) TYPE c, c TYPE c.
c = sy-tabix.
CONCATENATE sy-datum c INTO col.
wa_structure-fieldname = col. " 第一列列名
* wa_structure-col_pos = 1. " 表示第一列 --- 可心省略,默认情况下,第一行对应到生产内表的第一列,如果指定,则按指定的列顺序生成内表
wa_structure-inttype = 'C'. " 数据类型
wa_structure-intlen = 15. " 长度
APPEND wa_structure TO it_structure.
ENDDO.
delete it_structure INDEX 1.
ENDFORM. "create
*&---------------------------------------------------------------------*
*& Form create_dynamic_table
*&---------------------------------------------------------------------*
FORM create_dynamic_table .
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_structure
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO <dyn_table>. " 用表类型指针 <dyn_table> 指向 数据对象的内容.
ENDFORM. " create_dynamic_table
*&---------------------------------------------------------------------*
*& Form write_data_to_dyntable
*&---------------------------------------------------------------------*
FORM write_data_to_dyntable .
DATA:wa_new_line TYPE REF TO data.
DATA:i TYPE n.
DATA:j TYPE n.
CREATE DATA wa_new_line LIKE LINE OF <dyn_table>. " 建立一个与动态内表结构相同的数据对象,且数据对象为是一个结构
ASSIGN wa_new_line->* TO <dyn_wa>.
" 用<dyn_wa>指针指向该结构
DO 3 TIMES.
i = i + 1.
CLEAR j.
LOOP AT it_structure INTO wa_structure.
j = j + 1.
ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>. " 用指针 <dyn_field>指向工作区<dyn_wa>中的一个字段,字段名为wa_structure-fieldname.
CONCATENATE i j INTO <dyn_field>. " 给指针指向的字段赋值
ENDLOOP.
APPEND <dyn_wa> TO <dyn_table>.
ENDDO.
ENDFORM. " write_data_to_dyntable
*&---------------------------------------------------------------------*
*& Form output_dyntable_data
*&---------------------------------------------------------------------*
FORM output_dyntable_data .
LOOP AT it_structure INTO wa_structure.
WRITE: wa_structure-fieldname(15).
ENDLOOP.
LOOP AT <dyn_table> INTO <dyn_wa>.
WRITE: / .
LOOP AT it_structure INTO wa_structure.
ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>. " 用指针 <dyn_field>指向工作区<dyn_wa>中的一个字段,字段名为wa_structure-fieldname.
WRITE: <dyn_field>.
ENDLOOP.
ENDLOOP.
CLEAR nast.
nast-kappl = 'EF'.
nast-objky = '4014000182'. "采购单号
nast-kschl = 'XX01'.
nast-parnr = 'I02LS'. "Partner number
nast-parvw = 'LS'. "Partner function
PERFORM edi_processing(rsnasted) USING rcode 'X'.
CLEAR nast.
nast-kappl = 'EF'.
nast-objky = '4014000182'.
nast-kschl = 'XX01'.
nast-parnr = 'I02LS'.
nast-parvw = 'LS'.
nast-spras = sy-langu.
nast-erdat = sy-datum.
nast-eruhr = sy-uzeit.
nast-nacha = '6'. "EDI
nast-anzal = 0.
nast-usnam = sy-uname.
PERFORM einzelnachricht(rsnast00) USING rcode.
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_wa>,
<dyn_field>.
DATA: dy_table TYPE REF TO data,
it_structure TYPE lvc_t_fcat,
wa_structure TYPE lvc_s_fcat.
START-OF-SELECTION.
PERFORM create. " 定义内表的结构
PERFORM create_dynamic_table. " 按照定义的内表结构,产生一个内表
PERFORM write_data_to_dyntable. " 向动态内表中写数
PERFORM output_dyntable_data. " 从动态内表中取数,并写到屏幕
*&---------------------------------------------------------------------*
*& Form create_structure
*&---------------------------------------------------------------------*
FORM create_structure.
wa_structure-fieldname = 'COL1'. " 第一列列名
wa_structure-col_pos = 1. " 表示第一列 --- 可心省略,默认情况下,第一行对应到生产内表的第一列,如果指定,则按指定的列顺序生成内表
wa_structure-inttype = 'MENG13'. " 数据类型
wa_structure-intlen = 6. " 长度
APPEND wa_structure TO it_structure.
wa_structure-fieldname = 'COL2'. " 第二列列名
wa_structure-col_pos = 2. " 表示第二列--- 可心省略,默认情况下,第一行对应到生产内表的第一列,如果指定,则按指定的列顺序生成内表
wa_structure-inttype = 'C'. " 数据类型
wa_structure-intlen = 6. " 长度
APPEND wa_structure TO it_structure.
wa_structure-fieldname = 'COL3'. " 第三列名
wa_structure-col_pos = 3. " 表示第三列 --- 可心省略,默认情况下,第一行对应到生产内表的第一列,如果指定,则按指定的列顺序生成内表
wa_structure-inttype = 'C'. " 数据类型
wa_structure-intlen = 6. " 长度
APPEND wa_structure TO it_structure.
ENDFORM. " create_structure
*&---------------------------------------------------------------------*
*& Form create
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM create.
DO 4 TIMES.
DATA: col(15) TYPE c, c TYPE c.
c = sy-tabix.
CONCATENATE sy-datum c INTO col.
wa_structure-fieldname = col. " 第一列列名
* wa_structure-col_pos = 1. " 表示第一列 --- 可心省略,默认情况下,第一行对应到生产内表的第一列,如果指定,则按指定的列顺序生成内表
wa_structure-inttype = 'C'. " 数据类型
wa_structure-intlen = 15. " 长度
APPEND wa_structure TO it_structure.
ENDDO.
delete it_structure INDEX 1.
ENDFORM. "create
*&---------------------------------------------------------------------*
*& Form create_dynamic_table
*&---------------------------------------------------------------------*
FORM create_dynamic_table .
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_structure
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO <dyn_table>. " 用表类型指针 <dyn_table> 指向 数据对象的内容.
ENDFORM. " create_dynamic_table
*&---------------------------------------------------------------------*
*& Form write_data_to_dyntable
*&---------------------------------------------------------------------*
FORM write_data_to_dyntable .
DATA:wa_new_line TYPE REF TO data.
DATA:i TYPE n.
DATA:j TYPE n.
CREATE DATA wa_new_line LIKE LINE OF <dyn_table>. " 建立一个与动态内表结构相同的数据对象,且数据对象为是一个结构
ASSIGN wa_new_line->* TO <dyn_wa>.
" 用<dyn_wa>指针指向该结构
DO 3 TIMES.
i = i + 1.
CLEAR j.
LOOP AT it_structure INTO wa_structure.
j = j + 1.
ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>. " 用指针 <dyn_field>指向工作区<dyn_wa>中的一个字段,字段名为wa_structure-fieldname.
CONCATENATE i j INTO <dyn_field>. " 给指针指向的字段赋值
ENDLOOP.
APPEND <dyn_wa> TO <dyn_table>.
ENDDO.
ENDFORM. " write_data_to_dyntable
*&---------------------------------------------------------------------*
*& Form output_dyntable_data
*&---------------------------------------------------------------------*
FORM output_dyntable_data .
LOOP AT it_structure INTO wa_structure.
WRITE: wa_structure-fieldname(15).
ENDLOOP.
LOOP AT <dyn_table> INTO <dyn_wa>.
WRITE: / .
LOOP AT it_structure INTO wa_structure.
ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>. " 用指针 <dyn_field>指向工作区<dyn_wa>中的一个字段,字段名为wa_structure-fieldname.
WRITE: <dyn_field>.
ENDLOOP.
ENDLOOP.
ENDFORM. " output_dyntable_data
---------------------------实例----------------------------消息方式的IdOC
在系统中,我们经常可以看到EDI方式的输出,与打印输出类似。下面我们基于文章"IDOC实例,Outbound IDOC"的基础上,简单讲解这种方式的IDOC输出。
1,创建一个function:Y_IDOC_PO_SEND.
这是我们的Outbound Funtion Module,用来创建IDOC的内容。同样的,该函数的接口都是标准的。
下面将要介绍的tcode WE41中可以查看系统已有的同类函数,可以参照设置我们的函数接口。然后写入如下代码:
DATA: ls_pohead TYPE ypohead,
ls_poitem TYPE ypoitem,
ls_ekko TYPE ekko,
lt_ekpo TYPE TABLE OF ekpo WITH HEADER LINE.
ls_poitem TYPE ypoitem,
ls_ekko TYPE ekko,
lt_ekpo TYPE TABLE OF ekpo WITH HEADER LINE.
* control_record_out
CLEAR control_record_out.
MOVE control_record_in TO control_record_out.
* control_record_out-mestyp = 'YPO'. "Message Type
* control_record_out-idoctp = 'YPOIDOC'. "IDOC Type
control_record_out-direct = '1'.
control_record_out-serial = sy-datum.
control_record_out-serial+8 = sy-uzeit.
CLEAR control_record_out.
MOVE control_record_in TO control_record_out.
* control_record_out-mestyp = 'YPO'. "Message Type
* control_record_out-idoctp = 'YPOIDOC'. "IDOC Type
control_record_out-direct = '1'.
control_record_out-serial = sy-datum.
control_record_out-serial+8 = sy-uzeit.
* Read from ekko/ekpo
CLEAR ls_ekko.
SELECT SINGLE *
INTO ls_ekko
FROM ekko
WHERE ebeln = object-objky(10) "采购单号
AND loekz = space.
IF sy-subrc <> 0.
RAISE error_message_received.
ENDIF.
CLEAR ls_ekko.
SELECT SINGLE *
INTO ls_ekko
FROM ekko
WHERE ebeln = object-objky(10) "采购单号
AND loekz = space.
IF sy-subrc <> 0.
RAISE error_message_received.
ENDIF.
REFRESH lt_ekpo.
SELECT *
INTO TABLE lt_ekpo
FROM ekpo
WHERE ebeln = ls_ekko-ebeln
AND loekz = space.
SELECT *
INTO TABLE lt_ekpo
FROM ekpo
WHERE ebeln = ls_ekko-ebeln
AND loekz = space.
* INT_EDIDD
CLEAR int_edidd.
int_edidd-docnum = control_record_out-docnum.
int_edidd-segnam = 'YPOHEAD'."结点名称
int_edidd-dtint2 = 0.
CLEAR ls_pohead.
ls_pohead-ebeln = ls_ekko-ebeln.
ls_pohead-bukrs = ls_ekko-bukrs.
ls_pohead-bedat = ls_ekko-bedat.
int_edidd-sdata = ls_pohead. "结点内容
APPEND int_edidd.
CLEAR int_edidd.
int_edidd-docnum = control_record_out-docnum.
int_edidd-segnam = 'YPOHEAD'."结点名称
int_edidd-dtint2 = 0.
CLEAR ls_pohead.
ls_pohead-ebeln = ls_ekko-ebeln.
ls_pohead-bukrs = ls_ekko-bukrs.
ls_pohead-bedat = ls_ekko-bedat.
int_edidd-sdata = ls_pohead. "结点内容
APPEND int_edidd.
LOOP AT lt_ekpo.
CLEAR int_edidd.
int_edidd-docnum = control_record_out-docnum.
int_edidd-segnam = 'YPOITEM'.
int_edidd-dtint2 = 0.
CLEAR ls_poitem.
ls_poitem-ebeln = lt_ekpo-ebeln.
ls_poitem-ebelp = lt_ekpo-ebelp.
ls_poitem-matnr = lt_ekpo-matnr.
ls_poitem-menge = lt_ekpo-menge.
ls_poitem-meins = lt_ekpo-meins.
int_edidd-sdata = ls_poitem.
APPEND int_edidd.
ENDLOOP.
CLEAR int_edidd.
int_edidd-docnum = control_record_out-docnum.
int_edidd-segnam = 'YPOITEM'.
int_edidd-dtint2 = 0.
CLEAR ls_poitem.
ls_poitem-ebeln = lt_ekpo-ebeln.
ls_poitem-ebelp = lt_ekpo-ebelp.
ls_poitem-matnr = lt_ekpo-matnr.
ls_poitem-menge = lt_ekpo-menge.
ls_poitem-meins = lt_ekpo-meins.
int_edidd-sdata = ls_poitem.
APPEND int_edidd.
ENDLOOP.
2,WE41, 创建Outbound Process Code
进入编辑状态后,选择“添加新条目”按钮,创建YPOSEND,对应于新建的Y_IDOC_PO_SEND。
选中下面的Process with ALE Service,代表创建Outbound IDOC后自动发送给接收方。
3,NACE/NACO,创建我们的输出类型
创建一个EDI类型的输出,Program填写RSNASTED,Form Routine填写EDI_PROCESSING。
当然我们也可以在这里找一个已有的EDI输出类型做测试,比如Application EF下的XX01。
4,WE20设置Partner Profile.
前两篇文章中,我们并没有要求填写Partner function,不过这次必须填写了。
Partner function是从属于partner type下面的,这个逻辑关系在表TPAR中体现。
假设我们选中partner type=LS,Partner function=LS。其维护与之前的类似,点击加号添加一个Outbound Parameter,步骤与之前类似,不过必须记得在Message Control中添加一行,Application=EF,Message Type=XX01,Process code为YPOSEND。这里EF和XX01与上面NACO中的定义对应。
5,发送IDOC
事实上,我们已经定义完毕了,可以维护某采购订单的输出类型来发送IDOC了。
为了大家能更好的理解,我们来模仿这个输出过程。创建新程序,写入:
TABLES: nast.
DATA: rcode TYPE i.
DATA: rcode TYPE i.
CLEAR nast.
nast-kappl = 'EF'.
nast-objky = '4014000182'. "采购单号
nast-kschl = 'XX01'.
nast-parnr = 'I02LS'. "Partner number
nast-parvw = 'LS'. "Partner function
PERFORM edi_processing(rsnasted) USING rcode 'X'.
IF rcode IS INITIAL.
COMMIT WORK. "发送IDOC到接收方
COMMIT WORK. "发送IDOC到接收方
ENDIF.
上面的方法并不会在NAST留下输出记录。如果您希望能在NAST中留下一条记录,则应该这样:
TABLES: nast.
DATA: rcode TYPE i.
DATA: rcode TYPE i.
CLEAR nast.
nast-kappl = 'EF'.
nast-objky = '4014000182'.
nast-kschl = 'XX01'.
nast-parnr = 'I02LS'.
nast-parvw = 'LS'.
nast-spras = sy-langu.
nast-erdat = sy-datum.
nast-eruhr = sy-uzeit.
nast-nacha = '6'. "EDI
nast-anzal = 0.
nast-usnam = sy-uname.
PERFORM einzelnachricht(rsnast00) USING rcode.
IF rcode IS INITIAL.
COMMIT WORK. "发送IDOC到接收方
COMMIT WORK. "发送IDOC到接收方
ENDIF.
下面这种方式同样可用于触发其他方式的输出类型,比如打印输出。