Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Idoc programming question

Former Member
0 Likes
401

Hi friends,

I am asked to write a custom executable program (not a function module) to create outbound idocs with some data from the database tables as for the selection screen data the user entered. How do I do this and how to trigger these outbound idocs? Can you please give me a sample code for this or your valuable suggestions?

Thanks for the help.

1 ACCEPTED SOLUTION
Read only

raja_thangamani
Active Contributor
0 Likes
366

Here is the sample code which triggers the IDoc (Outbound)..I also added comments

report z_idoc_creat_tr .

tables : ekpo,
         edidd,  " Data record (IDoc)
         tbdlst, " Text for logical system
         edmsg . "Logical message types

* Selection screen
parameters: p_ebeln like ekpo-ebeln.

data : c_seg1 like edidd-segnam value 'ZSEG1_TR',
       c_seg2 like edidd-segnam value 'ZSEG2_TR',
       itab_comm_idocs  like edidc occurs 0 with header line ,
       control_record like edidc.

*Internal Tables

data :
     itab_edidd like edidd occurs 0 with header line, "Data record(IDoc)
       itab_seg2 like zseg2_tr  occurs 0 with header line .
* zseg2_tr structure generated when you define the Segments in WE31

* Work Area

data : seg1 like zseg1_tr, " Segments Header Data
       seg2 like zseg2_tr. " Segments 

select single * from ekpo  where ebeln = p_ebeln.

*Build Control Data

control_record-mestyp = 'Z_EKPO' .  "Message Type.
control_record-idoctp = 'ZIDOC_TR' ." IDoc Type

control_record-sndprt = 'LS'. 
control_record-sndprn = 'LOGSYS0100' . " Logical system
control_record-sndpor = 'SAPLT1'. "SEnder Port

control_record-rcvprt = 'LS'.
control_record-rcvprn = 'SEND' ." Reveiver Logical system
control_record-rcvpor = 'A000000171'." Receiver Port

*Filling the Segment 1 ie.,Purchasing Document Number .

move-corresponding ekpo to seg1.

itab_edidd-segnam = 'ZSEG1_TR'." Segment Name
itab_edidd-sdata = seg1.  " Value for the Segment1. "Segment value

append itab_edidd .
clear itab_edidd.

*Filling the Segment 2 ie.,Item Number of Purchasing Document.

select * from ekpo into corresponding fields of table itab_seg2  where
         ebeln = p_ebeln .

loop at itab_seg2 .

  move-corresponding  itab_seg2 to seg2.

  itab_edidd-segnam = 'ZSEG2_TR'.
  itab_edidd-sdata = seg2.

  append itab_edidd.
  clear itab_edidd.

endloop.

* FM to trigger the Outbound Idoc
call function 'MASTER_IDOC_DISTRIBUTE'
  exporting
    master_idoc_control                  = control_record
  tables
    communication_idoc_control           = itab_comm_idocs
    master_idoc_data                     = itab_edidd
 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.

endif.

loop at itab_comm_idocs.

  write:/2 'Docs generated', itab_comm_idocs-docnum.

endloop.

<i>* Reward each useful answer</i>

Raja T

Message was edited by:

Raja Thangamani

2 REPLIES 2
Read only

raja_thangamani
Active Contributor
0 Likes
367

Here is the sample code which triggers the IDoc (Outbound)..I also added comments

report z_idoc_creat_tr .

tables : ekpo,
         edidd,  " Data record (IDoc)
         tbdlst, " Text for logical system
         edmsg . "Logical message types

* Selection screen
parameters: p_ebeln like ekpo-ebeln.

data : c_seg1 like edidd-segnam value 'ZSEG1_TR',
       c_seg2 like edidd-segnam value 'ZSEG2_TR',
       itab_comm_idocs  like edidc occurs 0 with header line ,
       control_record like edidc.

*Internal Tables

data :
     itab_edidd like edidd occurs 0 with header line, "Data record(IDoc)
       itab_seg2 like zseg2_tr  occurs 0 with header line .
* zseg2_tr structure generated when you define the Segments in WE31

* Work Area

data : seg1 like zseg1_tr, " Segments Header Data
       seg2 like zseg2_tr. " Segments 

select single * from ekpo  where ebeln = p_ebeln.

*Build Control Data

control_record-mestyp = 'Z_EKPO' .  "Message Type.
control_record-idoctp = 'ZIDOC_TR' ." IDoc Type

control_record-sndprt = 'LS'. 
control_record-sndprn = 'LOGSYS0100' . " Logical system
control_record-sndpor = 'SAPLT1'. "SEnder Port

control_record-rcvprt = 'LS'.
control_record-rcvprn = 'SEND' ." Reveiver Logical system
control_record-rcvpor = 'A000000171'." Receiver Port

*Filling the Segment 1 ie.,Purchasing Document Number .

move-corresponding ekpo to seg1.

itab_edidd-segnam = 'ZSEG1_TR'." Segment Name
itab_edidd-sdata = seg1.  " Value for the Segment1. "Segment value

append itab_edidd .
clear itab_edidd.

*Filling the Segment 2 ie.,Item Number of Purchasing Document.

select * from ekpo into corresponding fields of table itab_seg2  where
         ebeln = p_ebeln .

loop at itab_seg2 .

  move-corresponding  itab_seg2 to seg2.

  itab_edidd-segnam = 'ZSEG2_TR'.
  itab_edidd-sdata = seg2.

  append itab_edidd.
  clear itab_edidd.

endloop.

* FM to trigger the Outbound Idoc
call function 'MASTER_IDOC_DISTRIBUTE'
  exporting
    master_idoc_control                  = control_record
  tables
    communication_idoc_control           = itab_comm_idocs
    master_idoc_data                     = itab_edidd
 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.

endif.

loop at itab_comm_idocs.

  write:/2 'Docs generated', itab_comm_idocs-docnum.

endloop.

<i>* Reward each useful answer</i>

Raja T

Message was edited by:

Raja Thangamani

Read only

abdul_hakim
Active Contributor
0 Likes
366

hi

have u refered the book "ALE/EDI and IDoc Technologies for SAP" By Arvind Nagpal..If not plz go ahead..it will help to write your own outbound / inbound Programs

Cheers,

Abdul Hakim