idoc same system

Upload: nilesh-pawar

Post on 09-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 IDOC Same System

    1/15

    This article will help you understand the basics of ALE and IDocs via a simple do-it-yourself example. We will

    create a custom IDoc in one SAP system and then post some business data through it to another SAP

    system. Business data will be picked up from custom data dictionary tables.

    ALE Application Link Enabling is a mechanism by which SAP systems communicate with each other and

    with non-SAP EDI subsystems. Thus it helps integration of distributed systems. It supports fail-safe deliverywhich implies that sender system does not have to worry about message not reaching the source due to

    unavoidable situations. ALE can be used for migration and maintenance of master data as well as for

    exchanging transactional data.

    The messages that are exchanged are in the form of IDocs or Intermediate Documents. IDocs act like a

    container or envelope for the application data. An IDOC is created as a result of execution of an Outbound

    ALE. In an Inbound ALE an IDOC serves as an input to create application document. In the SAP system

    IDocs are stored in the database tables. They can be used for SAP to SAP and SAP to non-SAP process

    communication as long as the participating processes can understand the syntax and semantics of the data.

    Complete documentation on IDOC is obtained by using transaction WE60.

    Every IDoc has exactly one control record along with a number of data records and status records. Control

    record has the details of sender/receiver and other control information. Data records contain the actual

    business data to be exchanged while the status records are attached to IDoc throughout the process as the

    IDoc moves from one step to other.

    Now, let us understand the ALE Configuration by means of an example scenario below:

    The Scenario

    Data from custom tables (created in customer

    namespace) is to be formatted into an IDoc and sentfrom one SAP R/3 system to another using ALE

    service. We need to have two instances of SAP R/3

    systems or we can simulate this on two clients of the

    same SAP R/3 system.

    Create three tables as shown below.

  • 8/8/2019 IDOC Same System

    2/15

    Creating Custom IDoc type and Message type

    All the objects created should be present on both source as well as target system(s).

    1. Create segments Transaction WE31

    y Create a segment ZRZSEG1

    y Add all fields of table ZCUSTOMERS to it

    y Save the segment

    y Release it using the menu path Edit -> Set Release

    y Similarly create two more segments given below

    y Seg. ZRZSEG2 to hold all fields of table ZSOHEADERS

    y Seg. ZRZSEG3 to hold all fields of table ZSOITEMS

    2. Create Basic IDoc type Transaction WE30

    y Create a Basic type ZRZORDER

  • 8/8/2019 IDOC Same System

    3/15

    y Add the created segments in the hierarchy shown

    y Maintain attributes for each of the segments

    y Save the object and go back

    y Release the object using the menu path Edit -> Set Release

    3. Create/Assign Message type Transactions WE81/WE82

    y Go to WE81

    y Create a new Message type ZRZSO_MT

    y Save the object

    y Go to WE82 and create new entry

    y Assign the message type ZRZSO_MT to the basic type ZRZORDER

    y Also specify the Release Version

    y Save the object

    Thus we have defined the IDoc structure which will hold the data to be transferred. In the next part of the

    article we will understand the outbound settings, i.e. the settings to be done in the source system.

    In the previous part we created an IDoc structure which can carry our data from source system to target

    system(s). In this part we will understand how to setup the source system to be able to generate and send an

    outbound IDoc.

    Outbound Settings

    Define Logical Systems and Assign Client to Logical System Transaction SALE

    y Go to Define Logical System (See the

    figure)

    y Define a new logical system to identify

    the local system and save it

    y Now, go to Assign Client to Logical

    System (See the figure)

    y Add a new entry

    y Specify the client, previously created logical system and other attributes

    y Save the entryy Define a new logical system to identify the partner system and save it

    Maintain RFC Destinations Transaction SM59

    y Create a new RFC destination for R/3 type connection

    y Specify the target host on Technical settings tab

    y Provide the Logon credentials on the Logon/Security tab

  • 8/8/2019 IDOC Same System

    4/15

    y Save the settings

    y To verify the settings, Click on Test connection or Remote logon

    Define Ports Transaction WE21

    y

    We need to define a tRFC port for the partner systemy Click on Transactional RFC node

    y Create a new port

    y Provide a description

    y Specify the name of the target RFC destination

    y Save the object

    Maintain Distribution Model Transaction BD64

    y Click on Change

    y Create a new model view

    y

    Provide a Short text and Technical name to the model viewy Add message type

    y Specify sender and receiver systems

    y Also, specify the message type that we created previously

    y Save the Distribution model

    Generate/Create Partner Profile Transactions BD82/WE20

    y To generate Partner profiles automatically you may use BD82 or go to BD64 and use the menu path

    Environment -> Generate partner profiles

  • 8/8/2019 IDOC Same System

    5/15

    y Otherwise, you may use transaction WE20 to create a partner profile

    y On selection screen, specify the model view, target system and execute

    y The result log will be displayed on the next screen

    y To verify the partner profile go to WE20

    y Check the partner profile for the target system

    Distribute Model View Transaction BD64

    y Select the Model View

    y Go to menu path Edit -> Model View -> Distribute

    y Result log will be displayed on the next screen

    Outbound IDoc Generation Program

    Create an executable program ZRZ_ORDER_IDOC in SE38. Below, I have described the program logic:

    y Fetch the data from the DDic tables ZCUSTOMERS, ZSOHEADERS and ZSOITEMS as per theselection criteria

    y Fill the control record structure of type EDIDC

    y Specify message type, Basic IDoc type, tRFC Port, Partner number and Partner type of the

    receiver

    y Fill the data records

    y Define structures like the IDoc segments

    y Fill the structures with fetched data

    y Pass the segment name and the above structure to the appropriate fields of EDIDD type

    structure

    y Append the EDIDD structure to the EDIDD type internal table

    y Now, call the function module MASTER_IDOC_DISTRIBUTE and pass the IDoc control recordstructure and data record table

    y Commit work if return code is zero

    y Function module returns a table of type EDIDC to provide the details about generated IDoc

    y Display appropriate log

    You can download sample code for the above program here.

    *&---------------------------------------------------------------------**& Report ZRZ_ORDER_IDOC **& **&---------------------------------------------------------------------*

    REPORT ZRZ_ORDER_IDOC .

    ************************************************************************ PURPOSE OF REPORT ************************************************************************* Generats an Idoc for Customer Master, Sales Master* and Sales item table.***********************************************************************

  • 8/8/2019 IDOC Same System

    6/15

    TABLES : ZCUSTOMERS, "Cutomer HeaderZSOHEADERS, "Sales HeaderZSOITEMS. "Sales Items

    DATA : S_CTRL_REC LIKE EDIDC, "Idoc Control RecordS_ZRZSEG1 LIKE ZRZSEG1, "CUSTOMER Header DataS_ZRZSEG2 LIKE ZRZSEG2, "SALES HEADER DataS_ZRZSEG3 LIKE ZRZSEG3. "SALES Detail Data

    DATA : T_ZCUSTOMERS LIKE ZCUSTOMERS OCCURS 0 WITH HEADER LINE.DATA : T_ZSOHEADERS LIKE ZSOHEADERS OCCURS 0 WITH HEADER LINE.DATA : T_ZSOITEMS LIKE ZSOITEMS OCCURS 0 WITH HEADER LINE.DATA : T_EDIDD LIKE EDIDD OCCURS 0 WITH HEADER LINE."Data RecordsDATA : T_COMM_IDOC LIKE EDIDC OCCURS 0 WITH HEADER LINE."Generated Communication IDOc

    CONSTANTS :

    C_ZRZSEG1 LIKE EDIDD-SEGNAM VALUE 'ZRZSEG1',C_ZRZSEG2 LIKE EDIDD-SEGNAM VALUE 'ZRZSEG2',C_ZRZSEG3 LIKE EDIDD-SEGNAM VALUE 'ZRZSEG3'.

    CONSTANTS: C_IDOCTP LIKE EDIDC-IDOCTP VALUE 'ZRZORDER'.

    *** Selection ScreenSELECT-OPTIONS : S_KUNNR FOR ZCUSTOMERS-KUNNR OBLIGATORY,

    S_VBELN FOR ZSOHEADERS-VBELN.PARAMETERS : C_MESTYP LIKE EDIDC-MESTYP DEFAULT 'ZRZSO_MT',

    "Message TypeC_RCVPRT LIKE EDIDC-RCVPRT DEFAULT 'LS',"Partner type of receiverC_LOGSYS LIKE EDIDC-RCVPRN DEFAULT 'Y901',C_RCVPOR LIKE EDIDC-RCVPOR DEFAULT 'A000000226',C_SNDPRN LIKE EDIDC-SNDPRN DEFAULT 'LSSENDS',C_SNDPRT LIKE EDIDC-SNDPRT DEFAULT 'LS'.

    "Destination System

    ***START-OF-SELECTIONSTART-OF-SELECTION.PERFORM GENERATE_DATA_RECORDS.PERFORM GENERATE_CONTROL_RECORD.PERFORM SEND_IDOC.

    ***********************************************************************

    *&---------------------------------------------------------------------**& Form generate_data_records*&---------------------------------------------------------------------*FORM GENERATE_DATA_RECORDS .PERFORM FETCH_ZCUSTOMERS.PERFORM FETCH_ZSOHEADERS.

  • 8/8/2019 IDOC Same System

    7/15

    PERFORM FETCH_ZSOITEMS.PERFORM ARRANGE_DATA_RECORDS.

    ENDFORM. " generate_data_records

    *&---------------------------------------------------------------------**& Form fetch_zcustomers*&---------------------------------------------------------------------*FORM FETCH_ZCUSTOMERS.SELECT *FROM ZCUSTOMERSINTO TABLE T_ZCUSTOMERSWHERE KUNNR IN S_KUNNR.IF SY-SUBRC NE 0.MESSAGE E398(00) WITH 'No Customers Found'.

    ENDIF.ENDFORM. " fetch_zcustomers

    *&---------------------------------------------------------------------**& Form fetch_zsoheaders*&---------------------------------------------------------------------*FORM FETCH_ZSOHEADERS.SELECT *FROM ZSOHEADERSINTO TABLE T_ZSOHEADERSWHERE VBELN IN S_VBELNAND KUNNR IN S_KUNNR.

    IF SY-SUBRC NE 0.MESSAGE I398(00) WITH 'No Sales orders found'.

    ENDIF.ENDFORM. " fetch_zsoheaders

    *&---------------------------------------------------------------------**& Form fetch_zsoitems*&---------------------------------------------------------------------*FORM FETCH_ZSOITEMS.IF NOT T_ZSOHEADERS[] IS INITIAL.SELECT *FROM ZSOITEMSINTO TABLE T_ZSOITEMSFOR ALL ENTRIES IN T_ZSOHEADERSWHERE VBELN = T_ZSOHEADERS-VBELN.

    IF SY-SUBRC NE 0.MESSAGE I398(00) WITH 'No Sales order items found'.

    ENDIF.ENDIF.

    ENDFORM. " fetch_zsoitems

    *&---------------------------------------------------------------------**& Form generate_control_record

  • 8/8/2019 IDOC Same System

    8/15

  • 8/8/2019 IDOC Same System

    9/15

  • 8/8/2019 IDOC Same System

    10/15

    In the previous parts we learned how to create a custom IDoc and set up the source system to send an

    outbound IDoc. In this part we will learn how to configure the receiving SAP R/3 system to be able to receive

    and post the inbound IDoc.

    Inbound IDoc Posting Function Module

    In the receiving system, create a function module Z_IDOC_INPUT_ZRZSO_MT using SE37. Below, I have

    described the logic for the same.

    Add Include MBDCONWF. This include contains predefined ALE constants.

    Loop at EDIDC table

    y Check if the message type is ZRZORDER. Otherwise raise WRONG_FUNCTION_CALLED exception

    y Loop at EDIDD table

    y Append data from the segments to appropriate internal tables

    y For example: append data from ZRZSEG1 segment to the internal table of type ZCUSTOMERS

    y

    Update the DDic tables from internal tablesy Depending on the result of the update, fill the IDoc status record (type BDIDOCSTAT) and append it to

    the corresponding table.

    y Status 53 => Success

    y Status 51 => Error

    You can download the sample ABAP code for the above function module here.

    FUNCTION Z_IDOC_INPUT_ZRZSO_MT.*"----------------------------------------------------------------------*"*"Local interface:*" IMPORTING

    *" VALUE(INPUT_METHOD) LIKE BDWFAP_PAR-INPUTMETHD*" VALUE(MASS_PROCESSING) LIKE BDWFAP_PAR-MASS_PROC*" EXPORTING*" VALUE(WORKFLOW_RESULT) LIKE BDWF_PARAM-RESULT*" VALUE(APPLICATION_VARIABLE) LIKE BDWF_PARAM-APPL_VAR*" VALUE(IN_UPDATE_TASK) LIKE BDWFAP_PAR-UPDATETASK*" VALUE(CALL_TRANSACTION_DONE) LIKE BDWFAP_PAR-CALLTRANS*" TABLES*" IDOC_CONTRL STRUCTURE EDIDC*" IDOC_DATA STRUCTURE EDIDD*" IDOC_STATUS STRUCTURE BDIDOCSTAT*" RETURN_VARIABLES STRUCTURE BDWFRETVAR*" SERIALIZATION_INFO STRUCTURE BDI_SER*" EXCEPTIONS*" WRONG_FUNCTION_CALLED*"----------------------------------------------------------------------

    * Include File containing ALE constantsINCLUDE MBDCONWF.TABLES : ZCUSTOMERS, "Cutomer Header

    ZSOHEADERS, "Sales HeaderZSOITEMS. "Sales Items

  • 8/8/2019 IDOC Same System

    11/15

    ***DataDATA : W_ZRZSEG1 LIKE ZRZSEG1,W_ZRZSEG2 LIKE ZRZSEG2,W_ZRZSEG3 LIKE ZRZSEG3.

    DATA : T_ZCUSTOMERS LIKE ZCUSTOMERS OCCURS 0 WITH HEADER LINE.DATA : T_ZSOHEADERS LIKE ZSOHEADERS OCCURS 0 WITH HEADER LINE.DATA : T_ZSOITEMS LIKE ZSOITEMS OCCURS 0 WITH HEADER LINE.

    ***********************************************************************WORKFLOW_RESULT = C_WF_RESULT_OK.

    LOOP AT IDOC_CONTRL.IF IDOC_CONTRL-MESTYP NE 'ZRZSO_MT'.RAISE WRONG_FUNCTION_CALLED.

    ENDIF.

    * Before reading a new entry, clear application buffer

    LOOP AT IDOC_DATA WHERE DOCNUM EQ IDOC_CONTRL-DOCNUM.CASE IDOC_DATA-SEGNAM.WHEN 'ZRZSEG1'.W_ZRZSEG1 = IDOC_DATA-SDATA.MOVE-CORRESPONDING W_ZRZSEG1 TO T_ZCUSTOMERS.INSERT INTO ZCUSTOMERS VALUES T_ZCUSTOMERS.

    WHEN 'ZRZSEG2'.W_ZRZSEG2 = IDOC_DATA-SDATA.MOVE-CORRESPONDING W_ZRZSEG2 TO T_ZSOHEADERS.INSERT INTO ZSOHEADERS VALUES T_ZSOHEADERS.

    WHEN 'ZRZSEG3'.W_ZRZSEG3 = IDOC_DATA-SDATA.MOVE-CORRESPONDING W_ZRZSEG3 TO T_ZSOITEMS.INSERT INTO ZSOITEMS VALUES T_ZSOITEMS.

    ENDCASE.ENDLOOP.

    ************************************************************************* CUSTOMER HEADER ************************************************************************** SELECT ** FROM zcustomers.* ENDSELECT.* IF sy-subrc NE 0.* INSERT INTO zcustomers VALUES t_zcustomers.* ELSE.

    UPDATE ZCUSTHEAD FROM T_ZCUSTOMERS.

    * ENDIF.IF SY-SUBRC EQ 0.IDOC_STATUS-DOCNUM = IDOC_CONTRL-DOCNUM.IDOC_STATUS-STATUS = '53'.IDOC_STATUS-MSGTY = 'I'.IDOC_STATUS-MSGID = 'YM'.IDOC_STATUS-MSGNO = '004'.IDOC_STATUS-MSGV1 = T_ZCUSTOMERS-KUNNR.APPEND IDOC_STATUS.CLEAR IDOC_STATUS.

  • 8/8/2019 IDOC Same System

    12/15

    ELSE.IDOC_STATUS-DOCNUM = IDOC_CONTRL-DOCNUM.IDOC_STATUS-STATUS = '51'.IDOC_STATUS-MSGTY = 'E'.IDOC_STATUS-MSGID = 'YM'.IDOC_STATUS-MSGNO = '005'.IDOC_STATUS-MSGV1 = T_ZCUSTOMERS-KUNNR.APPEND IDOC_STATUS.CLEAR IDOC_STATUS.WORKFLOW_RESULT = C_WF_RESULT_ERROR.RETURN_VARIABLES-WF_PARAM = 'Error_Idocs'.RETURN_VARIABLES-DOC_NUMBER = IDOC_CONTRL-DOCNUM.APPEND RETURN_VARIABLES.CLEAR RETURN_VARIABLES.

    ENDIF.************************************************************************* SALES HEADER ************************************************************************** SELECT ** FROM zsoheaders.

    * ENDSELECT.* IF sy-subrc NE 0.* INSERT INTO zsoheaders VALUES t_zsoheaders.* ELSE.

    UPDATE ZSOHEADERS FROM T_ZSOHEADERS.* ENDIF.

    IF SY-SUBRC EQ 0.IDOC_STATUS-DOCNUM = IDOC_CONTRL-DOCNUM.IDOC_STATUS-STATUS = '53'.IDOC_STATUS-MSGTY = 'I'.IDOC_STATUS-MSGID = 'YM'.IDOC_STATUS-MSGNO = '004'.IDOC_STATUS-MSGV1 = T_ZSOHEADERS-VBELN.APPEND IDOC_STATUS.CLEAR IDOC_STATUS.

    ELSE.IDOC_STATUS-DOCNUM = IDOC_CONTRL-DOCNUM.IDOC_STATUS-STATUS = '51'.IDOC_STATUS-MSGTY = 'E'.IDOC_STATUS-MSGID = 'YM'.IDOC_STATUS-MSGNO = '005'.IDOC_STATUS-MSGV1 = T_ZSOHEADERS-VBELN.APPEND IDOC_STATUS.CLEAR IDOC_STATUS.WORKFLOW_RESULT = C_WF_RESULT_ERROR.RETURN_VARIABLES-WF_PARAM = 'Error_Idocs'.RETURN_VARIABLES-DOC_NUMBER = IDOC_CONTRL-DOCNUM.

    APPEND RETURN_VARIABLES.CLEAR RETURN_VARIABLES.

    ENDIF.************************************************************************* SALES ITEM ************************************************************************** SELECT ** FROM zsoitems.* ENDSELECT.* IF sy-subrc NE 0.

  • 8/8/2019 IDOC Same System

    13/15

    * INSERT INTO zsoitems VALUES t_zsoitems.* ELSE.

    UPDATE ZSOITEMS FROM T_ZSOITEMS.* ENDIF.

    IF SY-SUBRC EQ 0.IDOC_STATUS-DOCNUM = IDOC_CONTRL-DOCNUM.IDOC_STATUS-STATUS = '53'.IDOC_STATUS-MSGTY = 'I'.IDOC_STATUS-MSGID = 'YM'.IDOC_STATUS-MSGNO = '004'.IDOC_STATUS-MSGV1 = T_ZSOITEMS-VBELN.APPEND IDOC_STATUS.CLEAR IDOC_STATUS.

    ELSE.IDOC_STATUS-DOCNUM = IDOC_CONTRL-DOCNUM.IDOC_STATUS-STATUS = '51'.IDOC_STATUS-MSGTY = 'E'.IDOC_STATUS-MSGID = 'YM'.IDOC_STATUS-MSGNO = '005'.IDOC_STATUS-MSGV1 = T_ZSOITEMS-VBELN.

    APPEND IDOC_STATUS.CLEAR IDOC_STATUS.WORKFLOW_RESULT = C_WF_RESULT_ERROR.RETURN_VARIABLES-WF_PARAM = 'Error_Idocs'.RETURN_VARIABLES-DOC_NUMBER = IDOC_CONTRL-DOCNUM.APPEND RETURN_VARIABLES.CLEAR RETURN_VARIABLES.

    ENDIF.************************************************************************ENDLOOP.

    ENDFUNCTION.

    Inbound Settings

    y Define Logical Systems Transaction SALE (Please refer to Outbound Settings discussed in previous

    part)

    y Assign Client to Logical System Transaction SALE (Please refer to Outbound Settings discussed in

    previous part)

    y Maintain RFC Destinations Transaction SM59 (Please refer to Outbound Settings discussed in

    previous part)

    y Define Ports Transaction WE21 (Please refer to Outbound Settings discussed in previous part)

    y Generate/Create Partner Profile Transactions BD82/WE20 (Please refer to Outbound Settings

    discussed in previous part)

    y Assign Function Module to Logical message Transaction WE57

    y Create a new entry

    y Specify name of the Function Module as Z_IDOC_INPUT_ZRZSO_MT

  • 8/8/2019 IDOC Same System

    14/15

    y Also, specify Type as F, Basic IDoc type as ZRZORDER, Message type as ZRZSO_MT and

    Direction as 2 (Inbound)

    y Save the entry

    y Define Input method for Inbound Function Module Transaction BD51

    y Create a new entry

    y

    Provide Function Module name as Z_IDOC_INPUT_ZRZSO_MTy Specify the Input method as 2

    y Save the entry

    y Create a Process Code Transaction WE42

    y Create a new Process Code ZPCRZ

    y Select Processing with ALE Service

    y Choose Processing type as Processing by function module

    y Save the entry

    y On the next screen, select your function module from the list

    y Save the changes

    y Now you will be taken to the next screen

    y

    Double-click on Logical messagey In the Assignment to logical message, specify the message type ZRZSO_MT

    y Save the changes

    Send and receive data

    On the sender system, execute the IDoc Generation Program. Check the status of IDoc using transaction

    WE02.

    Check the status of the IDoc in the receiver system using transaction WE02. You can also check the contents

    of DDic tables to make sure that the records have been created in the receiver system.

  • 8/8/2019 IDOC Same System

    15/15

    Thus to summarize we have learned how to:

    y Create a custom IDoc

    y Write an Outbound IDoc Generation Program

    y Write Inbound Function Module to post Inbound IDoc

    y Configure and test ALE scenario to transmit data between systems distributed across the network