惯例闲话:国庆假期后,状态不佳,闲人找回状态的方法,对电脑翻箱倒柜,整理文件,往往在整理鸡零狗碎时,会有一些新收获,这倒成了自我调整的一套规律…
这次整理捣鼓,还真是发现了一个解决长久以来SAP被人吐槽的附件上传问题。
闲话到此为止,来看看这个附件上传下载问题。
业务场景:公司要开发一个售后服务登记平台,基本功能如下:
1、公司接收到客户的售后服务电话请求后,在SAP录入一张服务单。仅作为统计只用,不用做后续费用结算
2、需要将客户的反馈单据作为附件和服务单绑定在一起,实现在一个界面上打开服务单,并能同时上载和下载附件。
功能很简单,但是附件上传和下载是历来被吐槽的地方,通常顾问都会拒绝这个需求,要么DMS实现,要么客户自己建一个文件服务器保存文件。闲人以为,在SAP服务器上保存非结构化数据,是极其不明智的选择。不过么,10年前的思维,还是被动型,领导提什么就做什么。
基本操作界面如下:
一、 数据录入
事务代码ZPSXX
打开之后,点击新建
可在以下界面中输入相关信息
如果有附件需要上传,只需要点击左上角附件上传按钮
选择附件上传,文件必须为PDF格式
若需要对输入的信息进行修改
输入登记号,点击修改按钮
编辑信息之后,点击保存即可
若需要导出附件,则点击附件导出即可,导出目录为
C:\根目录,文件格式必须为PDF格式。
若需要下载附件,则点击
二、 报表输出
事务代码ZPSXXX
三、源代码
作为一个偶尔做做ABAP打野的PSer,请ABAP大拿们谅解,当年的写代码烂手法。
&---------------------------------------------------------------------
*& Report ztest01
*&
&---------------------------------------------------------------------
*&
*&created by xianren
&---------------------------------------------------------------------
REPORT ztest01.
PARAMETERS:
p_objkey LIKE borident-objkey
DEFAULT ‘2013010’,"文件对象号
p_objtyp LIKE borident-objtype
DEFAULT ‘BUS2105’."对应的对象类型
DATA g_borident TYPE borident.
DATA refer TYPE REF TO cl_gos_document_service.
DATA: l_objectid TYPE bapiborid,
lt_relat TYPE TABLE OF bapirellk WITH HEADER LINE,
l_objdisp TYPE sood2,
lt_objcont TYPE TABLE OF soli,
lt_context LIKE sdokpropty OCCURS 0 WITH HEADER LINE,
loio_object LIKE sdokobject,
phio_object LIKE sdokobject,
lt_binary_content TYPE TABLE OF sdokcntbin,
xfile TYPE xstring,
xfilename TYPE string,
p_g_path(255) type C,
str1(255) type c, so_filename like SOLI-line, P_FOLDER_ID like SOODK, P_OBJECT_ID like SOODK, t_OBJHEAD like SOLI OCCURS 0 WITH HEADER LINE, p_ftype like sood2-file_ext.
DATA: xlen TYPE i.
DATA t_banfn LIKE eban-banfn.
START-OF-SELECTION.
g_borident-objkey = p_objkey."此处单据号
g_borident-objtype = p_objtyp."对象号
CREATE OBJECT refer.
CALL METHOD refer->create_attachment
EXPORTING
is_object = g_borident
l_objectid-objkey = p_objkey.
l_objectid-objtype = ‘BUS2105’.
CALL FUNCTION ‘BAPI_REL_GETRELATIONS’
EXPORTING
OBJECTID = l_objectid
sort lt_relat by OBJKEY_B DESCENDING.
LOOP AT lt_relat.
READ TABLE lt_relat INDEX 1."业务上规定附件只有一个
REFRESH lt_objcont.
P_FOLDER_ID-OBJTP = lt_relat-objkey_b(3).
P_FOLDER_ID-OBJYR = lt_relat-objkey_b+3(2).
P_FOLDER_ID-OBJNO = lt_relat-objkey_b+5(12).
P_OBJECT_ID-OBJTP = lt_relat-objkey_b+17(3).
P_OBJECT_ID-OBJYR = lt_relat-objkey_b+20(2).
P_OBJECT_ID-OBJNO = lt_relat-objkey_b+22(12).
CALL FUNCTION ‘SO_OBJECT_READ’
EXPORTING
folder_id = P_FOLDER_ID
object_id = P_OBJECT_ID
IMPORTING
object_hd_display = l_objdisp
TABLES
objcont = lt_objcont
OBJHEAD = t_OBJHEAD
EXCEPTIONS
OTHERS = 15.
loop at T_OBJHEAD.
if SY-TABIX = 1.
SPLIT t_OBJHEAD-line AT ‘=’ INTO str1 so_filename.
exit.
endif.
endloop.
CALL FUNCTION ‘SO_KPRO_DATA_FROM_OBJCONT_GET’
IMPORTING
loio_object = loio_object
TABLES
objcont = lt_objcont
context = lt_context
EXCEPTIONS
missing_kpro_data = 1
OTHERS = 2.
CALL FUNCTION ‘SO_LOIO_PHIO_GET’
EXPORTING
loio_object = loio_object
IMPORTING
phio_object = phio_object
EXCEPTIONS
kpro_inconsistency = 1
x_error = 2
OTHERS = 3.
CALL FUNCTION ‘SDOK_PHIO_LOAD_CONTENT’
EXPORTING
object_id = phio_object
TABLES
file_content_binary = lt_binary_content
EXCEPTIONS
not_existing = 1
not_authorized = 2
no_content = 3
bad_storage_type = 4
OTHERS = 5.
xlen = l_objdisp-objlen.
CALL FUNCTION ‘SCMS_BINARY_TO_XSTRING’
EXPORTING
input_length = xlen
IMPORTING
buffer = xfile
TABLES
binary_tab = lt_binary_content
EXCEPTIONS
failed = 1
OTHERS = 2.
DATA sap_path_n TYPE string.
DATA wa LIKE sdokcntbin.
CALL FUNCTION ‘GUI_DOWNLOAD’
EXPORTING
filename = ‘c:\下载文档.PDF’
filetype = ‘BIN’
TABLES
data_tab = lt_binary_content.
exit.
ENDLOOP.
回忆:闲人对开发的兴趣来自于当年师匠的教诲,师匠说,作为业务出身的SAP顾问,业务逻辑是你的长处,但是想要在IT这一行混下去,掌握一定的开发技能,是把技术做成艺术的必要条件之一。闲人当年年少气盛,对师匠的话,曾经不以为然,还喜欢怼几句,真是应验了那句话,人越浅薄越轻狂。
三人行,必有我师,做IT咨询这一行,永远保持学习心态。