Get PO Generic Object Services (GOS) attachment from DMS and attach via email

Notes :


Uploaded documents to be attached in the PO approval mail


Every time when the mail is sent from one release level to the other, It should go with attachment, attached in purchase order.


In Purchase order change screen ME22N, Purchase order has the option of attaching the documents of any type with the PO's, Once attached it should trigger a mail with a attachment as in Purchase order, It should also have the facility to select attachment which they want to send along with PO and which they do not want to send. 

---------------------------------------------------------------------------------

  DATA it_toa01 TYPE TABLE OF toa01,
         wa_toa01 
TYPE toa01.

  
DATA it_toaat TYPE TABLE OF toaat,
         wa_toaat 
TYPE toaat.

  
DATA l_i_tbl1024       TYPE STANDARD TABLE OF tbl1024,
         l_v_length        
TYPE syindex,
         pdf_content       
TYPE solix_tab,
         gte_solix         
TYPE solix_tab,
         gv_attype         
TYPE soodk-objtp,
         gv_atsub          
TYPE sood-objdes,
         l_size            
TYPE sood-objlen,          " Size of Attachment
         l_lines           
TYPE i,                    " Lines count
         lv_addr           
TYPE adr6-smtp_addr,
         lo_bcs_exception  
TYPE REF TO  cx_bcs,
         send_req          
TYPE REF TO  cx_send_req_bcs,
         ref_msg           
TYPE REF TO  cx_document_bcs,
         ref_addr          
TYPE REF TO  cx_address_bcs,
         document          
TYPE REF TO  cl_document_bcs,
         go_document       
TYPE REF TO  cl_document_bcs,
         lo_recipient      
TYPE REF TO  if_recipient_bcs,
         go_send_request   
TYPE REF TO  cl_bcs,
         lv_sent_to_all    
TYPE os_boolean,
         it_contents       
TYPE STANDARD TABLE OF solisti1,
         wa_contents       
TYPE solisti1,
         lv_str            
TYPE xstring,
         l_v_cdod          
TYPE so_obj_des,
         l_v_length1       
TYPE so_obj_len.


  
TYPES t_fieldcat         TYPE slis_fieldcat_alv.

  
DATA i_fieldcat         TYPE STANDARD TABLE OF t_fieldcat,
         w_fieldcat         
TYPE t_fieldcat.

  
DATA lv_frgzu           TYPE frgzu.  "PO Release status
  
DATA lv_count(2)        TYPE n.      

  
TYPES :  BEGIN OF ty_popup,
           
checkbox TYPE c,
           filename 
TYPE toaat-filename,
           
END OF ty_popup.

  
DATA it_popup TYPE TABLE OF ty_popup,
         wa_popup 
TYPE ty_popup.

  
DATA it_poatta TYPE STANDARD TABLE OF zpoatta,
         wa_poatta 
TYPE zpoatta.

  
PARAMETERS    :p_ebeln    TYPE ekko-ebeln.         " Purchasing Document No

  
SELECT FROM toa01
         
INTO TABLE it_toa01
         
WHERE object_id p_ebeln AND sap_object 'BUS2012'.

  
IF it_toa01 IS NOT INITIAL.
    
SELECT FROM toaat
           
INTO TABLE it_toaat
           
FOR ALL ENTRIES IN it_toa01
           
WHERE arc_doc_id it_toa01-arc_doc_id.
  
ENDIF.
  
LOOP AT it_objtxt INTO wa_objtxt.
    wa_contents
-line wa_objtxt-line.
    
APPEND wa_contents TO it_contents.
  
ENDLOOP.
**  PO ATTACHMENT POPUP **
  
SELECT SINGLE frgzu
                
FROM ekko
                
INTO lv_frgzu
                
WHERE ebeln p_ebeln.

  
IF lv_frgzu IS INITIAL.

    
DELETE FROM zpoatta WHERE ebeln p_ebeln.

    
IF it_toaat IS NOT INITIAL.
      
LOOP AT it_toaat INTO wa_toaat.
        wa_popup
-checkbox    'X'.
        wa_popup
-filename wa_toaat-filename.
        
APPEND wa_popup TO it_popup.
      
ENDLOOP.

      w_fieldcat
-fieldname 'CHECKBOX'.
      w_fieldcat
-tabname   'IT_POPUP'.
      w_fieldcat
-seltext_m 'CHECK BOX'.
      w_fieldcat
-outputlen 9.
      
APPEND w_fieldcat TO i_fieldcat.
      
CLEAR w_fieldcat.

      w_fieldcat
-fieldname 'FILENAME'.
      w_fieldcat
-tabname   'IT_POPUP'.
      w_fieldcat
-seltext_m 'FILE NAME'.
      w_fieldcat
-outputlen 60.
      
APPEND w_fieldcat TO i_fieldcat.
      
CLEAR w_fieldcat.

      
CALL FUNCTION 'REUSE_ALV_POPUP_TO_SELECT'
        
EXPORTING
          i_title              
'Po Attached Document List'
          i_tabname            
'IT_POPUP'
          i_checkbox_fieldname 
'CHECKBOX' "Checkbox field defined in the internal table
          it_fieldcat          
i_fieldcat
          i_callback_program   
sy-repid
        
TABLES
          t_outtab             
it_popup.

      
REFRESH it_poatta.
      
CLEAR   lv_count.
      
LOOP AT it_popup INTO wa_popup WHERE checkbox 'X'.
        lv_count 
lv_count + 1.
        wa_poatta
-ebeln    p_ebeln.
        wa_poatta
-filename wa_popup-filename.
        
CONCATENATE lv_count '_' wa_poatta-filename INTO wa_poatta-filename.  "To avoid same attachment name with in a mail
        
APPEND wa_poatta TO it_poatta.
        
CLEAR wa_poatta.
      
ENDLOOP.
      
IF  it_poatta IS NOT INITIAL.
        
INSERT zpoatta FROM TABLE it_poatta.
      
ENDIF.
    
ENDIF.
  
ELSE.   "ELSE Part of LV_FRGZU PO Release status

    
SELECT FROM zpoatta
             
INTO TABLE it_poatta
             
WHERE ebeln p_ebeln.

    
LOOP AT it_poatta INTO wa_poatta.
      wa_popup
-checkbox 'X'.
      wa_popup
-filename wa_poatta-filename+3(252).
      
APPEND wa_popup TO it_popup.
    
ENDLOOP.

  
ENDIF.  "End of LV_FRGZU PO Release status
**  PO ATTACHMENT POPUP DECLARATION END   ON 16-11-2015 **
************Mail Body Begin******************
  
TRY .
      go_send_request 
cl_bcs=>create_persistent( ).

    
CATCH cx_send_req_bcs INTO send_req.
  
ENDTRY.

  
TRY .
      document 
cl_document_bcs=>create_document(
                 i_type     
'HTM'
                 i_text     
it_contents
                 i_subject  
wa_docdata-obj_descr ).
    
CATCH cx_document_bcs INTO ref_msg .
  
ENDTRY.
************Mail Body End********************
************SSF to PDF conversion Begin******************
*  pdf_content = cl_document_bcs=>xstring_to_solix(
*           ip_xstring = bin_file ).
*
*  gv_attype   = 'PDF'. "File Type gse_dmsfiles-file_type.
*  gv_atsub    = p_ebeln. "File Name gse_dmsfiles-filename.
*
*  l_lines = lines( pdf_content ). "( gte_solix ).
*  l_size = l_lines * 255.
*
*  TRY.
*      document->add_attachment(
*      i_attachment_type      = gv_attype                           "'PDF'
*      i_attachment_subject   = gv_atsub                            "lv_sub
*      i_attachment_size      = l_size
*      i_att_content_hex      = pdf_content ).
*
*    CATCH cx_bcs INTO lo_bcs_exception.
*  ENDTRY.
*
************SSF to PDF conversion End*********************

  
LOOP AT it_toa01 INTO wa_toa01.
    
CLEARl_v_lengthl_i_tbl1024.

    
READ TABLE it_toaat INTO wa_toaat WITH KEY arc_doc_id wa_toa01-arc_doc_id.
    
IF sy-subrc 0.
      
READ TABLE it_popup INTO wa_popup WITH KEY filename wa_toaat-filename checkbox 'X'.
      
IF sy-subrc 0.
        
DELETE it_popup INDEX sy-tabix.
      
ELSE.
        
CONTINUE.
      
ENDIF.
*      " wa_toaat-filename hold attached file name
    
ENDIF.

    
CALL FUNCTION 'SCMS_AO_TABLE_GET'  "Get the data from DMS in RAW format
      
EXPORTING
        arc_id       
wa_toa01-archiv_id
        doc_id       
wa_toa01-arc_doc_id
      
IMPORTING
        length       
l_v_length
      
TABLES
        
data         l_i_tbl1024
      
EXCEPTIONS
        error_http   
1
        error_archiv 
2
        error_kernel 
3
        error_config 
4
        
OTHERS       5.
    
IF sy-subrc <> 0.
*        CONTINUE.
    
ENDIF.

    
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'  "RAW to XSTRING Conversion
      
EXPORTING
        input_length 
l_v_length
      
IMPORTING
        
buffer       lv_str
      
TABLES
        binary_tab   
l_i_tbl1024
      
EXCEPTIONS
        failed       
1
        
OTHERS       2.
    
IF sy-subrc 0.

      
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'  "XSTRING TO BINARY Conversion
        
EXPORTING
          
buffer     lv_str
        
TABLES
          binary_tab 
gte_solix.
    
ENDIF.

    l_v_cdod 
wa_toaat-filename.
    l_v_length1 
l_v_length.
"Create document
    
TRY.
        
CALL METHOD cl_document_bcs=>create_document
          
EXPORTING
            i_type    
'BIN'
            i_subject 
l_v_cdod
            i_length  
l_v_length1
            i_hex     
gte_solix
          RECEIVING
            result    
go_document.
      
CATCH cx_bcs INTO lo_bcs_exception.
    
ENDTRY.
"Attach document
    
TRY .
        
CALL METHOD document->add_document_as_attachment
          
EXPORTING
            im_document 
go_document.
      
CATCH cx_document_bcs INTO ref_msg .
    
ENDTRY.
"Set Attach document
    
TRY .
        
CALL METHOD go_send_request->set_documentdocument ).
      
CATCH cx_send_req_bcs INTO send_req.
    
ENDTRY.
  
ENDLOOP.
"Set Attach document outside loop if attachment is single SSF to PDF
*  TRY .
*      CALL METHOD go_send_request->set_document( document ).
*     CATCH cx_send_req_bcs INTO send_req.
*  ENDTRY.
"Get receiver mail ID
  
LOOP AT it_reclist INTO wa_reclist.
    lv_addr 
wa_reclist-receiver.

    
TRY .
     lo_recipient 
cl_cam_address_bcs=>create_internet_addresslv_addr ).
      
CATCH cx_address_bcs INTO ref_addr.
    
ENDTRY.

    
TRY .
        
CALL METHOD go_send_request->add_recipient
          
EXPORTING
            i_recipient 
lo_recipient
            i_express   
'X'.
      
CATCH cx_send_req_bcs INTO send_req.
    
ENDTRY.
  
ENDLOOP.
"Send mail immediately
  
TRY .
      
CALL METHOD go_send_request->set_send_immediately
        
EXPORTING
          i_send_immediately 
'X'.
    
CATCH cx_send_req_bcs INTO send_req.
  
ENDTRY.
"Send mail 
  
TRY .
      
CALL METHOD go_send_request->send(
        
EXPORTING
          i_with_error_screen 
'X'
        RECEIVING
          result              
lv_sent_to_all ).
    
CATCH cx_send_req_bcs INTO send_req.
  
ENDTRY.

  
COMMIT WORK.
  
IF sy-subrc 0.
*  MESSAGE 'check your mail' TYPE 'S'.
  
ENDIF.

Share this

Related Posts

2 comments

comments