Saturday, 20 November 2021

Automatic closure of pending Purchase Requisition’s

This blog post is to show, how to close the open PR’s for a specified time and email attachment of excel will be sent to respective persons through background jobs. Background jobs are done by basis team.

To store mail ID’s, we are using ‘STVARVC’ T-Code. Because this T-Code will save the mail ID’s in the ‘TVARVC’ table.

Fetching stored mail ID’s from ‘TVARVC’ table and sending closed PR’s to appropriate mail’s.

Steps to create a STVARVC data.

Step:1

Go to T-Code STVARVC-> Edit mode -> Create -> provide name and mail ID.

Name – ZMM_AUTOPR

Mail ID – XXXXXXXXXXXX@gmail.com

SAP ABAP Tutorial and Materials, SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Certification, SAP ABAP Skills, SAP ABAP Jobs

Click on Save to save the parameters.

In the same way we can give multiple mail ID’s by using select options in selection option tab in STVARVC T-Code.

Below is the code to ensure the above explanation.

*&---------------------------------------------------------------------*
*&------------------------------------------------------------*
*& Program Title     : MM PR Auto Closure                     *
*& Description       : SAP Implementation - MM PR Auto Closure*
**------------------------------------------------------------*
*&---------------------------------------------------------------------*

type-POOLs: slis.
TABLES : eban.


*** Structure Declaration
TYPES: BEGIN OF ty_final,
       BANFN TYPE eban-banfn,
       BNFPO TYPE eban-bnfpo,
       MATNR TYPE eban-MATNR,
       WERKS TYPE eban-WERKS,
       MENGE TYPE eban-MENGE,
       MEINS TYPE eban-MEINS,
       status TYPE c LENGTH 20,
       END OF ty_final.

DATA : it_eban TYPE TABLE OF eban,
       it_eban1 TYPE TABLE OF eban.

*****   Final Internal Table

DATA: it_final TYPE TABLE OF ty_final,
      it_final1 TYPE TABLE OF ty_final,
      wa_final TYPE ty_final,
      wa_final1 TYPE ty_final.

*** Work Area Declaration
DATA : wa_eban TYPE eban,
       wa_eban1 TYPE eban,
       wa_item TYPE bapieband.

*** Local Variable Declaration
DATA : lv_banfn TYPE eban-banfn,
       lv_date TYPE sy-datum,
       lv_days TYPE i.

data: it_tvarvc TYPE TABLE OF tvarvc,
      it_tvarvc1 TYPE TABLE OF tvarvc,
      wa_tvarvc TYPE tvarvc,
      wa_tvarvc1 TYPE tvarvc.

DATA: it_fcat TYPE slis_t_fieldcat_alv,
      wa_fcat TYPE slis_fieldcat_alv.
DATA: pdate TYPE p0001-begda,
      cdate TYPE p0001-begda.
DATA: wa_layout TYPE slis_layout_alv.

""""""""""'''Excel Details
DATA: wa_addr TYPE bapiaddr3.
DATA: it_return TYPE TABLE OF bapiret2.

DATA: lv_date1 TYPE d.
DATA: lv_filename TYPE string.

TYPES: BEGIN OF xml_line,
        data(255) TYPE X,
       END OF xml_line.

DATA: l_ixml            TYPE REF TO if_ixml,
      l_streamfactory   TYPE REF TO if_ixml_stream_factory,
      l_ostream         TYPE REF TO if_ixml_ostream,
      l_renderer        TYPE REF TO if_ixml_renderer,
      l_document        TYPE REF TO if_ixml_document.

DATA: l_element_root        TYPE REF TO if_ixml_element,
      ns_attribute          TYPE REF TO if_ixml_attribute,
      r_element_properties  TYPE REF TO if_ixml_element,
      r_element             TYPE REF TO if_ixml_element,
      r_worksheet           TYPE REF TO if_ixml_element,
      r_table               TYPE REF TO if_ixml_element,
      r_column              TYPE REF TO if_ixml_element,
      r_row                 TYPE REF TO if_ixml_element,
      r_cell                TYPE REF TO if_ixml_element,
      r_data                TYPE REF TO if_ixml_element,
      l_value               TYPE string,
      l_type                TYPE string,
      l_text(100)           TYPE c,
      r_styles              TYPE REF TO if_ixml_element,
      r_style               TYPE REF TO if_ixml_element,
      r_style1              TYPE REF TO if_ixml_element,
      r_format              TYPE REF TO if_ixml_element,
      r_border              TYPE REF TO if_ixml_element,
      num_rows              TYPE i.

DATA: l_xml_table       TYPE TABLE OF xml_line,
      wa_xml            TYPE xml_line,
      l_xml_size        TYPE i,
      l_rc              TYPE i.

********Mail Details
  DATA: objpack   LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE.
  DATA: objhead   LIKE solisti1 OCCURS 1 WITH HEADER LINE.
  DATA: objbin    LIKE solix OCCURS 10 WITH HEADER LINE.
  DATA: objtxt    LIKE solisti1 OCCURS 10 WITH HEADER LINE.
  DATA: reclist   LIKE somlreci1 OCCURS 5 WITH HEADER LINE.
  DATA: doc_chng  LIKE sodocchgi1.
  DATA: tab_lines LIKE sy-tabix.
  DATA: l_num(3).
  DATA: subj_date(10) TYPE c.


SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
     SELECT-OPTIONS : s_banfn FOR eban-banfn,
                      s_bnfpo FOR eban-bnfpo,
                      s_werks FOR eban-werks,
                      s_bsart FOR eban-bsart.

*    parameters:         p_date type sy-datum DEFAULT sy-datum OBLIGATORY.
     SELECT-OPTIONS:   p_ernam FOR eban-ernam." no-EXTENSION." OBLIGATORY.
 SELECT-OPTIONS:         p_date for sy-datum .

*
   SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN: BEGIN OF BLOCK b2 WITH FRAME .
  PARAMETERS: p_rb1 RADIOBUTTON GROUP rb1,
              p_rb2 RADIOBUTTON GROUP rb1.
 SELECTION-SCREEN: END OF BLOCK b2.


* INITIALIZATION.
INITIALIZATION.

CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
  EXPORTING
    date            = sy-datum
    days            = 0
    months          = 12
   SIGNUM          = '-'
    years           = 0
 IMPORTING
   CALC_DATE       = pdate
          .

  p_date-sign = 'I'.
  p_date-option = 'EQ'.
  p_date-low = pdate. "'20200101'."'20191201' .
  p_date-high = sy-datum.
  APPEND p_date.


**S_BADAT-SIGN = 'I'.
**S_BADAT-OPTION = 'EQ'.
**S_BADAT-LOW = '20150401'."'20191201' .
**S_BADAT-HIGH = SY-DATUM.
**APPEND S_BADAT.

START-OF-SELECTION.


if p_rb1 = 'X'.

  PERFORM get_data.
  PERFORM build_fieldcat_alv.
  PERFORM display_fieldcat_alv.

 endif.

 if p_rb2 = 'X'.

   PERFORM get_data.

   SELECT * FROM tvarvc INTO table it_tvarvc where name = 'ZMM_AUTOPR_MAIL'.

     IF sy-subrc eq 0.

LOOP AT it_tvarvc INTO wa_tvarvc.

    LOOP AT IT_FINAL INTO WA_FINAL .

    APPEND wa_FINAL TO IT_FINAL1.
    CLEAR: WA_FINAL1.
    CLEAR: WA_final.
      ENDLOOP.

 PERFORM PROCESS_EXCEL.
 PERFORM MAIL.

 REFRESH :it_final1.
 CLEAR: wa_tvarvc..
 endloop.

  ENDIF.
  endif.


*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_DATA .
selecting the mail ID’s
SELECT SINGLE * from tvarvc INTO wa_tvarvc WHERE name = 'ZMM_AUTOPR'. “ 

lv_days = wa_tvarvc-low.


CALL FUNCTION 'BKK_ADD_WORKINGDAY'
  EXPORTING
    i_date            = p_date-high
    i_days            = lv_days"
    i_calendar1       = 'SR'
 IMPORTING
    e_date            = lv_date.


SELECT * FROM eban INTO TABLE it_eban
         WHERE banfn IN s_banfn
         AND   werks in s_werks
*         AND   badat LE lv_date
         AND   badat BETWEEN p_date-low AND lv_date
         AND   bsart in s_bsart
*         AND   ernam = p_ernam
         AND   ernam in p_ernam
         AND   ebakz NE 'X'.

SORT it_eban ASCENDING BY banfn.

it_eban1 = it_eban.

LOOP AT it_eban INTO wa_eban.

READ TABLE it_eban1 INTO wa_eban1 WITH KEY BANFN = wa_eban-BANFN
                                         BNFPO = wa_eban-BNFPO.
   if wa_eban1-EBAKZ is INITIAL.

        wa_eban1-EBAKZ = 'X'.
        Modify eban FROM wa_eban1.
   CLEAR : wa_item.
 endif.
endloop.

LOOP AT it_eban INTO wa_eban.

       wa_final-BANFN   =  wa_eban-banfn.
       wa_final-BNFPO   =  wa_eban-bnfpo.
       SHIFT wa_final-BNFPO  LEFT DELETING LEADING '0'.
       wa_final-MATNR   =  wa_eban-matnr.
       SHIFT wa_final-MATNR  LEFT DELETING LEADING '0'.
       wa_final-WERKS   =  wa_eban-werks.
       wa_final-MENGE   =  wa_eban-menge.
       wa_final-MEINS   =  wa_eban-meins.
       wa_final-status  = 'PR Closed'.
   append wa_final to it_final.
   CLEAR: wa_final.

ENDLOOP.
ENDFORM.
FORM BUILD_FIELDCAT_ALV .

 wa_layout-colwidth_optimize = 'X'.

  wa_fcat-fieldname = 'BANFN'.
  wa_fcat-tabname = 'IT_FINAL'.
  wa_fcat-no_zero = 'X'.
  wa_fcat-col_pos = 1.
  wa_fcat-seltext_m = 'Purchase Reqion No.'.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-fieldname = 'BNFPO'.
  wa_fcat-tabname = 'IT_FINAL'.
  wa_fcat-col_pos = 2.
  wa_fcat-seltext_l = 'Requsion Item'.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-fieldname = 'MATNR'.
  wa_fcat-tabname = 'IT_FINAL'.
  wa_fcat-col_pos = 3.
  wa_fcat-seltext_s = 'Material'.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-fieldname = 'WERKS'.
  wa_fcat-tabname = 'IT_FINAL'.
  wa_fcat-col_pos = 4.
  wa_fcat-seltext_m = 'Plant'.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-fieldname = 'MENGE'.
  wa_fcat-tabname = 'IT_FINAL'.
  wa_fcat-col_pos = 5.
  wa_fcat-seltext_m = 'Quantity'.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-fieldname = 'MEINS'.
  wa_fcat-tabname = 'IT_FINAL'.
  wa_fcat-col_pos = 6.
  wa_fcat-seltext_m = 'UoM'.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-fieldname = 'STATUS'.
  wa_fcat-tabname = 'IT_FINAL'.
  wa_fcat-col_pos = 7.
  wa_fcat-seltext_m = 'Status'.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

ENDFORM.

FORM DISPLAY_FIELDCAT_ALV .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program      = sy-cprog
*      i_callback_user_command = 'USERCOMMAND'
      is_layout               = wa_layout
      it_fieldcat             = it_fcat
*      it_events               = it_events
    TABLES
      t_outtab                = it_final
    .
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.
*MESSAGE 'Above shown PRs are Closed' TYPE 'S'.
ENDFORM.

Creating the Excel
FORM PROCESS_EXCEL .

*  Creating a ixml Factory
  l_ixml = cl_ixml=>create( ).

* Creating the DOM Object Model
  l_document = l_ixml->create_document( ).

* Create Root Node 'Workbook'
  l_element_root  = l_document->create_simple_element( name = 'Workbook'  parent = l_document ).
  l_element_root->set_attribute( name = 'xmlns'  value = 'urn:schemas-microsoft-com:office:spreadsheet' ).

  ns_attribute = l_document->create_namespace_decl( name = 'ss'  prefix = 'xmlns'  uri = 'urn:schemas-microsoft-com:office:spreadsheet' ).
  l_element_root->set_attribute_node( ns_attribute ).

  ns_attribute = l_document->create_namespace_decl( name = 'x'  prefix = 'xmlns'  uri = 'urn:schemas-microsoft-com:office:excel' ).
  l_element_root->set_attribute_node( ns_attribute ).

* Create node for document properties.
  r_element_properties = l_document->create_simple_element( name = 'TEST_REPORT'  parent = l_element_root ).
  l_value = sy-uname.
  l_document->create_simple_element( name = 'Author'  value = l_value  parent = r_element_properties  ).

* Styles
  r_styles = l_document->create_simple_element( name = 'Styles'  parent = l_element_root  ).

* Style for Header
  r_style  = l_document->create_simple_element( name = 'Style'   parent = r_styles  ).
  r_style->set_attribute_ns( name = 'ID'  prefix = 'ss'  value = 'Header' ).

  r_format  = l_document->create_simple_element( name = 'Font'  parent = r_style  ).
  r_format->set_attribute_ns( name = 'Bold'  prefix = 'ss'  value = '1' ).

  r_format  = l_document->create_simple_element( name = 'Interior' parent = r_style  ).
  r_format->set_attribute_ns( name = 'Color'   prefix = 'ss'  value = '#92D050' ).
  r_format->set_attribute_ns( name = 'Pattern' prefix = 'ss'  value = 'Solid' ).

  r_format  = l_document->create_simple_element( name = 'Alignment'  parent = r_style  ).
  r_format->set_attribute_ns( name = 'Vertical'  prefix = 'ss'  value = 'Center' ).
  r_format->set_attribute_ns( name = 'WrapText'  prefix = 'ss'  value = '1' ).

  r_border  = l_document->create_simple_element( name = 'Borders'  parent = r_style ).
  r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
  r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Bottom' ).
  r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
  r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

  r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
  r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Left' ).
  r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
  r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

  r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
  r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Top' ).
  r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
  r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

  r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
  r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Right' ).
  r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
  r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

* Style for Data
  r_style1  = l_document->create_simple_element( name = 'Style'   parent = r_styles  ).
  r_style1->set_attribute_ns( name = 'ID'  prefix = 'ss'  value = 'Data' ).

  r_border  = l_document->create_simple_element( name = 'Borders'  parent = r_style1 ).
  r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
  r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Bottom' ).
  r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
  r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

  r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
  r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Left' ).
  r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
  r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

  r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
  r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Top' ).
  r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
  r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

  r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
  r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Right' ).
  r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
  r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

*  r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
*  r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Right' ).
*  r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
*  r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).


* Worksheet
  r_worksheet = l_document->create_simple_element( name = 'Worksheet'  parent = l_element_root ).
  r_worksheet->set_attribute_ns( name = 'Name'  prefix = 'ss'  value = 'Sheet1' ).

* Table
  r_table = l_document->create_simple_element( name = 'Table'  parent = r_worksheet ).
  r_table->set_attribute_ns( name = 'FullColumns'  prefix = 'x'  value = '1' ).
  r_table->set_attribute_ns( name = 'FullRows'     prefix = 'x'  value = '1' ).

* Column Formatting
  r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
  r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '90' ).

  r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
  r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '90' ).

  r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
  r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '90' ).

  r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
  r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '90' ).

  r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
  r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '90' ).

  r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
  r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '90' ).

    r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
  r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '90' ).


* Blank Row
  r_row = l_document->create_simple_element( name = 'Row'  parent = r_table ).

* Column Headers Row
  r_row = l_document->create_simple_element( name = 'Row'  parent = r_table ).
  r_row->set_attribute_ns( name = 'AutoFitHeight'  prefix = 'ss'  value = '1' ).

* Purchase req Number
  r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
  r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Header' ).
  r_data = l_document->create_simple_element( name = 'Data'  value = 'Purchase Req.'  parent = r_cell ).
  r_data->set_attribute_ns( name = 'Type'  prefix = 'ss' value = 'String' ).

* Requisition Item
  r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
  r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Header' ).
  r_data = l_document->create_simple_element( name = 'Data'  value = 'Requisn Item'  parent = r_cell ).
  r_data->set_attribute_ns( name = 'Type'  prefix = 'ss' value = 'String' ).

* Mantr
  r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
  r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Header' ).
  r_data = l_document->create_simple_element( name = 'Data'  value = 'Material'  parent = r_cell ).
  r_data->set_attribute_ns( name = 'Type'  prefix = 'ss' value = 'String' ).

* Plant
  r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
  r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Header' ).
  r_data = l_document->create_simple_element( name = 'Data'  value = 'Plant'  parent = r_cell ).
  r_data->set_attribute_ns( name = 'Type'  prefix = 'ss' value = 'String' ).

* Quantity
  r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
  r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Header' ).
  r_data = l_document->create_simple_element( name = 'Data'  value = 'Quantity'  parent = r_cell ).
  r_data->set_attribute_ns( name = 'Type'  prefix = 'ss' value = 'String' ).

* UoM
  r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
  r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Header' ).
  r_data = l_document->create_simple_element( name = 'Data'  value = 'UoM'  parent = r_cell ).
  r_data->set_attribute_ns( name = 'Type'  prefix = 'ss' value = 'String' ).

*  Status
   r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
  r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Header' ).
  r_data = l_document->create_simple_element( name = 'Data'  value = 'Status'  parent = r_cell ).
  r_data->set_attribute_ns( name = 'Type'  prefix = 'ss' value = 'String' ).


  LOOP AT it_final1 INTO wa_final1.

    r_row = l_document->create_simple_element( name = 'Row'  parent = r_table ).

                          " Cell format

* Purchase Req.
    r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
    r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Data' ).
    l_value = wa_final1-BANFN.
    r_data = l_document->create_simple_element( name = 'Data'  value = l_value   parent = r_cell ).           " Data
    r_data->set_attribute_ns( name = 'Type'  prefix = 'ss'  value = 'String' ).                               " Cell format

*  Requisition Item
    r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
    r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Data' ).
    l_value = wa_final1-BNFPO.
    r_data = l_document->create_simple_element( name = 'Data'  value = l_value   parent = r_cell ).           " Data
    r_data->set_attribute_ns( name = 'Type'  prefix = 'ss'  value = 'String' ).                               " Cell format

* Matnr
    r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
    r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Data' ).
    l_value = wa_final1-MATNR.
    r_data = l_document->create_simple_element( name = 'Data'  value = l_value   parent = r_cell ).           " Data
    r_data->set_attribute_ns( name = 'Type'  prefix = 'ss'  value = 'String' ).                               " Cell format

* WERKS
    r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
    r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Data' ).
    l_value = wa_final1-WERKS.
    r_data = l_document->create_simple_element( name = 'Data'  value = l_value   parent = r_cell ).          " Data
    r_data->set_attribute_ns( name = 'Type'  prefix = 'ss'  value = 'String' ).


* MENGE
    r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
    r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Data' ).
    l_value = wa_final1-menge.
    r_data = l_document->create_simple_element( name = 'Data'  value = l_value   parent = r_cell ).          " Data
    r_data->set_attribute_ns( name = 'Type'  prefix = 'ss'  value = 'String' ).

* MEINS
    r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
    r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Data' ).
    l_value = wa_final1-meins.
    r_data = l_document->create_simple_element( name = 'Data'  value = l_value   parent = r_cell ).          " Data
    r_data->set_attribute_ns( name = 'Type'  prefix = 'ss'  value = 'String' ).

*  Status
     r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
    r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Data' ).
    l_value = wa_final1-status.
    r_data = l_document->create_simple_element( name = 'Data'  value = l_value   parent = r_cell ).          " Data
    r_data->set_attribute_ns( name = 'Type'  prefix = 'ss'  value = 'String' ).


    CLEAR: wa_final1.

  ENDLOOP.

* Creating a Stream Factory
  l_streamfactory = l_ixml->create_stream_factory( ).

* Connect Internal XML Table to Stream Factory
  l_ostream = l_streamfactory->create_ostream_itable( table = l_xml_table ).

* Rendering the Document
  l_renderer = l_ixml->create_renderer( ostream  = l_ostream  document = l_document ).
  l_rc = l_renderer->render( ).

* Saving the XML Document
  l_xml_size = l_ostream->get_num_written_raw( ).

ENDFORM.

FORM build_body_of_mail  USING    l_message.

  objtxt-line = l_message.
  APPEND objtxt.
  CLEAR  objtxt.

ENDFORM.                    " BUILD_BODY_OF_MAIL
.
Sending mail
FORM MAIL .
""added"""""""""""""""""
CLEAR: lv_date, objtxt, objpack , reclist, objbin, objhead.

  lv_date = sy-datum.

* Mail Subject
  CONCATENATE lv_date+6(2) '-' lv_date+4(2) '-' lv_date+0(4) INTO subj_date.
  CONCATENATE 'AUTO PR CLOSE' subj_date INTO doc_chng-obj_descr SEPARATED BY space.

* Mail Contents
  objtxt = 'Dear User,'.
  APPEND objtxt.

  CLEAR objtxt.
  APPEND objtxt.

  CONCATENATE 'Please find the attached file with Auto Open PR Close' subj_date INTO objtxt SEPARATED BY space.              " Mail Contents
  APPEND objtxt.

  CLEAR objtxt.
  APPEND objtxt.

  DESCRIBE TABLE objtxt LINES tab_lines.
  READ TABLE objtxt INDEX tab_lines.
  doc_chng-doc_size = ( tab_lines - 1 ) * 255 + strlen( objtxt ).

* Packing List For the E-mail Body
  objpack-head_start = 1.
  objpack-head_num   = 0.
  objpack-body_start = 1.
  objpack-body_num   = tab_lines.
  objpack-doc_type   = 'RAW'.
  APPEND objpack.

* Creation of the Document Attachment
  LOOP AT l_xml_table INTO wa_xml.
    CLEAR objbin.
    objbin-line = wa_xml-data.
    APPEND objbin.
  ENDLOOP.

  DESCRIBE TABLE objbin LINES tab_lines.
  objhead = 'AUTO PR CLOSE'.
  APPEND objhead.

* Packing List For the E-mail Attachment
  objpack-transf_bin = 'X'.
  objpack-head_start = 1.
  objpack-head_num   = 0.
  objpack-body_start = 1.
  objpack-body_num = tab_lines.
  CONCATENATE '' subj_date INTO objpack-obj_descr SEPARATED BY space.
  objpack-doc_type = 'XLS'.
  objpack-doc_size = tab_lines * 255.
  APPEND objpack.

reclist-receiver = wa_tvarvc-low.
    reclist-rec_type = 'U'.
    APPEND reclist.
    CLEAR reclist.


* Sending the document
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = doc_chng
      put_in_outbox              = 'X'
    TABLES
      packing_list               = objpack
      object_header              = objhead
      contents_txt               = objtxt
      contents_hex               = objbin
      receivers                  = reclist
    EXCEPTIONS
      too_many_receivers         = 1
      document_not_sent          = 2
      operation_no_authorization = 4
      OTHERS                     = 99.

  IF sy-subrc EQ 0.
    COMMIT WORK.
    MESSAGE 'Mail has been Successfully Sent.' TYPE 'S'.
  ENDIF.

REFRESH: objpack, objhead , objtxt, objbin , reclist .  """"""""""Added""""""""
ENDFORM.

Once the report is executed mail will be sent to the users and we can check in ‘SOST’ T-Code

Selection Screen for ALV Output

SAP ABAP Tutorial and Materials, SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Certification, SAP ABAP Skills, SAP ABAP Jobs

Report Output

SAP ABAP Tutorial and Materials, SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Certification, SAP ABAP Skills, SAP ABAP Jobs

Selection Screen for Mail Sending

SAP ABAP Tutorial and Materials, SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Certification, SAP ABAP Skills, SAP ABAP Jobs

Mail has sent successfully, we can check in SOST T-Code.

Go to SOST T-Code -> select your mail -> click on Display Document

SAP ABAP Tutorial and Materials, SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Certification, SAP ABAP Skills, SAP ABAP Jobs

Mail attachment.

SAP ABAP Tutorial and Materials, SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Certification, SAP ABAP Skills, SAP ABAP Jobs

Double click on attachment to see the attachment data.

Source: sap.com

No comments:

Post a Comment