Create SAP DMS document using xstring

If you want to create SAP DMS document tor just creation of file using binary data, this FM code will help you in that. You can also use this code in API to integrate with non SAP system. This FM first generates the file on application server using binary data, then creates the DMS document.

01.

Enter the Input Parameters

02.

DMS Document Number in Export Parameters

Function Module Source code

FUNCTION ZDMS_DOCUMENTS_CREATE.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(IP_DOKAR) TYPE DOKAR OPTIONAL
*" VALUE(IP_FILE_NAME) TYPE STRING OPTIONAL
*" VALUE(IP_FILE_TYPE) TYPE CHAR20 OPTIONAL
*" VALUE(IP_DESCRIPTION) TYPE DKTXT OPTIONAL
*" VALUE(IP_XSTRING) TYPE XSTRING OPTIONAL
*" EXPORTING
*" VALUE(EP_MSG) TYPE STRING
*" VALUE(EP_DOKNR) TYPE DOKNR
*"----------------------------------------------------------------------
"This FM will return the DMS attachment output in binary format
**************************************************************************************
"Tables
DATA: LT_OBJECTLINKS TYPE TABLE OF BAPI_DOC_DRAD.
DATA: LT_OBJECTDESCR TYPE TABLE OF BAPI_DOC_DRAT.
DATA: LT_OBJECTFILES TYPE TABLE OF BAPI_DOC_FILES2.
DATA RETURN TYPE BAPIRET2.

"Structures
DATA: LS_DOCDATA TYPE BAPI_DOC_DRAW2.
DATA: LS_OBJECTLINK TYPE BAPI_DOC_DRAD.
DATA: LS_OBJECTDESCR TYPE BAPI_DOC_DRAT.
DATA: LS_OBJECTFILE TYPE BAPI_DOC_FILES2.

DATA : GS_TDWAT TYPE TDWAT,
ACCESS_INFO LIKE STANDARD TABLE OF SCMS_ACINF WITH HEADER LINE,
GT_BIN TYPE STANDARD TABLE OF RAW255,
GS_BIN TYPE RAW255.

"Variables
DATA: LV_FILENAME TYPE STRING,
DV_EXTENSION TYPE STRING,
DV_FILENAME TYPE STRING,
DV_FILENAME1 TYPE STRING,
DV_DOCTYPE TYPE DOKAR, "VALUE 'Z01',
DV_OBJTYPE TYPE DOKOB VALUE 'KNA1',
DV_DKTXT TYPE DKTXT,
GV_FLAG TYPE FLAG.

IF IP_DOKAR IS NOT INITIAL AND
IP_FILE_NAME IS NOT INITIAL AND
IP_FILE_TYPE IS NOT INITIAL AND
ip_description IS NOT INITIAL AND
IP_XSTRING IS NOT INITIAL.


"document type description
* SELECT SINGLE * FROM TDWAT INTO CORRESPONDING FIELDS OF GS_TDWAT WHERE DOKAR = IP_DOKAR.
*
* CONCATENATE 'Test' GS_TDWAT-DARTXT INTO DV_DKTXT SEPARATED BY '/'.

dv_dktxt = ip_description.

CONCATENATE '/tmp/' IP_FILE_NAME INTO LV_FILENAME.

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
BUFFER = IP_XSTRING
IMPORTING
OUTPUT_LENGTH = ACCESS_INFO-COMP_SIZE  "gv_filesize
TABLES
BINARY_TAB = GT_BIN.

OPEN DATASET LV_FILENAME FOR OUTPUT IN BINARY MODE.

LOOP AT GT_BIN INTO GS_BIN.
TRANSFER GS_BIN TO LV_FILENAME.
ENDLOOP.
CLOSE DATASET LV_FILENAME.

WAIT UP TO 1 SECONDS.

CLEAR : GV_FLAG.
"check if file exit
OPEN DATASET LV_FILENAME FOR INPUT IN BINARY MODE.
IF SY-SUBRC = 0.
GV_FLAG = 'X'.
ENDIF.
CLOSE DATASET LV_FILENAME.

* BREAK DEVELOPER1.

IF GV_FLAG IS NOT INITIAL. "if file exit on server
"add dms attachment concept

CLEAR : DV_DOCTYPE, DV_FILENAME, DV_EXTENSION, DV_FILENAME1.


DV_DOCTYPE = IP_DOKAR.
DV_FILENAME = IP_FILE_NAME.

CALL FUNCTION 'CH_SPLIT_FILENAME'
EXPORTING
COMPLETE_FILENAME = DV_FILENAME
IMPORTING
EXTENSION = DV_EXTENSION
NAME = DV_FILENAME1
EXCEPTIONS
INVALID_DRIVE = 1
INVALID_PATH = 2
OTHERS = 3.

LS_DOCDATA-DOCUMENTTYPE = IP_DOKAR.
LS_DOCDATA-DOCUMENTVERSION = '00'.
LS_DOCDATA-DOCUMENTPART = '000'.
LS_DOCDATA-STATUSEXTERN = 'PR'.

LS_OBJECTDESCR-DESCRIPTION = DV_DKTXT.
LS_OBJECTDESCR-LANGUAGE = 'E'.
LS_OBJECTDESCR-LANGUAGE_ISO = 'EN'.
APPEND LS_OBJECTDESCR TO LT_OBJECTDESCR.

* LS_OBJECTFILE-ORIGINALTYPE = '001'.
LS_OBJECTFILE-STORAGECATEGORY = 'DMS_C1_ST'.
* LS_OBJECTFILE-SOURCEDATACARRIER = 'SAP-SYSTEM'.
LS_OBJECTFILE-CREATED_BY = SY-UNAME.
LS_OBJECTFILE-DOCFILE = LV_FILENAME.
LS_OBJECTFILE-WSAPPLICATION = IP_FILE_TYPE.
LS_OBJECTFILE-CHECKEDIN = 'X'.
APPEND LS_OBJECTFILE TO LT_OBJECTFILES.

* IF IP_KUNNR IS NOT INITIAL.
* LS_OBJECTLINK-OBJECTTYPE = DV_OBJTYPE.
* LS_OBJECTLINK-OBJECTKEY = IP_KUNNR
* APPEND LS_OBJECTLINK TO LT_OBJECTLINKS.
* ENDIF.

CALL FUNCTION 'BAPI_DOCUMENT_CREATE2'
EXPORTING
DOCUMENTDATA = LS_DOCDATA
PF_FTP_DEST = 'SAPFTPA'
PF_HTTP_DEST = 'SAPHTTP'
IMPORTING
DOCUMENTNUMBER = EP_DOKNR
RETURN = RETURN
TABLES
DOCUMENTDESCRIPTIONS = LT_OBJECTDESCR
OBJECTLINKS = LT_OBJECTLINKS
DOCUMENTFILES = LT_OBJECTFILES.

IF EP_DOKNR IS NOT INITIAL.
COMMIT WORK.
ELSE.
EP_MSG = 'Error while creating the DMS document'.
ENDIF.
********end of upload dms document**********
* if GV_FLAG is NOT INITIAL.
* DELETE DATASET LV_FILENAME.
ENDIF.

ELSE.
EP_MSG = 'Pass all the required parameters'.
ENDIF.

ENDFUNCTION.

Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
ZK Tutorials 2024 Privacy policy Terms of use Contact us Refund policy