SAP IDOC開發(轉)

weilish發表於2012-09-18
建立IDOC:
   第一步:WE31 建立IDOC所包含的欄位.
   第二步:WE30 建立IDOC 把Segment分配給IDOC
第三步:WE81 建立資訊型別
第四步:WE82 把IDOC型別與資訊型別對應.
   第五步:WE57 Assign Message & Idoc Type to a Function Module for Data Process
   第六步:SM59 Define a RFC connection for Idoc transfer
   第七步:WE21 Define a Port ( Assign a RFC destination which created in SM59 )
   第八步:WE41/42 Creat Process Code
   第九步:WE20 Define a Partner Profiles( Also creat a Outbound parameters with Port, or Inbound parameters with Process code )
管理IDOC:
   WE02 顯示IDOC,可以根據時間,IDOC型別查詢IDOC,檢視成功,出錯資訊。
   WE46 IDOC管理(出\入)
   WE60 IDOC型別文件(可以檢視IDOC結構,和每個欄位的描述. 
   WE19 根據IDOC號進行IDOC處理,可以修改IDOC值進行補發動作,處理分為內向和外向。
訊息配置:
   WE20 配置夥伴訊息進和出IDOC型別
   WE21 配置夥伴。
 
官方介紹:
   What is a IDOC?
An IDoc is simply a data container that is used to exchange information between any two processes that can understand the syntax and semantics of the data. IDoc is not a process.
  IDocs are stored in the database.
  In the SAP system, IDocs are stored in database tables.
  IDocs are independent of the sending and receiving systems.
  IDocs are independent of the direction of data exchange.
 
An IDoc Type is SAP's format for data being moved between applications. Essentially, SAP has defined what a sales order, financial statement, invoice, etc will look like electronically. This includes how fields are grouped together (segments), the order and hierarchy of these groupings, and the format of each individual field.
If you're familiar with EDI at all, then an IDoc will look very familiar to you. Nearly everything you're used to is there: from segment name to allowable codes to min/max occurs.
 
It is important to note that an IDoc Type is really just a structure defined on the system and given a name (i.e., ORDERS04). An actual IDoc, however, consists of data, which fits within the defined structure of the IDoc Type. This IDoc is identified by a number rather than by a type.
 
Creation of IDOCs
Transaction code: WE30
 
Steps of Defining Segment
Creating Segment : Tcode - WE31
Creating Message Type : Tcode - we81
Assigning Message type to Idoc type: Tcode - we82
 
Process
The two processes for IDoc are Outbound Process and Inbound Process.
Outbound Process
When the data is sent out from the system, the process is called Outbound Process and the IDoc is known as Outbound IDoc.
Inbound Process
When the data is coming in, the process is called Inbound Process and the IDoc is known as Inbound IDoc.
 
Outbound Process (Sending System) Steps :  
1) Goto Tcode SALE:
Creating the logical system
Click on Sending & Receiving Systems à Select Logical Systems--Here Define the Logical Systems a Click on Execute Button
Go for New Entries
1)  System Name : LOG1 prescription: Sending System
  System Name : LOG2 prescription: Receiving System
 Press Enter & Save it will ask for Request if you want new request create new request or press continue for transferring the objects.
Assigning Client to the Logical System:
Select Assign Client to Logical Systems
 
Client         : Sending System
Logical System : LOG1
and also
Client         : Receiving System
System         : LOG2
Save this Data.

Step 2) For RFC Creation:
Goto Tcode SM59 and  Select R/3 Connects
Click on create Button
RFC Destination Name should be same as partner's logical system name and case sensitive to create the ports automatically while generating the partner profiles
Give the information for required fields:
RFC Destination    : LOG2
Connection type    : 3
Target Host        : sappdc.wipro.com
System No          : 00
Client             : 210
User               : Login user name5 s6 q6 o; y- O: [  g+ }- Z
Password           :
Save this & Test it and Remote Login

Step 3) Goto Tcode BD64:
Click on the change button>Click on the create model view
Short Text: model view
Technical Name: LMOD
Save this & press ok
Select just created model view
Name: "LMOD"
Goto add message type
Model Name  : LMOD
Sender           : LOG1
Receiver         : LOG2
Message type: ZAZHARMESS
Save and press Enter
 
4) Goto Tcode BD82:
Give Model View   : LMOD
Partner system     : LOG2
Execute this by pressing F8
It will gives you sending system port No:  A00000000089 (Like)

5) Goto Tcode BD64:
Select the model view
Goto >Edit >model view > Distribute
Press ok & Press Enter.

Run your Zprogram
REPORT  ZIDOC1 .
SPAN { font-family: "新宋體"; font-size: 10pt; color: #000000; background: #CCE8CF; } .L0S31 { font-style. italic; color: #808080; } .L0S32 { color: #3399FF; } .L0S33 { color: #4DA619; } .L0S52 { color: #0000FF; } DATA: BEGIN OF imara OCCURS 0,
       matnr LIKE mara-matnr,
       mtart LIKE mara-mtart,
      END OF imara.

DATA: wamas_con LIKE edidc,
imas_data LIKE edidd OCCURS WITH HEADER LINE,
icom_con LIKE edidc OCCURS WITH HEADER LINE.

PARAMETERS: e_matnr LIKE mara-matnr,
      e_msgtyp LIKE edidc-mestyp,
      e_rcvprn LIKE edidc-rcvprn.
* retrive app. data from DB
SELECT matnr mtart FROM mara INTO TABLE imara
   WHERE matnr = e_matnr.
*Master idoc control record
wamas_con-rcvprt = 'LS'.
wamas_con-rcvprn = e_rcvprn.
wamas_con-idoctp = 'ZAZHARIDOC'.
wamas_con-mestyp = e_msgtyp.

* master idoc data records
LOOP AT imara.
  imas_data-segnam = 'ZAZHARSEG'.
  MOVE imara TO imas_data-sdata .
  APPEND imas_data.
ENDLOOP.

CALL FUNCTION 'MASTER_IDOC_DISTRIBUTE'
  EXPORTING
    master_idoc_control        = wamas_con
  TABLES
    communication_idoc_control = icom_con
    master_idoc_data           = imas_data.
COMMIT WORK.
 
6) Verifying Transfer of IDOCs Tcode - we05
ALE/IDOC Status Codes (outbound):
01> IDoc Added
30 > IDoc ready for dispatch
29 >Error in ALE service Layer
12 >Dispatch ok
03 > Data passed to port ok.
 
Inbound Process (Receiving System) Steps:
Do the same step as you did in sending system
9 |9 S! c+ a  {
>  Creating IDoc
>  Defining the Segment
>  Creating Message Type
>  Assigning the Message Type
>  Defining the Logical System
>  Assigning the Logical System
>  Creating the Distribution Model

1) Goto Tcode - we57
Assign function module to IDoc type
Module: Function module
Basic type:  
Message type:
Direction: 2 (inbound)

2) Creating Inbound process code - we42
3) Verifying Idoc List Tcode - we05
4) ALE/IDOC Status Codes (Inbound):
50 >IDoc Added
51 >Application Document not posted
64 >IDoc ready to be transferred to application
62 >IDoc passed to application
53 >Application Document posted

----------------------例項------------------------------------------------
簡而言之,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(T-code:BD54),新增一個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.
CLEAR ls_edidc.
*系統根據下面4行即可與WE20設定關聯起來
ls_edidc-mestyp = 'YPO'.     "Message Type
ls_edidc-idoctp = 'YPOIDOC'. "IDOC Type
ls_edidc-rcvprn = 'I02LS'.   "partner Number of Recipient6
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.
 
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.
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'.8 P( Y; S' _9 |
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.
 
 7,檢視IDOC.
  WE05可以檢視我們剛剛傳送的IDOC, BD87還可對IDOC進行一些處理。
---------------------------------------例項----------------------------------Inbound Idoc
 
這裡將繼續沿用上篇文章的例子,講述I02對接收到的採購訂單資訊進行的相關處理。
1,建立IDOC Type和Message Type.
與上篇Outbound Idoc中的前兩個步驟完全一樣。
2,建立一個function:Y_IDOC_PO_PROCESS.
當IDOC設定完畢之後,SAP可以自動呼叫該Funtion Module處理IDOC。所以這個函式的介面都是規範的。
下面步驟中將介紹的tcode:BD51中可以查詢到很多Inbound Function,比如IDOC_INPUT_BBP_IV,可參照建立我們的介面。
接下來就可以寫入我們的程式碼,根據IDOC內容,建立相應的銷售訂單。為了簡化,這裡我們僅僅將其存到資料庫表裡面,請先建立兩個表Y02_POHEAD和Y02_POITEM,欄位參考WE31中的YPOHEAD和YPOITEM。然後寫入下面程式碼:
DATA: lv_subrc LIKE sy-subrc,  
      ls_chead TYPE ypohead,
      ls_citem TYPE ypoitem,
ls_pohead TYPE y02_pohead,
      lt_poitem TYPE TABLE OF y02_poitem WITH HEADER LINE.
CLEAR idoc_contrl.
READ TABLE idoc_contrl INDEX 1.
  IF idoc_contrl-mestyp <> 'YPO'.
    RAISE wrong_function_called.
ENDIF.
LOOP AT idoc_contrl.
CLEAR: ls_pohead, lt_poitem[].
LOOP AT idoc_data WHERE docnum = idoc_contrl-docnum.
CASE idoc_data-segnam.
        WHEN 'YPOHEAD'.
          CLEAR: ls_chead, ls_pohead.
          ls_chead = idoc_data-sdata.
          MOVE-CORRESPONDING ls_chead TO ls_pohead.
        WHEN 'YPOITEM'.
          CLEAR: ls_citem, lt_poitem.
          ls_citem = idoc_data-sdata.
          MOVE-CORRESPONDING ls_citem TO lt_poitem.
          APPEND lt_poitem.
        WHEN OTHERS.
      ENDCASE.
    ENDLOOP.
    lv_subrc = 0.
    INSERT y02_pohead FROM ls_pohead.
    IF sy-subrc = 0.
      INSERT y02_poitem FROM TABLE lt_poitem.
      lv_subrc = sy-subrc.
    ELSE. "訂單號已經存在
      lv_subrc = sy-subrc.
    ENDIF.
    IF lv_subrc = 0.  
      COMMIT WORK.
      CLEAR idoc_status.
      idoc_status-docnum = idoc_contrl-docnum.
      idoc_status-status = '53'. "IDOC處理成功
      APPEND idoc_status.
    ELSE.
      ROLLBACK WORK.
      CLEAR idoc_status.
      idoc_status-docnum = idoc_contrl-docnum.
      idoc_status-status = '51'. "IDOC不成功
      idoc_status-msgty = 'E'. "錯誤資訊
      idoc_status-msgid = 'YMSG'.
      idoc_status-msgno = '001'.
      APPEND idoc_status.
    ENDIF.
  ENDLOOP.
 
3,在BD51中註冊我們的Function Module.
在編輯狀態下,點選New Entries,填入函式名Y_IDOC_PO_PROCESS,Input Type=1即可。
 
4,在WE57中將Function Module與IDOC Type/Message Type關聯
點選New Entries,Function Module輸入Y_IDOC_PO_PROCESS,其下的Type填寫F;IDOC Type下的Basic Type填寫YPOIDOC;Message Type填寫YPO;Direction填寫2(Inbound)。
5,WE42,建立Inbound Process Code.
注意該步驟必須在BD51和WE57之後,否則將出現錯誤提示。
建立新條目,Process Code輸入YPC_PO,在Option ALE下選擇Processing with ALE service,在Processing Type下選擇function module。儲存後,在隨後的視窗中,輸入Inbound Module為Y_IDOC_PO_PROCESS。
6,WE20,維護Partner Profiles.
首先確保SALE中已經維護好了對應於接收方的Logical System,假設名稱為I04LS。(WE05檢視您的Inbound IDOC, 均可看到其Partner名稱,這就是我們所需要的。本步驟與上篇的WE20類似,不同的是,這裡是維護從I04LS過來的Inbound Parameters。Message Type輸入YPO,Process Type輸入YPC_PO,然後選中"Trigger immediately"即可。
7,至此,我們的設定已經完畢。系統接收到從I04傳來的IDOC後即會立刻處理。
同時,各位針對上篇做過練習的朋友,系統中應該會遺留幾個處於出錯狀態的IDOC吧?可以用BD87,在主介面上選中結點YPO後,點選Process按鈕,一次性處理。處理前,順便在SE37中開啟Y_IDOC_PO_PROCESS並設定幾個斷點即可除錯。
補充:
(1)如果狀態碼為56,則應該是您的WE20沒設定好;
(2)由於上面程式碼中控制了不能插入同樣的採購單號,所以只有一個Inbound IDOC將處理成功(狀態碼53),其餘將失敗(狀態碼51)。只要新傳送的IDOC賦於不同的採購單號即可避免。
(3)Outbound的狀態碼是0-50,03代表OK;Inbound的狀態碼則是50-99,53代表OK。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16794144/viewspace-743853/,如需轉載,請註明出處,否則將追究法律責任。

相關文章