Attach Purchase Order as PDF and view the same in the transaction ME23

Click here to go to main page

REPORT  z490_me23_smartforms_pdf.

**&------- Data Declaration ---------------------------------------------------------------------*
DATAcontrol   TYPE ssfctrlop,                              " Smart Forms: Control structure
      joboutput 
TYPE ssfcrescl,                              " Smart Forms: Return value at end of form printing
      it_doc    
LIKE TABLE OF docs,                          " Stored document
      otf       
LIKE TABLE OF itcoo,                         " OTF Structure
      bin_size  
TYPE i,                                      " Size of file converting into PDF
      it_lines  
LIKE TABLE OF tline,                         " Text Lines

      fm_name   
TYPE rs38l_fnam,                             " Function Module for Smartform

      lt_ekpo   
LIKE TABLE OF z1018_ekpo_stu,                " Internal Table for Header & Items
      lt_ekko   
LIKE z1018_ekko_stu,

      
header    LIKE  bapimepoheader,                        " Structure to hold PO Header Data (BAPI)
      headerx   
LIKE  bapimepoheaderx,
      lt_item   
LIKE  TABLE OF bapimepoitem,                 " Internal Table to hold PO Item Data (BAPI)
      ls_item   
LIKE LINE OF lt_item,                        " Structure to hold PO Item Data (BAPI)
      lt_itemx  
LIKE  TABLE OF bapimepoitemx,
      ls_itemx  
LIKE LINE OF lt_itemx,
      
return    LIKE bapiret2 OCCURS 0 WITH HEADER LINE,     " Internal Table to hold Messages from BAPI Call
      w_header  
TYPE ekko-ebeln.                             " To store the Purchase Order Number
*---------------------------------------------------------------------------------------------------------*
**&------- Screen Declaration  ---------------------------------------------------------------------------*
SELECTION-
SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS : doctyp  LIKE header-doc_type   DEFAULT 'NB'        ,
             cdate   
LIKE header-creat_date DEFAULT sy-datum    ,
             vendor  
LIKE header-vendor     DEFAULT '1000',
             pur_org 
LIKE header-purch_org  DEFAULT '1000'      ,
             pur_grp 
LIKE header-pur_group  DEFAULT '001'       .
SELECTION-
SCREEN END OF BLOCK b1.
SELECTION-
SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
PARAMETERS : item_num LIKE ls_item-po_item  DEFAULT '00010',
             material 
LIKE ls_item-material DEFAULT '100-100'   ,
             plant    
LIKE ls_item-plant    DEFAULT '1000' ,
             quantity 
LIKE ls_item-quantity DEFAULT '55'.
SELECTION-
SCREEN END OF BLOCK b2.
**&------- Screen Events  -------------------------------------------------------------------------------*
AT SELECTION-SCREEN.
  
LEAVE TO LIST-PROCESSING.
  
SET PF-STATUS space.

START-
OF-SELECTION.
  
PERFORM bapipo.                                                   " Subroutine for BAPI
  
PERFORM smartforms.                                               " Subroutine for Smartforms
  
PERFORM file_attach.                                              " Subroutine for File Attachment
*---------------------------------------------------------------------------------------------------------*
  
CALL TRANSACTION 'ME23'.
*---------------------------------------------------------------------------------------------------------*
FORM bapipo.
**&-------------------------------------------------------------------------------------------------------*
**Data Population for Header
**&-------------------------------------------------------------------------------------------------------*
  
header-doc_type   = doctyp.
  
header-creat_date = cdate.
  
header-vendor     = vendor.
  
header-purch_org  = pur_org.
  
header-pur_group  = pur_grp.
**&-------------------------------------------------------------------------------------------------------*
**Populate for Header Flag
**&-------------------------------------------------------------------------------------------------------*
  headerx-doc_type   = 
'X'.
  headerx-creat_date = 
'X'.
  headerx-vendor     = 
'X'.
  headerx-purch_org  = 
'X'.
  headerx-pur_group  = 
'X'.
**&-------------------------------------------------------------------------------------------------------*
**Data Population for Item
**&-------------------------------------------------------------------------------------------------------*
  ls_item-po_item  = item_num.
  ls_item-material = material.
  ls_item-plant    = plant.
  ls_item-quantity = quantity.
  
APPEND ls_item TO lt_item.
**&-------------------------------------------------------------------------------------------------------*
**Populate for Item Flag
**&-------------------------------------------------------------------------------------------------------*
  ls_itemx-po_item    = item_num.
  ls_itemx-material   = 
'X'.
  ls_itemx-plant      = 
'X'.
  ls_itemx-quantity   = 
'X'.
  
APPEND ls_itemx TO lt_itemx.
**&-------------------------------------------------------------------------------------------------------*
**BAPI Call through the Function Module
**&-------------------------------------------------------------------------------------------------------*
  
CALL FUNCTION 'BAPI_PO_CREATE1'
    
EXPORTING
      poheader         = 
header
      poheaderx        = headerx
    
IMPORTING
      exppurchaseorder = w_header
    
TABLES
      
return           = return
      poitem           = lt_item
      poitemx          = lt_itemx.
**&-------------------------------------------------------------------------------------------------------*
**Confirm the Document Creation by Calling Database COMMIT
**&-------------------------------------------------------------------------------------------------------*
  
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    
EXPORTING
      
wait = 'X'.
  
LEAVE TO LIST-PROCESSING.
  
IF sy-subrc <> 0.
    
WRITE:/ 'Purchase Order Number is', w_header.
  
ELSE.
    
LOOP AT return.                                               " Output: Messages Returned from BAPI call
      
WRITE / return-message.
    
ENDLOOP.
  
ENDIF.
ENDFORM.                    "bapipo
*&---------------------------------------------------------------------------------------------------------*
*&      Form  SMARTFORMS
*&---------------------------------------------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------------------------------------------*
FORM smartforms .
  
SELECT SINGLE * FROM ekko INTO CORRESPONDING FIELDS OF lt_ekko WHERE ekko~ebeln = w_header.  " Fetches the Data
  
SELECT * FROM ekpo INTO CORRESPONDING FIELDS OF TABLE lt_ekpo WHERE ekpo~ebeln = w_header.
  
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'                      " Call the Function Module for Smartforms
    
EXPORTING
      formname            = 
'Z490_PO'
 
IMPORTING
     fm_name              =  fm_name
 
EXCEPTIONS
   no_form                = 
1
   no_function_module     = 
2
   
OTHERS                 = 3.
  
IF sy-subrc <> 0.
    
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  
ENDIF.

  
control-no_dialog       = 'X'.
  
control-preview         = 'X'.
  
control-getotf          = 'X'.

  
CALL FUNCTION  fm_name                                        " Call the Function Module for '/1BCDWB/SF00001144'
   
EXPORTING
      control_parameters  = 
control
      lt_ekko             = lt_ekko
   
IMPORTING
     job_output_info      = joboutput
    
TABLES
     lt_ekpo              = lt_ekpo
   
EXCEPTIONS
     formatting_error     = 
1
     internal_error       = 
2
     send_error           = 
3
     user_canceled        = 
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.
  
ENDIF.

  
CALL FUNCTION 'CONVERT_OTF_2_PDF'                         " Call the Function Module for OTF Conversion
     
IMPORTING
       bin_filesize   = bin_size
     
TABLES
       otf            = joboutput-otfdata[]
       doctab_archive = it_doc
       
lines          = it_lines.

ENDFORM.                    " SMARTFORMS
*&--------------------------------------------------------------------------------------------------------*
*&      Form  FILE_ATTACH
*&--------------------------------------------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*---------------------------------------------------------------------------------------------------------*
FORM file_attach .
**&--------  Local Data Declaration  ---------------------------------------------------------------------*
  
DATA: ls_fol_id   TYPE soodk,
        ls_obj_id   
TYPE soodk,
        ls_obj_data 
TYPE sood1,
        ls_folmem_k 
TYPE sofmk,
        ls_note     
TYPE borident,
        ls_object   
TYPE borident,
        lv_ep_note  
TYPE borident-objkey,
        lv_offset   
TYPE i,
        l_subrc     
TYPE subrc,

        it_objhead  
TYPE STANDARD TABLE OF soli,
        it_content  
LIKE STANDARD TABLE OF soli,
        wa_content  
LIKE soli,

        p_key      
TYPE swo_typeid ,                   " Invoice Number 9000000028
        p_type     
TYPE swo_objtyp ,                        " BUS2037
*       p_file     TYPE c LENGTH 100 ,                 " D:\usr\sap\DE1\DVEBMGS11\work\5010\9000000028_0003000155.PDF
        p_desc     
TYPE so_obj_des .                   " Test pdf documents

  
CONCATENATE  'Purchase Order' w_header INTO p_desc.
  p_key = w_header.
  p_type = 
'BUS2012'.

  ls_object-objkey  = p_key.
  ls_object-objtype = p_type.

  
CALL FUNCTION 'SX_TABLE_LINE_WIDTH_CHANGE'      " Call Function Module for Change the Table Line Width
    
TABLES
      content_in                  = it_lines
      content_out                 = it_content
    
EXCEPTIONS
      err_line_width_src_too_long = 
1
      err_line_width_dst_too_long = 
2
      err_conv_failed             = 
3
      
OTHERS                      = 4.
  
IF sy-subrc <> 0.
    
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  
ENDIF.

  
CHECK l_subrc IS INITIAL.

  
CALL FUNCTION 'SO_CONVERT_CONTENTS_BIN'       " Call the Function Module for Converts into Bin
    
EXPORTING
      it_contents_bin = it_content[]
    
IMPORTING
      et_contents_bin = it_content[].

  
CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET'         " Call the Function Module to get the Folder Id
    
EXPORTING
      region    = 
'B'
    
IMPORTING
      folder_id = ls_fol_id
    
EXCEPTIONS
      
OTHERS    = 1.

  ls_obj_data-objsns    = 
'O'.
  ls_obj_data-objla     = sy-langu.
  ls_obj_data-objdes    = p_desc.
* lv_offset             = STRLEN( p_file ) - 3.
  ls_obj_data-file_ext  = 
'PDF'.
  ls_obj_data-objlen    = 
LINES( it_content ) * 255.

  
CALL FUNCTION 'SO_OBJECT_INSERT'
    
EXPORTING
      folder_id             = ls_fol_id
      object_type           = 
'EXT'
      object_hd_change      = ls_obj_data
    
IMPORTING
      object_id             = ls_obj_id
    
TABLES
      objhead               = it_objhead
      objcont               = it_content
    
EXCEPTIONS
      active_user_not_exist = 
35
      folder_not_exist      = 
6
      object_type_not_exist = 
17
      owner_not_exist       = 
22
      parameter_error       = 
23
      
OTHERS                = 1000.

  
IF sy-subrc = 0 AND ls_object-objkey IS NOT INITIAL.
    ls_folmem_k-foltp = ls_fol_id-objtp.
    ls_folmem_k-folyr = ls_fol_id-objyr.
    ls_folmem_k-folno = ls_fol_id-objno.
    ls_folmem_k-doctp = ls_obj_id-objtp.
    ls_folmem_k-docyr = ls_obj_id-objyr.
    ls_folmem_k-docno = ls_obj_id-objno.
    lv_ep_note        = ls_folmem_k.
    ls_note-objtype   = 
'MESSAGE'.
    ls_note-objkey    = lv_ep_note.

    
CALL FUNCTION 'BINARY_RELATION_CREATE_COMMIT'  " Call the Function Module For Commit the Object
      
EXPORTING
        obj_rolea    = ls_object
        obj_roleb    = ls_note
        relationtype = 
'ATTA'
      
EXCEPTIONS
        
OTHERS       = 1.
  
ELSE.
    
MESSAGE 'Not OK' TYPE 'I'.
    
RETURN.
  
ENDIF.
  
IF sy-subrc = 0.
    
MESSAGE 'File Attachment Successfull' TYPE 'I'.
  
ELSE.
    
MESSAGE 'Error in File Attachment' TYPE 'E'.
  
ENDIF.
ENDFORM.                    " FILE_ATTACH

*&--------------------------------------------------------------------------------------------------------*

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image