This weblog is a sequel to my most successful weblog till date. [ was quoted in at least 2 Forum threads, only once by myself ;-) ]. Just trying to capitalize on it.

I tried to clarify a few points about attaching URL, Notes & documents in background using GOS basics in my earlier weblog.

Generic object services (GOS) - in Background .


However, I received a few queries from the readers who wanted the code for attaching documents in background [ Also for 4.6C ].

The code for it, might well have been discussed in the forums since my earlier weblog was published but I would like to present the code here for attaching documents like DOC, PDF, GIF etc. Last time I cheated my readers with 'only funda but no code' for document attachments.

The code from my earlier weblog was converted to suit 4.6C by one of my weblog readers [ Madeleine Kempka / Kempka Madeleine - I am not sure about the sequence of first & last name :-) ] and then I corrected a few problems with it for attaching PDF and other documents.

Points to remember:

  • For file attachments: Pass the file path on application server and also choose correct message type [ P_MSGTYP ] as per the file extension[e.g. PDF,DOC etc]. P_RELTYP should be 'ATTA'. Also file size should be calculated and passed while creating the message.
  • For URLs: P_RELTYP should be 'URL' & P_MSGTYP will be derived as 'URL'.
  • For Notes: P_RELTYP should be 'NOTE' & P_MSGTYP will be derived as 'RAW'.

Sample Code:
In case you find bugs in the code below...rememeber, it's just a sample...real one contains too many ;-)
REPORT  Z_RMTIWARI_ATTACH_DOC_TO_BO_46          .*---------------------------------------------------------------------** Function   : We need to maintain links between Business Object and*              the attachment.Attachment document is basiclally a*              business object of type 'MESSAGE'.In order to maintain*              links, first the attachment will be crated as Business*              Object of type 'MESSAGE' using Message.Create method.**              This program can be used to attach PC documents eg. PDF*              ,DOC,TXT,GIF etc. to a SAP Business Object e.g Purchase*              Order etc. You should pass the correct message type*              [ P_MSGTYP ] as per the file extension[e.g. PDF,DOC etc].**              The code below is suitable for 4.6C+, though you might*              like to use the commented code, instead, for 4.7+*---------------------------------------------------------------------** Include for BO macros  INCLUDE : &ltcntn01&gt.* Load class.*  CLASS CL_BINARY_RELATION definition load.  PARAMETERS:*  Object_a*   P_BOTYPE LIKE obl_s_pbor-typeid DEFAULT 'BUS2031', " e.g. 'BUS2012'*   P_BO_ID  LIKE OBL_S_PBOR-INSTID DEFAULT '0002029816',   P_BOTYPE LIKE borident-OBJTYPE DEFAULT 'BUS2031', " e.g. 'BUS2012'   P_BO_ID  LIKE borident-OBJKEY DEFAULT '0002029816',                                                    " Key e.g. PO No.*  Object_b   P_MSGTYP LIKE SOFM-DOCTP  DEFAULT 'PDF',   P_DOCTY  LIKE borident-OBJTYPE DEFAULT 'MESSAGE',*  Relationship   P_RELTYP  LIKE BRELTYP-RELTYPE DEFAULT 'ATTA',*  File Name   P_FNAME like rlgrap-filename Default '/usr/data/Test.pdf'.  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 : LV_MESSAGE_KEY type TY_MESSAGE_KEY.  DATA : LO_MESSAGE type SWC_OBJECT.  DATA : LT_DOC_CONTENT type standard table of SOLI-LINE                             with header line.*----------------------------------------------------------------------** First derive the Attachment's ( MESSAGE )document type.  P_DOCTY = 'MESSAGE'.  CASE P_RELTYP.*   In case of URls    WHEN 'URL'.      P_MSGTYP = 'URL'.*   In case of Notes / Private Notes    WHEN 'NOTE' OR 'PNOT'.      P_MSGTYP = 'RAW'.    WHEN 'ATTA'.*     Take given parameter e.g. 'DOC', 'PDF' etc.*     P_MSGTYP = 'EXT'.    WHEN OTHERS.*    ....exit    EXIT.  ENDCASE.*----------------------------------------------------------------** Create an initial instance of BO 'MESSAGE' - to call the* instance-independent method 'Create'.  swc_create_object LO_MESSAGE 'MESSAGE' LV_MESSAGE_KEY.* define container to pass the parameter values to the method call* in next step.  swc_container LT_MESSAGE_CONTAINER.* Populate container with parameters for method  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTTITLE' 'TestDocument'.  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTLANGU' 'E'.  swc_set_element LT_MESSAGE_CONTAINER 'NO_DIALOG'     'X'.  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTNAME'   P_DOCTY.  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTTYPE'   P_MSGTYP.* In case of URLs..it should be concatenated with &KEY& in the begining.  CASE P_MSGTYP.  WHEN 'URL'.    LT_DOC_CONTENT = '&KEY&http://www.rmtiwari.com' .    append LT_DOC_CONTENT.* In case of Notes or Private Notes, get the data from files on appl* server or from wherever(? - remember background).  WHEN 'RAW'.    LT_DOC_CONTENT = 'Hi How r u?' .    append LT_DOC_CONTENT.* In case of PC File attachments  WHEN OTHERS.    OPEN DATASET P_FNAME FOR INPUT.    IF SY-subrc EQ 0.      DO.        READ DATASET P_FNAME INTO LT_DOC_CONTENT.        IF SY-subrc EQ 0.          append LT_DOC_CONTENT.        ELSE.          EXIT.        ENDIF.      ENDDO.      CLOSE DATASET P_FNAME.    ENDIF.  ENDCASE.* 'DocumentContent' is a multi-line element ( itab ).  swc_set_table LT_MESSAGE_CONTAINER 'DocumentContent' LT_DOC_CONTENT.* Size is required in case of File attachments  data : LV_DOC_SIZE type i.  data : L_FILE_LINES type i.  DESCRIBE TABLE LT_DOC_CONTENT LINES L_FILE_LINES.  READ TABLE LT_DOC_CONTENT INDEX L_FILE_LINESLV_DOC_SIZE = ( 255 * ( L_FILE_LINES - 1 ) ) +              STRLEN( LT_DOC_CONTENT ).  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTSIZE'   LV_DOC_SIZE .* Refresh to get the reference of create 'MESSAGE' object for attachment  swc_refresh_object LO_MESSAGE.  swc_call_method LO_MESSAGE 'CREATE' LT_MESSAGE_CONTAINER.* Get Key of new object  swc_get_object_key LO_MESSAGE LV_MESSAGE_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* data: LO_IS_OBJECT_A type SIBFLPORB.  "type SIBFLPORB is unknown, so I  data: LO_IS_OBJECT_A type BORIDENT.  LO_IS_OBJECT_A-OBJKEY = P_BO_IDLO_IS_OBJECT_A-OBJTYPE = P_BOTYPE.*  LO_IS_OBJECT_A-CATID  = 'BO'.* Create attachment BO object_b* data: LO_IS_OBJECT_B type SIBFLPORB.    "type SIBFLPORB is unknown  data: LO_IS_OBJECT_B type BORIDENT.  LO_IS_OBJECT_B-OBJKEY = LV_MESSAGE_KEYLO_IS_OBJECT_B-OBJTYPE = P_DOCTY.*  LO_IS_OBJECT_B-CATID  = 'BO'.*TRY.*CALL METHOD CL_BINARY_RELATION=&gtCREATE_LINK*  EXPORTING*    IS_OBJECT_A            = LO_IS_OBJECT_A*    IS_OBJECT_B            = LO_IS_OBJECT_B*    IP_RELTYPE             = P_RELTYP.call function 'BINARY_RELATION_CREATE'  EXPORTING    obj_rolea    = LO_IS_OBJECT_A    obj_roleb    = LO_IS_OBJECT_B    relationtype = P_RELTYP  EXCEPTIONS    others       = 1.* Check if everything OK...who cares!!commit work.

 

This is about a simple ABAP Utility . It will post ABAP object's documentation to Google, for the purpose of displaying translated text in your logon language or in English.

You can also use this weblog for extending workbench functionality.




The Problem :

Not sure if you've faced this problem but I found that quite a few of the function modules are not provided with English documentation - however documentation in German does exist for most of these.




Actually I made the problem afterwards. Frankly speaking, I didn't write this utility because I wanted to solve any problem. It was just for the fun !!

[ Now where is the fun part? - keep on reading, it's in The End ]



Benefits :

You can write a weblog on SDN about it.
    1. It will extend the functionality of the ABAP workbench function[-code] "documentation"
, to get the documentation translated online using Google.

It translates into (other) logon language - if documentation exist in English.
    1. Translates into English - if documentation exists only in German.
When no documentation exist at all then you can be sure about that immediately.







Processing :

You can run the report program directly from SE38 as well . But it will be good if you can embed the functionality in ABAP Workbench itself .



Implementation for BADI WB_PROCESS_FCODE -Method DISPATCH

: It calls the main utility program Z_RMTIWARI_ABAP_DOC_TRANSLATOR, whenever someone clicks on the 'Documentation' of ABAP Workbench Menu.. 



method IF_EX_WB_PROCESS_FCODE~DISPATCH . Data : lv_OBJECT type DOKHL-OBJECT. Data : lv_obj_id type DOKHL-ID. data : lv_langp  type char5. data : lv_typ    type c. data : lt_dokil type standard table of dokil. data : wa_dokil type dokil.

 

*P_OK_CODE Check P_OK_CODE eq 'WB_DOCUMENTATION'. break-point.   IF sy-tcode eq 'SE37'.    get parameter id 'LIB' field lv_OBJECT.    lv_obj_id = 'FU'.    lv_typ = 'T'.

 

  ELSEIF sy-tcode eq 'SE38'.     get parameter id 'RID' field lv_OBJECT.     lv_obj_id = 'RE'.     lv_typ = 'E'.

 

  ELSE.

 

  • Not implemented as yet

 

  ENDIF.

 

 

   select *     into table lt_dokil     from DOKIL    where ID     eq lv_obj_id      and OBJECT eq lv_OBJECT      and TYP    eq lv_typ.

 

    check sy-subrc eq 0.

 

    READ TABLE lt_dokil into wa_dokil with key langu = sy-langu.

 

    check sy-subrc ne 0.

 

    READ TABLE lt_dokil into wa_dokil with key langu = 'E'.

 

     if sy-subrc eq 0 .

 

  •       Translate from english to logon language
        concatenate 'en|' sy-langu into lv_langp.

 

 

     else.
  •      Get German and translate to English.
       lv_langp = 'de|en'.

 

     endif.

 

     SUBMIT Z_RMTIWARI_ABAP_DOC_TRANSLATOR AND RETURN        WITH   P_ID       = lv_obj_id        WITH   P_OBJECT   = lv_object        WITH   P_LANGP    = lv_langp.

 

 

 

endmethod.




Program Z_RMTIWARI_ABAP_DOC_TRANSLATOR :

The program accepts inputs for the function module name & the language pair in. It simply gets the available documentation of the object and then generates the HTML code for Google translation FORM. The HTML Page gets downloaded to ‘C:\Temp’ folder on your PC and subsequently called by the program in your default browser.

 

 

REPORT  Z_RMTIWARI_ABAP_DOC_TRANSLATOR          . ----
  •       ABAP Document Translator - using Google - Date - 18.02.2006
----
  •       Written By: Ram Manohar Tiwari                     
----
----
  • This utility will accept ABAP documentation objects and will submit
  • the text to google in order to  translate it from one language to
  • other.
*
  • This utility is useful for translating the function module and other
  • ABAP Object's documenations from German to English, in case it is
  • only available in German.
*
  • You can also translate the documentation from english to any other
  • (available) language,in case, for you, English is as good as German.
*----
  • This utility is developed on MiniSAP .
*----
  • SELECTION SCREEN
*----
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001. PARAMETERS : P_ID            LIKE     DOKHL-ID DEFAULT 'FU' OBLIGATORY,              P_LANGU            LIKE     SY-LANGU DEFAULT 'D'  NO-DISPLAY,              P_OBJECT     LIKE     DOKHL-OBJECT OBLIGATORY,              P_LANGP        TYPE CHAR5  default 'de|en' OBLIGATORY. SELECTION-SCREEN END OF BLOCK b1 .

 

SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME TITLE text-002.

 

SELECTION-SCREEN BEGIN OF LINE. PARAMETERS: P_OUTDIR LIKE rlgrap-filename obligatory default 'C:\temp'. SELECTION-SCREEN END OF LINE. SELECTION-SCREEN END OF BLOCK B2.

 

----
  • Data declaration
----
data : gv_directory LIKE rlgrap-filename,        gv_file_name TYPE STRING,        gv_html_file_name(100) TYPE C.

 

data : lt_doc_text type standard table of TLINE with header line.

 

DATA : BEGIN OF gt_html OCCURS 0,         rec(200) TYPE c,        END OF gt_html.

 

DATA : gv_html_wa        like line of gt_html.

 

----
DATA: begin of lt_VALUETAB occurs 0,        P_LANGP type char5,        DESC    type char100,       end of lt_VALUETAB .

 

data : lt_return_tab type standard table of DDSHRETVAL with header line. data : lv_prog type sy-repid,        lv_dynnr type sy-dynnr.

 

 

 

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_LANGP.

 

  CLEAR lt_valuetab. REFRESH lt_valuetab.

 

  lv_prog = sy-repid.   lv_dynnr = sy-dynnr.

 

  lt_valuetab-P_LANGP = 'de|en'.   lt_valuetab-DESC = 'German to English'.   APPEND lt_valuetab.

 

  lt_valuetab-P_LANGP = 'de|fr'.   lt_valuetab-DESC = 'German to French'.   APPEND lt_valuetab.

 

  lt_valuetab-P_LANGP = 'en|de'.   lt_valuetab-DESC = 'English to German'.   APPEND lt_valuetab.

 

  lt_valuetab-P_LANGP = 'en|es'.   lt_valuetab-DESC = 'English to Spanish'.   APPEND lt_valuetab.

 

  lt_valuetab-P_LANGP = 'en|fr'.   lt_valuetab-DESC = 'English to French'.   APPEND lt_valuetab.

 

  lt_valuetab-P_LANGP = 'en|it'.   lt_valuetab-DESC = 'English to Italian'.   APPEND lt_valuetab.

 

  lt_valuetab-P_LANGP = 'en|pt'.   lt_valuetab-DESC = 'English to Portuguese'.   APPEND lt_valuetab.

 

  lt_valuetab-P_LANGP = 'en|ja'.   lt_valuetab-DESC = 'English to Japanese BETA'.   APPEND lt_valuetab.

 

  lt_valuetab-P_LANGP = 'en|ko'.   lt_valuetab-DESC = 'English to Korean BETA'.   APPEND lt_valuetab.

 

  lt_valuetab-P_LANGP = 'en|cn'.   lt_valuetab-DESC = 'English to Chinese(Simplified) BETA'.   APPEND lt_valuetab.

 

 

  • Call the help value screen
  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'        EXPORTING             retfield        = 'P_LANGP'             dynpprog        = lv_prog             dynpnr          = lv_dynnr             dynprofield     = 'P_LANGP'             value_org       = 'S'        TABLES             value_tab       = lt_VALUETAB             RETURN_TAB      = lt_return_tab        EXCEPTIONS             parameter_error = 1             no_values_found = 2             OTHERS          = 3.   IF sy-subrc <> 0.
  •    ...
  ENDIF.

 

 

START-OF-SELECTION.

 

P_LANGU = P_LANGP(1).
  • Get the document text
  CALL FUNCTION 'DOCU_GET'     EXPORTING
  •     EXTEND_EXCEPT                = ' '
      ID                           = P_ID       LANGU                        = P_LANGU       OBJECT                       = P_OBJECT
  •     TYP                          = 'E'
  •     VERSION                      = 0
  •     VERSION_ACTIVE_OR_LAST       = 'L'
  •     PRINT_PARAM_GET              = 'X'
  •   IMPORTING
  •     DOKSTATE                     =
  •     DOKTITLE                     =
  •     HEAD                         =
  •     DOKTYP                       =
    TABLES       LINE                         = lt_doc_text    EXCEPTIONS      NO_DOCU_ON_SCREEN            = 1      NO_DOCU_SELF_DEF             = 2      NO_DOCU_TEMP                 = 3      RET_CODE                     = 4      OTHERS                       = 5             .   IF SY-SUBRC <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO          WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.   ENDIF.

 

 

  • Create the google html form for the document text.
  PERFORM convert_doc_to_html TABLES  lt_doc_text                                       gt_html.

 

  Concatenate 'Trans_' P_OBJECT '.html' into  gv_html_file_name.

 

 

  PERFORM prepare_file_name   USING   p_outdir                                       gv_html_file_name                            CHANGING   gv_file_name.

 

  PERFORM download_html_file_on_pc tables    gt_html                                     using    gv_file_name.

 

  PERFORM show_html_file using gv_file_name.

 

 

 

END-OF-SELECTION.

 

&----
*&      Form  prepare_file_name &----
  •       text
----
  •      -->x_directory    text
  •      -->x_program_name text
  •      -->y_file_name    text
----
FORM prepare_file_name USING x_directory                              x_file                     CHANGING y_file_name.

 

  CONCATENATE x_directory '\' x_file into y_file_name.

 

 

 

ENDFORM.                    " prepare_file_name &----
*&      Form  download_html_file_on_pc &----
  •       text
----
  • -->  p1        text
  • <--  p2        text
----
FORM download_html_file_on_pc tables   yt_download                               using    x_outfile type string.

 

 

  • Use gui_download if file is located on the local PC.
  • WS_download only works in foreground
  IF sy-batch EQ 'X'.     MESSAGE e001(AQ) WITH    'This program cannot be executed in background'.
  •   ERROR: Unable to download locally stored files when running in
  •   background
  ELSE.

 

    CALL FUNCTION 'GUI_DOWNLOAD'       EXPORTING         FILENAME                = x_outfile         FILETYPE                = 'ASC'       TABLES         DATA_TAB                = yt_download       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.
  • Status of download
    CASE sy-subrc.       WHEN 0.         MESSAGE I002(AQ) WITH         'HTML page downloaded as ' x_outfile.

 

      WHEN OTHERS.
  •        Upload unsuccessful - error message
        MESSAGE ID SY-MSGID TYPE 'E' NUMBER SY-MSGNO            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.     ENDCASE.   ENDIF.

 

ENDFORM.                    " download_html_file_on_pc &----
*&      Form  convert_code_to_html &----
  •       text
----
  •      -->P_GT_REP_TABLE  text
  •      -->P_GT_HTML  text
----
FORM convert_doc_to_html  TABLES xt_REP_TABLE structure TLINE                                   yt_HTML.

 

  DEFINE add_html.     yt_html = &1.     APPEND yt_html.

 

  END-OF-DEFINITION.

 

  • Add Header to HTML
  add_html ''           into lv_langp_html. endif.

 

  • Remove the space in < /textarea> - this is for SDN weblog
add_html '< /textarea>'.
add_html lv_langp_html.
add_html '
'.
add_html ''.
add_html ''.
add_html ''.
add_html '
'.

 

 

  • Close HTML Tags
  add_html '</BODY>'.   add_html '</HTML>'.

 

 

 

ENDFORM.                    " convert_code_to_html &----
*&      Form  show_html_file &----
  •       text
----
  •      -->X_FILE_NAME  text
----
FORM show_html_file  USING    X_FILE_NAME.

 

data : lv_url(200) type c.

 

lv_url = x_file_name. CALL FUNCTION 'CALL_BROWSER'   EXPORTING     URL                          = lv_url.

 

 

ENDFORM.                    " show_html_file





The Result:

For example : You can open a function module say ABAP_DOCU_SHOW using transaction SE37 . Further click on Menu Function .

 

Since the BADI is implemented for function code 'documentation', the utility will be called and Google translated text from German to English will be shown in your default browser.


 

Google Also there seems to be some problem with the google translation and it does not respect end-of-sentence or line-breaks http://www.worldlingo.com/ could have done better . That makes the translated text look like some kind of garbage. Also, there seems to be some encoding related issue. But that should not be very difficult to resolve. </P>

Actions