Sample program to attach any file from application server to any Business Object

GOS program
Share on Facebook4Share on LinkedIn0Tweet about this on TwitterShare on Google+0
Please Share!!

* Sample program to attach documents to any business object
REPORT z_gos_attachment NO STANDARD PAGE HEADING
LINE-COUNT 132.
*———————————————————————*
* SELECTION SCREEN                                                    *
*———————————————————————*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS: p_file   TYPE localfile,      ” Application File Path
p_objid  TYPE swo_typeid,               ” Any object like material no/vendor
“/customer/po/pr etc
p_bo     TYPE swo_objtyp.                ” Business object like LFA1 for vendor
SELECTION-SCREEN END OF BLOCK b1.
DATA:
li_content   TYPE  STANDARD TABLE OF soli,
li_objhead   TYPE STANDARD TABLE OF soli,
lwa_folmem_k TYPE sofmk,
lwa_note     TYPE borident,
lwa_object   TYPE borident,
lwa_obj_id   TYPE soodk,
lwa_content  TYPE soli,
lwa_fol_id   TYPE soodk,
lwa_obj_data TYPE sood1,
lv_ep_note   TYPE borident-objkey,
lv_lifnr     TYPE lifnr,
lv_file      TYPE string,
lv_filename  TYPE c LENGTH 100, ” file name and ext
lv_extension TYPE c LENGTH 4. ” extension only
*   Refresh data
REFRESH: li_content[], li_objhead[].

*   Open corresponding DMS files
OPEN DATASET p_file FOR INPUT IN BINARY MODE.
IF sy-subrc EQ 0.
WHILE sy-subrc = 0.
READ DATASET p_file INTO lwa_content.
* Do not put Sy-subrc eq 0 here. Please add the last line of the file,
* though sy-subrc may fail
APPEND  lwa_content TO li_content.
ENDWHILE.
* Close file
CLOSE DATASET p_file.
* In this example we are attaching in Vendor.
* Converting according to your requirement.. For example for Material,
* it should be 18 chars
* Convert Vendor
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT’
EXPORTING
input  = p_objid
IMPORTING
output = lv_lifnr.

* Convert to BIN
CALL FUNCTION ‘SO_CONVERT_CONTENTS_BIN’
EXPORTING
it_contents_bin = li_content[]
IMPORTING
et_contents_bin = li_content[].

* Get folder id
CALL FUNCTION ‘SO_FOLDER_ROOT_ID_GET’
EXPORTING
region                = ‘B’
IMPORTING
folder_id             = lwa_fol_id
EXCEPTIONS
communication_failure = 1
owner_not_exist       = 2
system_failure        = 3
x_error               = 4
OTHERS                = 5.
* Sy-subrc check not required
* Keeping file in string data type
lv_file = p_file.
* You may not need this step. But  no harm in adding this
* Get file name and extension
CALL FUNCTION ‘CH_SPLIT_FILENAME’
EXPORTING
complete_filename = lv_file
IMPORTING
extension         = lv_extension
name_with_ext     = lv_filename
EXCEPTIONS
invalid_drive     = 1
invalid_path      = 2
OTHERS            = 3.
IF sy-subrc EQ 0.
* Object header
CLEAR lwa_content.
CONCATENATE ‘&SO_FILENAME=’ lv_filename INTO lwa_content.
APPEND lwa_content TO li_objhead.
CLEAR lwa_content.
ENDIF.

lwa_object-objkey  = lv_lifnr.
* For example, business object name for PO is BUS2012,
* business object for PR is BUS2105,
* business object for Vendor is LFA1 etc
lwa_object-objtype = p_bo.
*        lwa_object-logsys  = lv_logical_system.

lwa_obj_data-objsns = ‘O’.
lwa_obj_data-objla = sy-langu.
lwa_obj_data-objdes = ‘Attachment by Raju Shrestha’.  .
lwa_obj_data-file_ext = lv_extension.

TRANSLATE lwa_obj_data-file_ext TO UPPER CASE.
* This is very important step. If your object size does not match with the input
* file size, then your object might get attached, but it will show error while you
* try to open it.
* If you have a way, where you can read the input file size directly, then assign
* it directly else, use the below formula
lwa_obj_data-objlen =  lines( li_content ) * 255.

* Insert data
CALL FUNCTION ‘SO_OBJECT_INSERT’
EXPORTING
folder_id                  = lwa_fol_id
object_type                = ‘EXT’
object_hd_change           = lwa_obj_data
IMPORTING
object_id                  = lwa_obj_id
TABLES
objhead                    = li_objhead
objcont                    = li_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 lwa_object-objkey IS NOT INITIAL.
lwa_folmem_k-foltp = lwa_fol_id-objtp.
lwa_folmem_k-folyr = lwa_fol_id-objyr.
lwa_folmem_k-folno = lwa_fol_id-objno.

* Please note: lwa_fol_id and lwa_obj_id are different work areas

lwa_folmem_k-doctp = lwa_obj_id-objtp.
lwa_folmem_k-docyr = lwa_obj_id-objyr.
lwa_folmem_k-docno = lwa_obj_id-objno.

lv_ep_note = lwa_folmem_k.
lwa_note-objtype = ‘MESSAGE’.
*          lwa_note-logsys    = lv_logical_system.
lwa_note-objkey = lv_ep_note.

* Link it
CALL FUNCTION ‘BINARY_RELATION_CREATE_COMMIT’
EXPORTING
obj_rolea      = lwa_object
obj_roleb      = lwa_note
relationtype   = ‘ATTA’
EXCEPTIONS
no_model       = 1
internal_error = 2
unknown        = 3
OTHERS         = 4.
IF sy-subrc EQ 0.
* Commit it
COMMIT WORK.
WRITE:/ ‘Attached successfully’.
ENDIF.
ELSE.
MESSAGE ‘Error while opening file’ TYPE ‘I’.
LEAVE LIST-PROCESSING.
ENDIF.
ENDIF.

Share on Facebook4Share on LinkedIn0Tweet about this on TwitterShare on Google+0
Please Share!!

About the Author

SAP Yard
SAP Yard
SAPYard is one stop page for all Technical Folks in SAP. You would find un-conventional explanations, tutorials, and tricks. Please like our Facebook Page and also join our LinkedIn Group.

8 Comments on "Sample program to attach any file from application server to any Business Object"

  1. Hi, What are the typical scenarios when we need to attach something in a business object?

    • Hi Sandip – Thank you for visiting our page and leaving your message.

      Case 1:
      Client wanted to move all the attachments in the Contract BO (ME33K) for the Vendor to the Vendor Master BO (XK03).

      Case 2:
      Client acquired a new company which has SAP. Now the client wanted to copy all the attachements from the PO Business Object (ME23N) of the new company to the clients PO BO in the client’s existing SAP System.

      Please let us know if we need to provide any other clarification.

      Regards,
      Raju.

  2. Hi Thanks for posting, very useful document ,I have used the above code and it works fine for a text file.However when i try to pass a csv or excel , incorrect data or wrong format of data is sent to sap office.Can you help me out?

    • Dear Jennifer – Usually we get that error when the length/size of file is sent incorrectly. If you could share your test csv/excel file, we could check and try to let you know the loophole.

      Thanks for your feedback.

      Team SAPYard.

  3. hello Great idea but how do you get the DMS file path?
    Regards

    • Dear Crepy – You can find the DMS file path in table DRAW (Document Info Record) field name FILEP (Original of document).

      Please check and let us know if you have futher questions.

      Regards,
      Team SAPYard.

  4. Thanks Andrew.. Glad it was useful to you..

    Regards,
    Raju.

  5. Andrew Strauss | August 26, 2014 at 9:29 am | Reply

    Very useful SAP tutorial. Thanks for sharing.

Leave a comment

Your email address will not be published.


*