Create File Attachment in Sales Documents(VA02) using Net Weaver Gateway
Scenario:
Many times there is a business requirement of linking documents, entering notes, sending notes or linking an internet address to various SAP objects. These external attachments can be reference documents, pictures, Email attachments, design , diagrams or related spreadsheets. To meet this requirement SAP has provided a tool bar called ‘Generic Service Toolbar'(GOS).
Recently, I came across a requirement where i had to create a attachment for existing sales order (VA02) through Net weaver Gateway Service.
By Using this Blog, you can attach a wide range of documents like Word Documents,Excel Sheets, PDF, Text Files and Images and many more through Gateway service.
Procedure:
We have created a project in ECC system for create attachment to Sales Order through Gateway Service. As shown in below.
Right Click on the Data Model folder and select Import DDIC structure and Give the Entity Type Name as Attachment and select the Required properties for the Entity Type.
In the Entity Type Properties select the check-box: Media. Our Entity Type Attachment and its properties look as below.
Then click on Generate Run time objects. It displayed “Generated Objects Successfully” , that time Generated all class automatically.
Then Redefine the DEFINE method in the *MPC_EXT Class and add the below logic.
method DEFINE.
super->DEFINE( ).
DATA: lo_entity type REF TO /IWBEP/IF_MGW_ODATA_ENTITY_TYP,
lo_property type REF TO /IWBEP/IF_MGW_ODATA_PROPERTY.
lo_entity = model->GET_ENTITY_TYPE( IV_ENTITY_NAME = ‘Attachment’ ).“Entity Name
IF lo_entity is BOUND.
lo_property = lo_entity->GET_PROPERTY( IV_PROPERTY_NAME = ‘Filename’ ).“Key Value(SLUG)
lo_property->SET_AS_CONTENT_TYPE( ).
ENDIF.
endmethod.
Then redefine the CREATE_STREAM method ( /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_STREAM ) in the *DPC_EXT class and implement the below logic to upload the file attachment into the Sales Order(VA02) based on Sales Order Number.
Code:
method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_STREAM.
*————————————————————-
* Constants
*————————————————————-
CONSTANTS :
c_bus2032 TYPE swo_objtyp VALUE ‘BUS2032’, ” Bus number for sale order
c_ext(3) TYPE c VALUE ‘EXT’,
c_atta(4) TYPE c VALUE ‘ATTA’,
c_b(1) TYPE c VALUE ‘B’,
c_x(1) TYPE c VALUE ‘X’,
c_o(1) TYPE c VALUE ‘O’.
*————————————————————-
* Data declaration
*————————————————————-
DATA: it_content TYPE STANDARD TABLE OF soli, ” Content Of File Storage
it_objhead TYPE STANDARD TABLE OF soli,
wa_folmem_k TYPE sofmk, ” Folder Content Data
wa_note TYPE borident, ” BOR object identifier
wa_object TYPE borident,
wa_obj_id TYPE soodk, ” Definition of an Object (Key Part)
wa_fol_id TYPE soodk,
wa_obj_data TYPE sood1, ” Object definition and Change attributes
lv_ep_note TYPE borident–objkey, ” BOR Object Key
lv_extension TYPE c LENGTH 4 value ‘TXT’, ” File Extension only
lv_so_num TYPE vbeln_va, ” Sales order number
lv_file_des TYPE so_obj_des. ” File name
*/Refresh data
REFRESH: it_content[], it_objhead[].
*/Field Symbol for SLUG
FIELD-SYMBOLS:<fs_key> TYPE /iwbep/s_mgw_name_value_pair.
*/Read the SLUG Value and Name based on INDEX
READ TABLE it_key_tab ASSIGNING <fs_key> INDEX 1.
*/Function module for Xstring to Binary Conversion
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING
buffer = is_media_resource–value “Xstring
append_to_table = c_x
* IMPORTING
* OUTPUT_LENGTH =
TABLES
binary_tab = it_content[] “Binary
.
*/Get folder id
CALL FUNCTION ‘SO_FOLDER_ROOT_ID_GET’
EXPORTING
region = c_b
IMPORTING
folder_id = wa_fol_id
EXCEPTIONS
communication_failure = 1
owner_not_exist = 2
system_failure = 3
x_error = 4
OTHERS = 5.
CLEAR: lv_so_num,lv_file_des.
IF iv_slug IS NOT INITIAL.
SPLIT iv_slug AT ‘/’ INTO lv_so_num lv_file_des.
IF lv_so_num IS NOT INITIAL.
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT’
EXPORTING
input = lv_so_num
IMPORTING
output = lv_so_num.
ENDIF.
ENDIF.
*/ Assigning Valuse to the Standard Strucuture Fields
wa_object–objkey = lv_so_num. ” Sales Order Number
wa_object–objtype = c_bus2032. ” Bus Number
wa_obj_data–objsns = c_o. ” Sensitivity of Object (O-Standard)
wa_obj_data–objla = sy–langu. ” Language
wa_obj_data–objdes = lv_file_des. ” Slug Value – Description
wa_obj_data–file_ext = lv_extension. ” File Extension
*/ Change Extension to UpperCase
TRANSLATE wa_obj_data–file_ext TO UPPER CASE.
wa_obj_data–objlen = lines( it_content ) * 255.
*/ Insert data
CALL FUNCTION ‘SO_OBJECT_INSERT’
EXPORTING
folder_id = wa_fol_id
object_type = c_ext
object_hd_change = wa_obj_data
IMPORTING
object_id = wa_obj_id
TABLES
objhead = it_objhead
objcont = it_content
EXCEPTIONS
active_user_not_exist = 1
communication_failure = 2
component_not_available = 3
dl_name_exist = 4
folder_not_exist = 5
folder_no_authorization = 6
object_type_not_exist = 7
operation_no_authorization = 8
owner_not_exist = 9
parameter_error = 10
substitute_not_active = 11
substitute_not_defined = 12
system_failure = 13
x_error = 14
OTHERS = 15.
IF sy–subrc = 0 AND wa_object–objkey IS NOT INITIAL.
wa_folmem_k–foltp = wa_fol_id–objtp.
wa_folmem_k–folyr = wa_fol_id–objyr.
wa_folmem_k–folno = wa_fol_id–objno.
*/Please note: wa_fol_id and wa_obj_id are different work areas
wa_folmem_k–doctp = wa_obj_id–objtp.
wa_folmem_k–docyr = wa_obj_id–objyr.
wa_folmem_k–docno = wa_obj_id–objno.
lv_ep_note = wa_folmem_k.
wa_note–objtype = ‘MESSAGE’.
wa_note–objkey = lv_ep_note.
*/Link it
CALL FUNCTION ‘BINARY_RELATION_CREATE_COMMIT’
EXPORTING
obj_rolea = wa_object
obj_roleb = wa_note
relationtype = c_atta
EXCEPTIONS
no_model = 1
internal_error = 2
unknown = 3
OTHERS = 4.
CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
EXPORTING
WAIT = c_x
* IMPORTING
* RETURN =
.
IF sy–subrc EQ 0.
*/Commit it
COMMIT WORK.
ENDIF.
ENDIF.
endmethod.
All input Values we have to get into the SLUG parameter from the UI Side. If you have multiple input parameter values then with concatenate of multiple parameter values with delimiter we have to get the values in SLUG parameter.
Testing Our Service:
Now we will test our service in Gateway Client transaction for that is /IWFND/MAINT_SERVICE
Upload a file through add file button which is on left side corner as shown in the below screen shot.
Pass SLUG values(mandatory) pass file name and sales order number as shown in the below screen shot.
In this example we passing Multiple parameter values like SalesOrder Number and File Description. These two values are separted by ‘ , ‘. shown in below screen shot.
Paste our URI in Request URI field and click on POST HTTP Method.
URI: /sap/opu/odata/sap/ZASC_ECOMM_SO_ATTACHMENT_SRV/AttachmentSet
Service Response:
Successfully created the Attachment in GWS.
Result:
Go to Sales Order Display (VA03) Transaction and click on Services for Objects in Title Bar then you will get the attachment list as shown in below.
You will find your attachment.
Attachment added successfully to the Sales Order.
Thanks&Regards,
Harikrishna Malladi
Nice Blog Hari... 🙂
Thankyou.....
Nice. I'll try soon. 😉
Thanks
--Pavan G
Thanks alot.... 😳
Nice one Hari. Keep going..:)
Regards,
B Raghu Prasad
Thank you very much Raghu... 🙂
Nice blog. 🙂
I have followed the same steps. But after attachment is done, if I open the attachment from GOS, the data in the attachment is incorrect or some junk values are there.
Is there any parameters I need to change based on the document type?
Thank you 🙂
Imran can you please fill the lv_extension value (File Extension).
Eg: lv_extension = 'TXT'.
I have passed extension also based on document type.
But while converting from xstring to binary, internal table it_content is filled with some junk values and the same is being displayed in the file when I open from GOS.
Imran are you sending file through gateway interface or any report?. In the above case i am sending file through Gateway, in gateway interface the file content should be in binary format thats why i am changing that format.If you use any other interface no need to convert the file format to Binary.
Harish,
I am sending through gateway only. If I send the file in CSV format, data reflecting correctly in attachment. But if I upload file in DOCX, PNG, TXT, etc., junk values are coming.
Imran, I think it is not supporting your system please contact your basis,if you need any configuration.
By using SO_OBJECT_INSERT Function module you can send only 3 characters extension file names only like txt,pdf,jpg,png and it is not supporting 4 characters extension file names like xlsx,docx because the structure(sood1) holds 3 characters only.
Harish, If that is the case, it should work for TXT and PNG. But for these type of documents also I am getting junk values after uploading.
Did you try opening the TXT, PNG files after uploading in the GOS??
Yes, I have checked its working fine for TXT file.(attached)
Where as in PNG image files successfully upload the GOS bar, when try to open that image shows below.
I have manually added the PNG image to the GOS Bar it will shows like this Information message.
I think SAP System is not supporting PNG File format.
Hi Harikrishna,
I am using FM ‘SCMS_XSTRING_TO_BINARY’ , but I am getting junk data. I am using exactly same data types as yours. Can you suggest what might be the issue.
Â
Thanks,
Sachin
For anyone trying to attach a JPG to the a doc, this code isnt exactly correct. The above code only works for TXT files. I was able to replace the call to SO_OBJECT_INSERT which only allowed file data in SOLI form with SO_DOCUMENT_INSERT that gives an option for SOLI or SOLIX data. In the case of JPG you need to pass SOLIX. Here is my code. Be weary of the Object type it needs to match the doc. I was trying to attach to a return order where the doc type is BUS2102
REPORT YPHOTO_UP.
CONSTANTS :
* C_BUS2032 TYPE SWO_OBJTYP VALUE 'BUS2032', " Bus number for sale order
C_BUS2032 TYPE SWO_OBJTYP VALUE 'BUS2102', " Bus number for sale order
C_EXT(3) TYPE C VALUE 'EXT',
C_ATTA(4) TYPE C VALUE 'ATTA',
C_B(1) TYPE C VALUE 'B',
C_X(1) TYPE C VALUE 'X',
C_O(1) TYPE C VALUE 'O'.
DATA: IT_CONTENT TYPE STANDARD TABLE OF SOLI,
IT_OBJHEAD TYPE STANDARD TABLE OF SOLI,
WA_FOLMEM_K TYPE SOFMK,
WA_NOTE TYPE BORIDENT,
WA_OBJECT TYPE BORIDENT,
WA_OBJ_ID TYPE SOODK,
WA_FOL_ID TYPE SOODK,
WA_OBJ_DATA TYPE SOOD1,
LV_EP_NOTE TYPE SWO_TYPEID,
LV_EXTENSION TYPE C LENGTH 4 VALUE 'JPG',
LV_SO_NUM TYPE VBELN_VA,
LV_FILE_DES TYPE SO_OBJ_DES.
DATA DOCUMENTS TYPE TABLE OF SOOD4.
DATA ST_DOCUMENTS TYPE SOOD4.
DATA: FILETAB LIKE TABLE OF SALFLDIR WITH HEADER LINE.
DATA: SAPOBJID LIKE SAPB-SAPOBJID,
SAPPFAD LIKE SAPB-SAPPFAD.
DATA LENG TYPE I.
DATA: XSTR TYPE XSTRING.
DATA: STR TYPE STRING.
*data: hex_container TYPE x LENGTH 5000.
DATA: FILENAME LIKE SALFLDIR ,
FILEEXT(10) TYPE C ,
LEN TYPE I ,
PICFILENAME TYPE STRING.
DATA: IT_OBJBIN TYPE STANDARD TABLE OF SOLIX.
DATA: FILEPATH LIKE SALFILE-LONGNAME VALUE '/common/tony/sled.jpg'.
REFRESH IT_OBJBIN[].
REFRESH IT_OBJHEAD[].
*OPEN DATASET FILEPATH FOR INPUT IN TEXT MODE ENCODING NON-UNICODE .
OPEN DATASET FILEPATH FOR INPUT IN BINARY MODE.
READ DATASET FILEPATH INTO XSTR.
CLOSE DATASET FILEPATH.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
BUFFER = XSTR
APPEND_TO_TABLE = C_X
* IMPORTING
* OUTPUT_LENGTH = LENG
TABLES
BINARY_TAB = IT_OBJBIN.
CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET'
EXPORTING
REGION = C_B
IMPORTING
FOLDER_ID = WA_FOL_ID
EXCEPTIONS
COMMUNICATION_FAILURE = 1
OWNER_NOT_EXIST = 2
SYSTEM_FAILURE = 3
X_ERROR = 4
OTHERS = 5.
WA_OBJECT-OBJKEY = '0003170983'.
WA_OBJECT-OBJTYPE = C_BUS2032.
WA_OBJ_DATA-OBJSNS = C_O.
WA_OBJ_DATA-OBJLA = SY-LANGU.
WA_OBJ_DATA-OBJDES = 'sled3'.
WA_OBJ_DATA-FILE_EXT = LV_EXTENSION.
TRANSLATE WA_OBJ_DATA-FILE_EXT TO UPPER CASE.
*WA_OBJ_DATA-OBJLEN = LINES( IT_OBJBIN ) * 255.
WA_OBJ_DATA-OBJLEN = LENG.
CALL FUNCTION 'SO_DOCUMENT_INSERT'
EXPORTING
PARENT_ID = WA_FOL_ID
OBJECT_HD_CHANGE = WA_OBJ_DATA
* OBJECT_FL_CHANGE =
DOCUMENT_TYPE = C_EXT
* OWNER =
IMPORTING
DOCUMENT_ID = WA_OBJ_ID
* OBJECT_HD_DISPLAY =
* OBJECT_FL_DISPLAY =
TABLES
* OBJCONT_TEXT =
OBJCONT_BIN = IT_OBJBIN
* OBJHEAD =
* OBJPARA =
* OBJPARB =
EXCEPTIONS
ACTIVE_USER_NOT_EXIST = 1
DL_NAME_EXIST = 2
FOLDER_NOT_EXIST = 3
FOLDER_NO_AUTHORIZATION = 4
OBJECT_TYPE_NOT_EXIST = 5
OPERATION_NO_AUTHORIZATION = 6
OWNER_NOT_EXIST = 7
PARAMETER_ERROR = 8
SUBSTITUTE_NOT_ACTIVE = 9
SUBSTITUTE_NOT_DEFINED = 10
X_ERROR = 11
OTHERS = 12
.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
IF SY-SUBRC = 0 AND WA_OBJECT-OBJKEY IS NOT INITIAL.
WA_FOLMEM_K-FOLTP = WA_FOL_ID-OBJTP.
WA_FOLMEM_K-FOLYR = WA_FOL_ID-OBJYR.
WA_FOLMEM_K-FOLNO = WA_FOL_ID-OBJNO.
*/Please note: wa_fol_id and wa_obj_id are different work areas
WA_FOLMEM_K-DOCTP = WA_OBJ_ID-OBJTP.
WA_FOLMEM_K-DOCYR = WA_OBJ_ID-OBJYR.
WA_FOLMEM_K-DOCNO = WA_OBJ_ID-OBJNO.
LV_EP_NOTE = WA_FOLMEM_K.
WA_NOTE-OBJTYPE = 'MESSAGE'.
WA_NOTE-OBJKEY = LV_EP_NOTE.
*/Link it
CALL FUNCTION 'BINARY_RELATION_CREATE_COMMIT'
EXPORTING
OBJ_ROLEA = WA_OBJECT
OBJ_ROLEB = WA_NOTE
RELATIONTYPE = C_ATTA
EXCEPTIONS
NO_MODEL = 1
INTERNAL_ERROR = 2
UNKNOWN = 3
OTHERS = 4.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = C_X
* IMPORTING
* RETURN =
.
IF SY-SUBRC EQ 0.
COMMIT WORK.
ENDIF.
ENDIF.
Thanks Tony. Awsome. Helped me attach PDF.
This is awesome. I was stuck with OBJECT_INSERT fm for so long and was having this weird line break issue. But after switching over to SO_DOCUMENT_INSERT and using SOLIX type instead of SOLI it worked perfectly. Thanks a ton.
Hi Harikrishna, Thanks for this blog. It helped a lot. Though we are facing issue in ZIP attachment. Surprisingly some of the compressed docs in the zip are not opening stating error as
Windows Cannot complete the extraction. The destination file could not be created.
Did you ever come across such error? Please help.