最近群里的兄弟对IDOC的学习热情高涨,所以我在这里做个最简单的IDOC例子,供大家分享。
简而言之,IDOC是类似XML的一种SAP系统与其他系统的一种集成工具。
假设I04和I02是同一个集团下两个不同子公司的SAP系统,I04需要将其采购订单信息及时发送给I02。下面简单介绍IDOC的设置步骤,为了避免混淆,我的命名都比较特别。
1,设置IDOC Type.
(1) WE31,创建segment,类似于创建XML的结点及结点属性。
这里先输入YPOHEAD,点击创建,在接下来的屏幕中,录入EBELN, BUKRS, BEDAT等字段及他们对应的data element;接着创建YPOITEM,输入EBELN, EBELP, MATNR, MENGE, MEINS等字段及他们对应的data element。
保存后用SE12查看你将发现,系统自动添加了YPOHEAD和YPOITEM两个结构,每个字段都成了CHAR类型,长度就是WE31中的EXPORT LENG。
(2) WE30, 创建IDOC Type,定义结点间的相互逻辑关系.
先输入YPOIDOC,然后点击创建,紧跟着点击create new进入。在主界面中,先点击创建按钮,将YPOHEAD添加,设置Mandatory seg打勾,min = 1, max = 1,代表我们每个IDOC仅包含一张采购订单。然后在YPOHEAD下添加YPOITEM,同样的Mandatory seg打勾,min = 1, max = 99999.
2,创建Message Type.
(1) WE81,创建Message Type.
先切换到编辑状态,然后点击New Entries,输入YPO即可。
(2) WE82,,关联Message Type和IDOC Type.
3,创建到I02的端口.
首先确保系统内已经有到I02的R/3 Connection(SM59),如果没有则先创建一个名为I02001。
接着WE21创建Port,类型TRANSACTIONAL RFC,名为I02PORT,RFC destination则填写I02001。
4,SALE,创建Logical System.
SALE, Sending and Receiving Systems,Logical Systems, define logical systems,新增一个logical system,名为I02LS,这将作为下一步的Partner。
Define logical systems下方,有Assign Client to logical system,这里可针对本系统(I04)定义logical system,作为发送IDOC时的发送方标识,我对Client 001的定义是I04LS.
5,WE20,定义我们的Partner profile.
在PARTNER TYPE为logical system下,创建一个patner no为I02LS的partner,type填写LS。
下面的Permitted agent,填写US(User),Agent为你自己的用户名或某basis人员用户名。
然后,点击outbound下方的加号,创建一个outbound parameter。Message Type为YPO,receiver port为I02PORT,output mode选择transfer idoc immed.,Basic Type填写YPOIDOC,保存即可。
6,编程发送IDOC
设置了这么多,真累啊,总算可以写程序了,是不是很开心呢?
程序的思路就是,把每个IDOC结点按字符串形式逐个添加,而字符串的添加次序自然也体现了IDOC结点间的逻辑关系。代码如下,
DATA: ls_pohead TYPE ypohead,
ls_poitem TYPE ypoitem,
ls_edidc TYPE edidc,
lt_edidc TYPE TABLE OF edidc,
lt_edidd TYPE TABLE OF edidd WITH HEADER LINE.
ls_poitem TYPE ypoitem,
ls_edidc TYPE edidc,
lt_edidc TYPE TABLE OF edidc,
lt_edidd TYPE TABLE OF edidd WITH HEADER LINE.
CLEAR ls_edidc.
*系统根据下面4行即可与WE20设置关联起来
ls_edidc-mestyp = 'YPO'. "Message Type
ls_edidc-idoctp = 'YPOIDOC'. "IDOC Type
ls_edidc-mestyp = 'YPO'. "Message Type
ls_edidc-idoctp = 'YPOIDOC'. "IDOC Type
ls_edidc-rcvprn = 'I02LS'. "Partner Number of Recipient
ls_edidc-rcvprt = 'LS'. "Partner Type of Receiver
ls_edidc-rcvprt = 'LS'. "Partner Type of Receiver
*添加IDOC结点
CLEAR lt_edidd.
lt_edidd-segnam = 'YPOHEAD'."结点名称
lt_edidd-dtint2 = 0.
CLEAR ls_pohead.
ls_pohead-ebeln = '4001122334'.
ls_pohead-bukrs = '0400'.
ls_pohead-bedat = '20090630'.
lt_edidd-sdata = ls_pohead. "结点内容
APPEND lt_edidd.
lt_edidd-segnam = 'YPOHEAD'."结点名称
lt_edidd-dtint2 = 0.
CLEAR ls_pohead.
ls_pohead-ebeln = '4001122334'.
ls_pohead-bukrs = '0400'.
ls_pohead-bedat = '20090630'.
lt_edidd-sdata = ls_pohead. "结点内容
APPEND lt_edidd.
CLEAR lt_edidd.
lt_edidd-segnam = 'YPOITEM'.
lt_edidd-dtint2 = 0.
CLEAR ls_poitem.
ls_poitem-ebeln = '4001122334'.
ls_poitem-ebelp = '0001'.
ls_poitem-matnr = '000000000000004527'.
ls_poitem-menge = '3'.
ls_poitem-meins = 'ST'.
lt_edidd-sdata = ls_poitem.
APPEND lt_edidd.
lt_edidd-segnam = 'YPOITEM'.
lt_edidd-dtint2 = 0.
CLEAR ls_poitem.
ls_poitem-ebeln = '4001122334'.
ls_poitem-ebelp = '0001'.
ls_poitem-matnr = '000000000000004527'.
ls_poitem-menge = '3'.
ls_poitem-meins = 'ST'.
lt_edidd-sdata = ls_poitem.
APPEND lt_edidd.
CLEAR lt_edidd.
lt_edidd-segnam = 'YPOITEM'.
lt_edidd-dtint2 = 0.
CLEAR ls_poitem.
ls_poitem-ebeln = '4001122334'.
ls_poitem-ebelp = '0002'.
ls_poitem-matnr = '000000000000009289'.
ls_poitem-menge = '5'.
ls_poitem-meins = 'M'.
lt_edidd-sdata = ls_poitem.
APPEND lt_edidd.
lt_edidd-segnam = 'YPOITEM'.
lt_edidd-dtint2 = 0.
CLEAR ls_poitem.
ls_poitem-ebeln = '4001122334'.
ls_poitem-ebelp = '0002'.
ls_poitem-matnr = '000000000000009289'.
ls_poitem-menge = '5'.
ls_poitem-meins = 'M'.
lt_edidd-sdata = ls_poitem.
APPEND lt_edidd.
CALL FUNCTION 'MASTER_IDOC_DISTRIBUTE'
EXPORTING
master_idoc_control = ls_edidc "Export
TABLES
communication_idoc_control = lt_edidc "Import
master_idoc_data = lt_edidd "Export
EXCEPTIONS
error_in_idoc_control = 1
error_writing_idoc_status = 2
error_in_idoc_data = 3
sending_logical_system_unknown = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
COMMIT WORK.
WRITE: 'Idoc sent:'.
LOOP AT lt_edidc INTO ls_edidc.
NEW-LINE.
WRITE: 'Idoc number is', ls_edidc-docnum,
'; receiver partner is', ls_edidc-rcvprn,
'; sender partner',ls_edidc-sndprn.
ENDLOOP.
ENDIF.
EXPORTING
master_idoc_control = ls_edidc "Export
TABLES
communication_idoc_control = lt_edidc "Import
master_idoc_data = lt_edidd "Export
EXCEPTIONS
error_in_idoc_control = 1
error_writing_idoc_status = 2
error_in_idoc_data = 3
sending_logical_system_unknown = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
COMMIT WORK.
WRITE: 'Idoc sent:'.
LOOP AT lt_edidc INTO ls_edidc.
NEW-LINE.
WRITE: 'Idoc number is', ls_edidc-docnum,
'; receiver partner is', ls_edidc-rcvprn,
'; sender partner',ls_edidc-sndprn.
ENDLOOP.
ENDIF.
7,查看IDOC.
WE05可以查看我们刚刚发送的IDOC, BD87还可对IDOC进行一些处理。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/16794144/viewspace-676268/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/16794144/viewspace-676268/