ABAP GOS 上传标准附件

来源:互联网 发布:淘宝装机服务靠谱吗 编辑:程序博客网 时间:2024/05/17 06:24

此文参考:https://wiki.scn.sap.com/wiki/display/ABAP/GOS+-+Attachments+to+SAP+Documents

用GUI_UPLOAD选择上传文件.

附件数据库表为:SRGBTBREL,可以通过此表确定对应附件的OBJECT TYPE。

可通过修改参数实现,对采购申请,财务凭证,资产号等sap对象的标准附件上传。


*&---------------------------------------------------------------------*
*& Report  ZGOS_ATTACHMENT
*&
*&---------------------------------------------------------------------*
*&this program will attach documents to SAP documents.
*&
*&---------------------------------------------------------------------* 

report  zgos_attachment.

*Macros.
include <cntn01>.
swc_container lt_message_container.
*top dec.
type-pools: slis, abap, truxs.
data: g_objid     type ydmfw_objid,
      gt_filename type standard table of dxfilep with header line,
      wa_filename type dxfilep,
      gt_bin      type solix occurs 0,
      wa_bin      type solix,
      g_filename  type string,
      l_obj       type swc_object,
      gs_obja     type borident,
      gs_objb     type borident,
      gs_binrel   type gbinrel,
      gt_binatt   type standard table of brelattr,
      g_attsize   type INT4."wsrm_error-wsrm_direction.

selection-screen begin of block b0 with frame title text-001.
parameters: p_file  type rlgrap-filename,
            p_belnr type bkpf-belnr.
selection-screen end of block b0.

*----------------------------------------------------------------------*
* AT SELECTION-SCREEN  on <parameters>                                 *
*----------------------------------------------------------------------*
at selection-screen on p_file.
  if p_file is initial.
    message e999(yfimc01) with 'Please select the file path'. "#EC NOTEXT
  endif.
*----------------------------------------------------------------------*
* AT SELECTION-SCREEN  on HELP-REQUEST|VALUE-REQUEST                   *
*----------------------------------------------------------------------*
at selection-screen  on value-request for p_file.
  call function 'F4_FILENAME'
    exporting
      program_name  = syst-cprog
      dynpro_number = syst-dynnr
      field_name    = 'P_FILE'
    importing
      file_name     = p_file.

start-of-selection.
* this step can be replace to get file from app server as well.
*uploading [file: EXT is TXT].
  move  p_file to g_filename.
  call function 'GUI_UPLOAD'
    exporting
      filename                = g_filename
      filetype                = 'BIN'
    importing
      filelength              = g_attsize
    tables
      data_tab                = gt_bin
    exceptions
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      others                  = 17.
  if sy-subrc <> 0.
  else.
*file Uploaded successfully.
*convert uploaded file contents into BIN format.
    data: l_seq type i.
    swc_container      l_cont.
    swc_create_object  l_obj  'MESSAGE'       ''.
    swc_set_element    l_cont 'NO_DIALOG'     'X'.
    swc_set_element    l_cont 'DOCUMENTTITLE' g_filename.
    swc_set_table      l_cont 'Content_Hex'   gt_bin.
    swc_set_element    l_cont 'DOCUMENTTYPE'  'TXT'.
    swc_set_element    l_cont 'DOCUMENTSIZE'  g_attsize.
    swc_refresh_object l_obj.
    swc_call_method    l_obj  'CREATE'        l_cont.
    swc_get_object_key l_obj  gs_objb-objkey.

    gs_objb-objtype = 'MESSAGE'.   "type of attach document
    gs_obja-objtype = 'BKPF'.      "BO of SAP Document.
    concatenate    '0001'  "company code
                   p_belnr "FI Document
                   '2009'  "fiscal year
                   into
                   gs_obja-objkey.

    call function 'BINARY_RELATION_CREATE_COMMIT'
      exporting
        obj_rolea      = gs_obja
        obj_roleb      = gs_objb
        relationtype   = 'ATTA'
      importing
        binrel         = gs_binrel
      tables
        binrel_attrib  = gt_binatt
      exceptions
        no_model       = 1
        internal_error = 2
        unknown        = 3
        others         = 4.
    if sy-subrc eq 0.
      message s043(sgos_msg).
    endif.
  endif.


0 0