Friday 22 May 2020

Upload & Download Attachments in Dispute Management(FSCM)

In this blog, I am going to explain how to work with Attachments in the Dispute management module(FSCM) through the ABAP code.

There is a requirement where we need to Upload & Download the attachments for the Dispute case in our custom Applications I search for Different blogs to find any function modules or classes that can serve this purpose.

PROCEDURE:


Below is the Step-by-Step Process of how to create/Display Dispute case Attachment

DOWNLOAD DISPUTE CASE ATTACHMENT 


STEP 1: get the CASE_GUID from SCMG_T_CASE_ATTR table and pass to the Function Module

Functi UDM_DC_CASE_READ to get the attachment details.

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Exam Prep

STEP 2: For each attachment, a Unique GUID will be generated this is DOC_GUID and DOC_CLASS will show Storage Location of Document in SRM (Content Model), these two attributes data we can get from above Function module so loop the LT_ATTACH internal table and get attachment data for each DOC_GUID by calling function module SRM_DOCUMENT_CHECKOUT_CONT_TAB.

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Exam Prep

STEP 3: Convert the Mime Type of Attachment into file type  by calling function module ‘SA_KW_RFC_FILENAME_EXT_GET’

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Exam Prep

STEP 4: Finally pass the binary content fetching from SRM_DOCUMENT_CHECKOUT_CONT_TAB to Function module GUI_DOWNLOAD and download the file to whichever Location you want, here we are downloading the file to Desktop directory.

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Exam Prep

Below is complete code

REPORT ZCASE_GET_ATTACHMENTS.
PARAMETERS: P_EXTKEY TYPE SCMG_EXT_KEY.

DATA: LV_GUID     TYPE SCMG_CASE_GUID,
LV_PATH     TYPE STRING,
LV_FILENAME TYPE STRING,
LV_DOCTYP   TYPE CHAR10,
LT_ATTACH   TYPE UDM_T_CASE_DOCUMENT_LIST.

*FETCH CASE GUID BASED ON CASE NO
SELECT SINGLE CASE_GUID
FROM   SCMG_T_CASE_ATTR
INTO LV_GUID
WHERE EXT_KEY = P_EXTKEY.

"TO READ THE ATTACHMENT BASED ON GUID
CALL FUNCTION 'UDM_DC_CASE_READ'
EXPORTING
I_CASE_GUID   = LV_GUID
IMPORTING
ET_ATTACHMENT = LT_ATTACH.

DATA: LT_COMPONENT TYPE STANDARD TABLE OF BAPIDOCCOMP,
LT_BIN       TYPE STANDARD TABLE OF BAPICONTENT255.

LOOP AT LT_ATTACH ASSIGNING FIELD-SYMBOL (<LS_ATTACH>).
REFRESH:LT_COMPONENT,LT_BIN.
"COVERT DATA INTO BINARY FORMAT
CALL FUNCTION 'SRM_DOCUMENT_CHECKOUT_CONT_TAB'
EXPORTING
OBJECTID        = <LS_ATTACH>-DOC_GUID
DOCUMENTCLASS   = <LS_ATTACH>-DOC_CLASS
TABLES
COMPONENTS      = LT_COMPONENT
BIN_CONTENT     = LT_BIN
EXCEPTIONS
INTERNAL_ERROR  = 1
PARAMETER_ERROR = 2
NOT_AUTHORIZED  = 3
DOC_NOT_FOUND   = 4.

"TO CONVERT MIMETYPE TO DOCTYPE
CLEAR: LV_DOCTYP.
CALL FUNCTION 'SA_KW_RFC_FILENAME_EXT_GET'
EXPORTING
MIMETYPE  = <LS_ATTACH>-MIMETYPE
IMPORTING
EXTENSION = LV_DOCTYP.

"TO READ DESKTOP PATH
CLEAR: LV_PATH, LV_FILENAME.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>GET_DESKTOP_DIRECTORY
CHANGING
DESKTOP_DIRECTORY    = LV_PATH
EXCEPTIONS
CNTL_ERROR           = 1
ERROR_NO_GUI         = 2
NOT_SUPPORTED_BY_GUI = 3
OTHERS               = 4.
CALL METHOD CL_GUI_CFW=>UPDATE_VIEW.

"PREPARE THE FILE NAME ALONG WITH PATH
LV_FILENAME = LV_PATH && '\' && LV_GUID  && '\'
&& <LS_ATTACH>-DESCRIPTION && '.' && LV_DOCTYP.

"DOWNLOAD THE FILE TO DESKTOP PATH
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
FILENAME                = LV_FILENAME
FILETYPE                = 'BIN'
TABLES
DATA_TAB                = LT_BIN
EXCEPTIONS
FILE_WRITE_ERROR        = 1
NO_BATCH                = 2
GUI_REFUSE_FILETRANSFER = 3
INVALID_TYPE            = 4
NO_AUTHORITY            = 5
UNKNOWN_ERROR           = 6
HEADER_NOT_ALLOWED      = 7
SEPARATOR_NOT_ALLOWED   = 8
FILESIZE_NOT_ALLOWED    = 9
HEADER_TOO_LONG         = 10
DP_ERROR_CREATE         = 11
DP_ERROR_SEND           = 12
DP_ERROR_WRITE          = 13
UNKNOWN_DP_ERROR        = 14
ACCESS_DENIED           = 15
DP_OUT_OF_MEMORY        = 16
DISK_FULL               = 17
DP_TIMEOUT              = 18
FILE_NOT_FOUND          = 19
DATAPROVIDER_EXCEPTION  = 20
CONTROL_FLUSH_ERROR     = 21
OTHERS                  = 22.
IF SY-SUBRC <> 0.
* IMPLEMENT SUITABLE ERROR HANDLING HERE
ENDIF.
ENDLOOP.

CREATE DISPUTE CASE ATTACHMENT:


STEP 1:  Upload the file and convert it into Binary data by using function module GUI_UPLOAD as below.

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Exam Prep

STEP 2: convert this binary data to XSTRING.

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Exam Prep

STEP 3: get the Mime type bypassing the file name to by Calling Function module CV120_GET_MIME_TYPE and convert the xstring to RAW DATA.

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Exam Prep

STEP 4: Prepare File class and File content internal table and Pass to the Function Module UDM_DC_ATTRIBUTES_SET.

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Exam Prep

Pass the File content to UDM_DC_ATTRIBUTES_SET Function module to create attachment

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Exam Prep

Below is complete code

DATA: RES_TAB        TYPE TABLE OF BAPICONTEN,
LV_CSTRING     TYPE        XSTRING,
I_FILELENGTH   TYPE        I,
LV_FNAME       TYPE        STRING,
LV_LOCAL       TYPE        DRAW-FILEP,
LV_GUID        TYPE        SCMG_T_CASE_ATTR-CASE_GUID,
LT_ATTRIBUTE   TYPE        BDM_T_ATTRIBUTE,
LT_FILECONTENT TYPE        BDM_T_FILECONTENT,
LR_FILECONTENT TYPE REF TO BDM_FILECONTENT,
LT_FILECLASS   TYPE        BDM_T_FILECLASS,
LR_FILECLASS   TYPE REF TO BAPI_DISPUTE_FILECLASS,
LT_CONTENT     TYPE STANDARD TABLE OF BAPIRMDATX255,
LR_CONTENT     TYPE REF TO BAPIRMDATX255,
LV_SIZE        TYPE        BDM_COMP_SIZE,
LV_FILENAME    TYPE        BDM_DOCID,
LV_EXT         TYPE STRING,
LV_MIMETYPE    TYPE        W3CONTTYPE,
LT_DOCUMENT    TYPE        UDM_T_CASE_DOCUMENT_LIST,
LR_DOCUMENT    TYPE REF TO UDM_CASE_DOCUMENT_LIST,
ES_RETURN      TYPE BAPIRET2.

LV_FNAME = LV_ATTACH.

CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME                = LV_FNAME
FILETYPE                = 'BIN'
READ_BY_LINE            = 'X'
IMPORTING
FILELENGTH              = I_FILELENGTH
TABLES
DATA_TAB                = RES_TAB
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.
MESSAGE TEXT-118 TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
ENDIF.

LOOP AT RES_TAB ASSIGNING FIELD-SYMBOL(<FS_RAW>).
CONCATENATE LV_CSTRING <FS_RAW>-LINE INTO LV_CSTRING IN BYTE MODE.
ENDLOOP.

LV_LOCAL = LV_FNAME.

CALL FUNCTION 'CV120_GET_MIME_TYPE'
EXPORTING
*       PF_DAPPL     =
PF_FILE      = LV_LOCAL
IMPORTING
PFX_MIMETYPE = LV_MIMETYPE.

CALL METHOD CL_SWF_UTL_CONVERT_XSTRING=>XSTRING_TO_TABLE
EXPORTING
I_STREAM      = LV_CSTRING
IMPORTING
E_TABLE       = LT_CONTENT
EXCEPTIONS
INVALID_INPUT = 1
INVALID_TABLE = 2
OTHERS        = 3.
IF SY-SUBRC <> 0.
ENDIF.

CALL FUNCTION 'CH_SPLIT_FILENAME'
EXPORTING
COMPLETE_FILENAME = LV_FNAME
IMPORTING
NAME              = LV_FILENAME.

LV_SIZE = XSTRLEN( LV_CSTRING )." LENGTH LV_SIZE IN CHARACTER MODE.

LOOP AT LT_CONTENT REFERENCE INTO LR_CONTENT.
CREATE DATA LR_FILECONTENT.
LR_FILECONTENT->FILE_ID      = 1.
LR_FILECONTENT->CONTENT_LINE = LR_CONTENT->*.
LR_FILECONTENT->COMP_ID      = LV_FILENAME.
LR_FILECONTENT->MIMETYPE     = LV_MIMETYPE.
LR_FILECONTENT->COMP_SIZE    = LV_SIZE.
APPEND LR_FILECONTENT->* TO LT_FILECONTENT.
ENDLOOP.

CREATE DATA LR_FILECLASS.
LR_FILECLASS->FILE_ID = 1.
LR_FILECLASS->NEW_CLASS = 'INFO'. "This class varies based on requirement
APPEND LR_FILECLASS->* TO LT_FILECLASS.

CALL METHOD /KFINAPP/CL_DM_CONVERSIONS=>GET_EXISTING_ATTRIBUTES(
EXPORTING
IM_CASE_GUID  = LV_CASE
IMPORTING
ET_ATTRIBUTES = LT_ATTRIBUTE
).

CALL FUNCTION 'UDM_DC_ATTRIBUTES_SET'
EXPORTING
I_CASE_GUID       = LV_CASE
IT_ATTRIBUTE      = LT_ATTRIBUTE
IT_FILECONTENT    = LT_FILECONTENT
IT_FILECLASS      = LT_FILECLASS
I_ENQUEUE         = 'X'
I_NO_FREE_OBJECTS = ' ' "'X'
IMPORTING
ES_RETURN         = ES_RETURN.

IF ES_RETURN IS INITIAL.
COMMIT WORK.
CLEAR LV_ATTACH.
MESSAGE TEXT-119 TYPE 'S'.

ENDIF.

No comments:

Post a Comment