Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos

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.
42 Comments