09-24-2009 12:31 PM
Hi all.
I have a folder with a lot of documents that I have to load like a GOS (That thing in PA30 AKA Attachments to the PERNR). The pernr will be part of the name of each file, so we can get the this data from there.
What I need is any kind of batch input or something to upload all this stuff.
Thanks in advance...
09-24-2009 4:12 PM
Here is a function that you can use. You can either pass the binary table, or pass the filepath and it will upload from the frontend.
Code Part 1
function z_gos_create_attachment.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" REFERENCE(OBJTYPE) TYPE BORIDENT-OBJTYPE
*" REFERENCE(OBJKEY) TYPE BORIDENT-OBJKEY
*" REFERENCE(FILEPATH) TYPE STRING OPTIONAL
*" REFERENCE(FILENAME) TYPE STRING OPTIONAL
*" REFERENCE(DOCTITLE) TYPE STRING OPTIONAL
*" REFERENCE(DOCTYPE) TYPE SOFM-DOCTP OPTIONAL
*" TABLES
*" BIN_TAB OPTIONAL
*" EXCEPTIONS
*" UNKNOWN_ERROR
*" NO_FILE_OR_BIN
*" NO_DOC_TYPE
*"----------------------------------------------------------------------
include : <cntn01>.
constants:
c_docnm type borident-objtype value 'MESSAGE',
c_reltyp type breltyp-reltype value 'ATTA'.
types: begin of ty_message_key,
foltp type so_fol_tp,
folyr type so_fol_yr,
folno type so_fol_no,
doctp type so_doc_tp,
docyr type so_doc_yr,
docno type so_doc_no,
fortp type so_for_tp,
foryr type so_for_yr,
forno type so_for_no,
end of ty_message_key.
data : xobject_b type borident.
data : xobject_a type borident.
data : xdoc_size type i.
data : xfile_lines type i.
data : xmessage_key type ty_message_key.
data : xmessage type swc_object.
data : idoc_content type standard table of soli-line.
data : xdoc_content type soli-line.
data : xfilenamepath type string.
data : xdoctitle type string.
data : xdoctype type sofm-doctp.
data : xfname type string.
if bin_tab[] is initial
and ( filename is initial
or filepath is initial ).
raise no_file_or_bin.
endif.
if not bin_tab[] is initial
and doctype is initial.
raise no_doc_type.
endif.
if not bin_tab[] is initial.
xdoctype = doctype.
translate xdoctype to upper case.
else.
split filename at '.' into xfname xdoctype.
translate xdoctype to upper case.
endif.
Regards,
Rich Heilman
09-24-2009 4:12 PM
Here is a function that you can use. You can either pass the binary table, or pass the filepath and it will upload from the frontend.
Code Part 1
function z_gos_create_attachment.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" REFERENCE(OBJTYPE) TYPE BORIDENT-OBJTYPE
*" REFERENCE(OBJKEY) TYPE BORIDENT-OBJKEY
*" REFERENCE(FILEPATH) TYPE STRING OPTIONAL
*" REFERENCE(FILENAME) TYPE STRING OPTIONAL
*" REFERENCE(DOCTITLE) TYPE STRING OPTIONAL
*" REFERENCE(DOCTYPE) TYPE SOFM-DOCTP OPTIONAL
*" TABLES
*" BIN_TAB OPTIONAL
*" EXCEPTIONS
*" UNKNOWN_ERROR
*" NO_FILE_OR_BIN
*" NO_DOC_TYPE
*"----------------------------------------------------------------------
include : <cntn01>.
constants:
c_docnm type borident-objtype value 'MESSAGE',
c_reltyp type breltyp-reltype value 'ATTA'.
types: begin of ty_message_key,
foltp type so_fol_tp,
folyr type so_fol_yr,
folno type so_fol_no,
doctp type so_doc_tp,
docyr type so_doc_yr,
docno type so_doc_no,
fortp type so_for_tp,
foryr type so_for_yr,
forno type so_for_no,
end of ty_message_key.
data : xobject_b type borident.
data : xobject_a type borident.
data : xdoc_size type i.
data : xfile_lines type i.
data : xmessage_key type ty_message_key.
data : xmessage type swc_object.
data : idoc_content type standard table of soli-line.
data : xdoc_content type soli-line.
data : xfilenamepath type string.
data : xdoctitle type string.
data : xdoctype type sofm-doctp.
data : xfname type string.
if bin_tab[] is initial
and ( filename is initial
or filepath is initial ).
raise no_file_or_bin.
endif.
if not bin_tab[] is initial
and doctype is initial.
raise no_doc_type.
endif.
if not bin_tab[] is initial.
xdoctype = doctype.
translate xdoctype to upper case.
else.
split filename at '.' into xfname xdoctype.
translate xdoctype to upper case.
endif.
Regards,
Rich Heilman
09-24-2009 4:15 PM
Code Part 2.
* Create an initial instance of BO 'MESSAGE' - to call the
* instance-independent method 'Create'.
swc_create_object xmessage 'MESSAGE' xmessage_key.
* define container to pass the parameter values to the method call
* in next step.
swc_container imessage_container.
* Populate container with parameters for method
if doctitle is initial.
xdoctitle = filename.
else.
xdoctitle = doctitle.
endif.
swc_set_element imessage_container 'DOCUMENTTITLE' xdoctitle.
swc_set_element imessage_container 'DOCUMENTLANGU' 'E'.
swc_set_element imessage_container 'NO_DIALOG' 'X'.
swc_set_element imessage_container 'DOCUMENTNAME' c_docnm.
swc_set_element imessage_container 'DOCUMENTTYPE' xdoctype.
if not bin_tab[] is initial.
* Translate table to 255, if not 255.
data: tab_len type i.
read table bin_tab index 1.
tab_len = strlen( bin_tab ).
if tab_len < 255.
data: xbuffer type string.
loop at bin_tab.
translate bin_tab using ' ~'.
concatenate xbuffer bin_tab into xbuffer.
endloop.
translate xbuffer using '~ '.
do.
xdoc_content = xbuffer.
append xdoc_content to idoc_content.
shift xbuffer left by 255 places.
if xbuffer is initial.
exit.
endif.
enddo.
else.
idoc_content[] = bin_tab[].
endif.
else.
concatenate filepath filename into xfilenamepath.
* Upload file from frontend
call function 'GUI_UPLOAD'
exporting
filename = xfilenamepath
filetype = 'BIN'
tables
data_tab = idoc_content
exceptions
others = 17.
endif.
Regards,
Rich Heilman
09-24-2009 4:16 PM
Code Part 3. Ridiculous that I have to do this.
* 'DocumentContent' is a multi-line element ( itab ).
swc_set_table imessage_container 'DocumentContent' idoc_content.
* Size is required in case of File attachments
describe table idoc_content lines xfile_lines.
read table idoc_content into xdoc_content index xfile_lines.
xdoc_size = ( 255 * ( xfile_lines - 1 ) ) +
strlen( xdoc_content ).
swc_set_element imessage_container 'DOCUMENTSIZE' xdoc_size .
* Refresh to get the reference of create 'MESSAGE' object for attachment
swc_refresh_object xmessage.
swc_call_method xmessage 'CREATE' imessage_container.
* Get Key of new object
swc_get_object_key xmessage xmessage_key.
* Now we have attachment as a business object instance. We can now
* attach it to our main business object instance.
* Create main BO object_a
xobject_a-objkey = objkey.
xobject_a-objtype = objtype.
* Create attachment BO object_b
xobject_b-objkey = xmessage_key.
xobject_b-objtype = c_docnm.
call function 'BINARY_RELATION_CREATE'
exporting
obj_rolea = xobject_a
obj_roleb = xobject_b
relationtype = c_reltyp
exceptions
others = 1.
if sy-subrc = 1.
raise unknown_error.
endif.
commit work.
endfunction.
Regards,
Rich Heilman
08-11-2012 9:52 AM
Hi Rich,
Thanks for the code , i am able to attach files to the vendor master with the help of your code but i am facing an issue that the uploaded file shows garbage values , whereas if i use the services for object method manually the file is being open without any problem. Please suggest.
Thanks
08-11-2012 11:09 AM
Hi ,
Solved it , the document size was not getting properly calculated , i dont know why though, i used the importing parameter of GUI_UPLOAD F.M. filelength and pass this in xdoc_size , rather then calculating it.
Thanks
10-15-2014 9:44 AM
03-04-2016 6:48 PM
Hi Rick ,
I am using your code and its working fine for doc and ppt document types but its not loading DOCX , xlsx and pptx .What can be the reason and what should be the solution ?
Thanks,
Nikita
08-17-2015 5:07 PM
Hi Carlos
Kindly could you please attach snapshot of the program ,As i have the same requirements
Regards
Ghadeer