Sunday, 18 December 2016

How to retrieve data from IDOC using segment and mail IDOC information as an attachment

                                    Invoice IDOC status report




  • Go to transaction code SE38.
  • Enter the program name which start with Y or Z<program name > and click on CREATE button.

  • Pop-up will appear.
  • Enter the title .
  • Select the attribute type as Executable program.
  • Click on Save.

  • Enter the package which is start's with Y or Z or Local Package.

Source Code




REPORT  ZSD_INVOICE_IDOC_REPORT.

INITIALIZATION.
**---------------------------------------------------------------------*
** Table Declaration
**---------------------------------------------------------------------*
TABLES : edidc.

**---------------------------------------------------------------------*
** Types Declaration
**---------------------------------------------------------------------*

*!-- Control record (IDoc)

TYPES : BEGIN OF ts_control,
        docnum TYPE edidc-docnum,          "Doc number
        status TYPE edidc-status,          "Status of IDoc
        direct TYPE edidc-direct,          "Direction for IDoc
        rcvprn  TYPE edidc-rcvprn,         "Partner Number of Receiver
        credat TYPE edidc-credat,          "IDoc Created On
        mestyp TYPE edidc-mestyp,          "Message Type
       END OF ts_control.

*!-- Status Record (IDoc)

TYPES : BEGIN OF ts_status,      " Used to display reason for failure in report
        docnum TYPE edids-docnum,          "Doc number
        statxt TYPE edids-statxt,          "Text for status code
        END OF ts_status.

*!-- IDoc Data Records

TYPES : BEGIN OF ts_data,
         docnum TYPE edid4-docnum,        "Doc number
         sdata TYPE edid4-sdata,          "Application data
       END OF ts_data.

TYPES : BEGIN OF ts_final,
        date TYPE edidc-credat,            " Date
        customer TYPE e1edka1-partn,       " Customer
        saloff TYPE e1edk14-orgid,         " Sales Office
        docnum TYPE edidc-docnum ,         " IDOC number
        irnum TYPE e1edk01-belnr,          " Invoice number
        iramount TYPE e1eds01-summe,       " Invoice amount
        ponum TYPE e1edp02-belnr,          " PO number
        ornum TYPE e1edp02-belnr,          " Order number
        partner TYPE edidc-rcvprn,         " Trading partner
        status TYPE edids-status ,         " Status
        error TYPE edids-statxt ,          " Error
      END OF ts_final.

**-----------------------------------------------------------------------*
**  Internal table and work area declaration
**-----------------------------------------------------------------------*

DATA : lt_control TYPE TABLE OF ts_control,
       ls_control TYPE ts_control,
       lt_manual  TYPE TABLE OF ts_control.    " To capture any idocs not yet manually processed


DATA : lt_status TYPE TABLE OF ts_status ,    " Used to display reason for failure in report
       ls_status TYPE ts_status.

DATA : lt_invoice      TYPE TABLE OF ts_data,     " To get invoice number
       ls_invoice      TYPE ts_data,
       lt_order        TYPE TABLE OF ts_data,     " To get Order number
       ls_order        TYPE ts_data,
       lt_po           TYPE TABLE OF ts_data,     " To get PO number
       ls_po           TYPE ts_data,
       lt_customer     TYPE TABLE OF ts_data,    " To get Customer number
       ls_customer     TYPE ts_data,
       lt_iramount     TYPE TABLE OF ts_data,    " To get Invoice amount
       ls_iramount     TYPE ts_data,
       lt_salorg       TYPE TABLE OF ts_data,     " To get Sales org
       ls_salorg       TYPE ts_data,
       lt_saloffice    TYPE TABLE OF ts_data,    " To get Sales office
       ls_saloffice    TYPE ts_data,
       lt_final        TYPE TABLE OF ts_final,   " Output
       ls_final        TYPE ts_final.

data : lT_ATTACH TYPE STANDARD TABLE OF SOLISTI1 INITIAL SIZE 0
                                              WITH HEADER LINE.

 DATA: lv_filename(9),
       lv_subject(70).


 DATA: lt_message TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0
                 WITH HEADER LINE,
       lt_packing_list LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
       lt_attachment LIKE solisti1 OCCURS 0 WITH HEADER LINE,
       lt_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
       ls_receivers TYPE somlreci1.
*  t_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE

 DATA : gv_error    TYPE sy-subrc,
        gv_reciever TYPE sy-subrc.

 Data : ls_doc_data LIKE sodocchgi1,
       lv_cnt TYPE i,
      lv_sent_all(1) TYPE c.



**---------------------------------------------------------------------*
** Selection-Screen
**---------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK blk WITH FRAME TITLE text-001.

SELECT-OPTIONS : s_credat FOR edidc-credat.                             "IDoc Created On

SELECT-OPTIONS : s_rcvprn FOR edidc-rcvprn.                             "Partner Number of Receiver

SELECT-OPTIONS : s_mestyp FOR edidc-mestyp DEFAULT 'INVOIC'.            "Message Type

PARAMETERS     : p_direct TYPE edidc-direct DEFAULT 1.                  "Direction for IDoc

SELECT-OPTIONS : s_docnum FOR edidc-docnum.                              "IDoc number

SELECTION-SCREEN END OF BLOCK blk.


START-OF-SELECTION.
*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*



* From table Control record (IDoc)

  SELECT docnum status direct rcvprn credat mestyp
    FROM edidc
    INTO TABLE lt_control                                     " To capture all outbound  idocs
    WHERE credat IN s_credat
    AND rcvprn IN s_rcvprn
    AND mestyp IN s_mestyp
    AND direct EQ p_direct
    AND docnum IN s_docnum.

  IF lt_control IS NOT INITIAL.



    SELECT docnum status direct rcvprn credat mestyp
      FROM edidc
      INTO TABLE lt_manual                                  " To capture any idocs not yet manually processed
      WHERE credat IN s_credat
      AND rcvprn IN s_rcvprn
      AND mestyp IN s_mestyp
      AND direct EQ p_direct
      AND docnum IN s_docnum
      AND status EQ 15.


* From table Status Record (IDoc)

    IF lt_manual  IS NOT INITIAL.


      SELECT docnum statxt
        FROM edids
        INTO TABLE lt_status                                 " Used to display reason for failure in report
        FOR ALL ENTRIES IN lt_manual
        WHERE docnum EQ lt_manual-docnum
        AND status EQ 15.

    ENDIF.




* From table IDoc Data Records

  SELECT docnum sdata
    FROM edid4
    INTO TABLE lt_invoice                                      " To get invoice number
    FOR ALL ENTRIES IN lt_control
    WHERE docnum EQ lt_control-docnum
    AND segnam EQ 'E1EDK01'.


  SELECT docnum sdata
     FROM edid4
     INTO TABLE lt_order                                       " To get Order number
     FOR ALL ENTRIES IN lt_control
     WHERE docnum EQ lt_control-docnum
    AND segnam EQ 'E1EDP02'.

  SELECT docnum sdata
   FROM edid4
   INTO TABLE lt_po                                          " To get PO number
    FOR ALL ENTRIES IN lt_control
   WHERE docnum EQ lt_control-docnum
  AND segnam EQ 'E1EDP02'.


  SELECT docnum sdata
  FROM edid4
  INTO TABLE lt_customer                                    " To get Customer number
  FOR ALL ENTRIES IN lt_control
  WHERE docnum EQ lt_control-docnum
 AND segnam EQ 'E1EDKA1'.

  SELECT docnum sdata
  FROM edid4
  INTO TABLE  lt_iramount                                   " To get Invoice amount
  FOR ALL ENTRIES IN lt_control
  WHERE docnum EQ lt_control-docnum
 AND segnam EQ 'E1EDS01'.

  SELECT docnum sdata
   FROM edid4
   INTO TABLE  lt_salorg                                      " To get Sales org
    FOR ALL ENTRIES IN lt_control
   WHERE docnum EQ lt_control-docnum
  AND segnam EQ 'E1EDK14'.


  SELECT docnum sdata
  FROM edid4
  INTO TABLE lt_saloffice                                     " To get Sales office
    FOR ALL ENTRIES IN lt_control
  WHERE docnum EQ lt_control-docnum
 AND segnam EQ 'E1EDK14'.

  ENDIF.





*!--------------------------------------------------------------------
*!--------- Final Data
*!--------------------------------------------------------------------



  DATA : ls_e1edk01   TYPE e1edk01,    " For  Invoice number  segment
         ls_e1edp02_o TYPE e1edp02,    " For  Order number   segment
         ls_e1edp02_p TYPE e1edp02,    " For Po number segment
         ls_e1edka1   TYPE e1edka1,    " For Customer number segment
         ls_e1eds01   TYPE e1eds01,    " For Invoice amount  segment
         "ls_salorg    TYPE e1edk14,    " For Sales org segment
         ls_saloff    TYPE e1edk14.    " For Sales office segment


  LOOP AT lt_control INTO ls_control.
*---Date

    ls_final-date  = ls_control-credat.         "Date

*---To get customer
    CLEAR ls_customer.

    READ TABLE lt_customer
     INTO ls_customer
     WITH KEY docnum = ls_control-docnum
     BINARY SEARCH .
    IF sy-subrc EQ 0.

      ls_e1edka1  = ls_customer-sdata.

      IF ls_e1edka1-parvw EQ 'AG' .
        ls_final-customer = ls_e1edka1-partn.     "Customer number
      ENDIF.


    ENDIF.

*---To get sales office

    CLEAR ls_saloffice .

    READ TABLE lt_saloffice
    INTO ls_saloffice
    WITH KEY docnum = ls_control-docnum
    BINARY SEARCH.

    IF sy-subrc EQ 0.
      ls_saloff = ls_saloffice-sdata.

      IF ls_saloff-qualf EQ '016'.

        ls_final-saloff = ls_saloff-orgid.          " Sales office

      ENDIF.
    ENDIF.

*--- To get IDOC number
    ls_final-docnum = ls_control-docnum.          " Idoc number

*---To get Invoice number

    CLEAR ls_invoice.

    READ TABLE lt_invoice
    INTO ls_invoice
    WITH  KEY docnum = ls_control-docnum
   BINARY SEARCH.

    IF sy-subrc EQ 0.
      ls_e1edk01 = ls_invoice-sdata.
      ls_final-irnum = ls_e1edk01-belnr.         " Invoice number

    ENDIF.

*---To get Invoice amount

    CLEAR ls_iramount.

    READ TABLE lt_iramount
    INTO ls_iramount
    WITH  KEY docnum = ls_control-docnum
  BINARY SEARCH.

    IF sy-subrc EQ 0.

      ls_e1eds01 = ls_iramount-sdata.

      IF ls_e1eds01-sumid EQ '011'.

        ls_final-iramount = ls_e1eds01-summe.      "Invoice amount

      ENDIF.

    ENDIF.

*--- To get PO number

    CLEAR ls_po.

    READ TABLE lt_po
    INTO ls_po
     WITH  KEY docnum = ls_control-docnum
    BINARY SEARCH.

    IF sy-subrc EQ 0.
      ls_e1edp02_p = ls_po-sdata.

      IF ls_e1edp02_p-qualf EQ '001'.

        ls_final-ponum = ls_e1edp02_p-belnr.       " Po Number

      ENDIF.


    ENDIF.

*--- Order number

    CLEAR  ls_order.

    READ TABLE lt_order
    INTO ls_order
     WITH  KEY docnum = ls_control-docnum
   BINARY SEARCH.

    IF   sy-subrc EQ 0.

      ls_e1edp02_o =  ls_order-sdata.

      IF ls_e1edp02_o-qualf EQ '002'.

        ls_final-ornum = ls_e1edp02_o-belnr.     " Order number.

      ENDIF.

    ENDIF.



*--- partner

    ls_final-partner  = ls_control-rcvprn .     " Partner Number of Receiver

*---Status

    ls_final-status = ls_control-status.        " Status of Idocs

*-- Error

    CLEAR ls_status.

    READ TABLE lt_status
    INTO ls_status
    WITH  KEY docnum = ls_control-docnum
    BINARY SEARCH.


    IF sy-subrc EQ 0.

      ls_final-error = ls_status-statxt.       " Error

    ENDIF.

    APPEND ls_final TO lt_final.

    CLEAR : ls_control , ls_final ,ls_e1edk01 ,ls_e1edp02_o , ls_e1edp02_p , ls_e1edka1 , ls_e1eds01 , ls_salorg, ls_saloff  .
  ENDLOOP.

END-OF-SELECTION.
*!-------------------------------------------------------------------
*Build Excel
*!-------------------------------------------------------------------
*If you have Unicode check active in program attributes thnen you will
*need to declare constants as follows
*class cl_abap_char_utilities definition load.

  CONSTANTS:
         con_tab  TYPE c VALUE cl_abap_char_utilities=>horizontal_tab,
         con_cret TYPE c VALUE cl_abap_char_utilities=>cr_lf.

*-- XLS For header label
  CONCATENATE 'Date' 'Customer' 'Sales Office' 'Idoc Number' 'Invoice Number' 'Invoice Amount'
              'PO Number' 'Order Number' 'Trading Partner' 'Status' 'Error'
     INTO lt_attach
     SEPARATED BY con_tab.
  CONCATENATE con_cret lt_attach  INTO lt_attach.
  APPEND  lt_attach.
  CLEAR  : lt_attach , ls_final.

  LOOP AT lt_final INTO ls_final.

    CONCATENATE ls_final-date ls_final-customer ls_final-saloff ls_final-docnum ls_final-irnum
      ls_final-iramount  ls_final-ponum  ls_final-ornum  ls_final-partner  ls_final-status  ls_final-error
      INTO lt_attach
     SEPARATED BY con_tab.
    CONCATENATE con_cret lt_attach  INTO lt_attach.
    APPEND  lt_attach.
    CLEAR : lt_attach ,ls_final .

  ENDLOOP.






*&---------------------------------------------------------------------*
*&     POPULATE_EMAIL_MESSAGE_BODY


lv_subject  = 'Invoice IDOC status Report' .


*! File name has to SAPTECH.txt

CLEAR lv_filename.
CONCATENATE 'SAPTECH' sy-datum INTO lv_filename.
CONDENSE lv_filename.


PERFORM send_file_as_email_attachment
                           TABLES lt_message
                                   lt_attach
                           USING  lv_subject
                                  'XLS'
                                  'Filename'
                                  lv_filename
                                  '  '
                                  '  '
                         CHANGING
                                  gv_error
                                  gv_reciever.

*&---------------------------------------------------------------------*
*&      Form  SEND_FILE_AS_EMAIL_ATTACHMENT
*&---------------------------------------------------------------------*

*----------------------------------------------------------------------*
FORM send_file_as_email_attachment  TABLES   plt_message
                                             plt_attach
                                    USING    p_mtitle
                                             p_format
                                             p_filename
                                             p_attdescription
                                             p_sender_address
                                             p_sender_addres_type
                                    CHANGING p_gv_error
                                             p_gv_reciever.





  DATA:   lv_error    TYPE sy-subrc,
          lv_reciever TYPE sy-subrc,
          lv_mtitle LIKE sodocchgi1-obj_descr,
          lv_email LIKE  somlreci1-receiver,
          lv_format TYPE  so_obj_tp ,
          lv_attdescription TYPE  so_obj_nam ,
          lv_attfilename TYPE  so_obj_des ,
          lv_sender_address LIKE  soextreci1-receiver ,
          lv_sender_address_type LIKE  soextreci1-adr_typ,
          lv_receiver LIKE  sy-subrc,
          lv_emailid   LIKE  somlreci1-receiver. " Email-id


  lv_mtitle              = p_mtitle.
  lv_format              = p_format.
  lv_attdescription      = p_attdescription.
  lv_attfilename         = p_filename.
  lv_sender_address      = p_sender_address.
  lv_sender_address_type =  p_sender_addres_type.

* Fill the document data.
  ls_doc_data-doc_size = 1.

* Populate the subject/generic message attributes
  ls_doc_data-obj_langu = sy-langu.
  ls_doc_data-obj_name  = 'SAPRPT'.
  ls_doc_data-obj_descr = lv_mtitle .
  ls_doc_data-sensitivty = 'F'.

* Fill the document data and get size of attachment
  CLEAR ls_doc_data.
  READ TABLE lt_attach INDEX lv_cnt.
  ls_doc_data-doc_size =
     ( lv_cnt - 1 ) * 255 + strlen( lt_attach ).
  ls_doc_data-obj_langu  = sy-langu.
  ls_doc_data-obj_name   = 'SAPRPT'.
  ls_doc_data-obj_descr  = lv_mtitle.
  ls_doc_data-sensitivty = 'F'.
  CLEAR lt_attachment.
  REFRESH lt_attachment.
  lt_attachment[] =  plt_attach[].

* Describe the body of the message
  CLEAR lt_packing_list.
  REFRESH lt_packing_list.
  lt_packing_list-transf_bin = space.
  lt_packing_list-head_start = 1.
  lt_packing_list-head_num = 0.
  lt_packing_list-body_start = 1.
  DESCRIBE TABLE lt_message LINES lt_packing_list-body_num.
  lt_packing_list-doc_type = 'RAW'.
  APPEND lt_packing_list.



* Create attachment notification
  lt_packing_list-transf_bin = 'X'.
  lt_packing_list-head_start = 1.
  lt_packing_list-head_num   = 1.
  lt_packing_list-body_start = 1.

  DESCRIBE TABLE lt_attachment LINES lt_packing_list-body_num.
  lt_packing_list-doc_type   =  lv_format.
  lt_packing_list-obj_descr  =  lv_attdescription.
  lt_packing_list-obj_name   =  lv_attfilename.
  lt_packing_list-doc_size   =  lt_packing_list-body_num * 255.
  APPEND lt_packing_list.


* email id


    lv_emailid = 'write2sapabap@gmail.com'.


* Add the recipients email address

  CLEAR lt_receivers.
  REFRESH lt_receivers.

  ls_receivers-receiver = lv_emailid.
  ls_receivers-rec_type = 'U'.
  ls_receivers-com_type = 'INT'.
  ls_receivers-notif_del = 'X'.
  ls_receivers-notif_ndel = 'X'.
  APPEND ls_receivers TO lt_receivers.



  CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
    EXPORTING
      document_data              = ls_doc_data
      put_in_outbox              = 'X'
      sender_address             = lv_sender_address
      sender_address_type        = lv_sender_address_type
      commit_work                = 'X'
    IMPORTING
      sent_to_all                = lv_sent_all
    TABLES
      packing_list               = lt_packing_list
      contents_bin               = lt_attachment
      contents_txt               = lt_message
      receivers                  = lt_receivers
    EXCEPTIONS
      too_many_receivers         = 1
      document_not_sent          = 2
      document_type_not_exist    = 3
      operation_no_authorization = 4
      parameter_error            = 5
      x_error                    = 6
      enqueue_error              = 7
      OTHERS                     = 8.

* Populate zerror return code
  lv_error = sy-subrc.

* Populate zreceiver return code
  LOOP AT lt_receivers.
    lv_receiver = lt_receivers-retrn_code.
  ENDLOOP.
  IF lv_receiver = 0 AND lv_error = 0  .
    MESSAGE 'Email sent successfully' TYPE 'S'.
  ENDIF.
ENDFORM.



  • Save-> Check->activate.
  • Click on Execute.

Input



Output

  • Click on Execute.



To check output

  • Go to Transaction SOST.


  • Mail  successfully triggering.
  • Select the row and Click on Display to check attachment.

  • Looks like,

  • To View EXCEL file click on excel file.





No comments:

Post a Comment