Problem solve Get help with specific problems with your technologies, process and projects.

Display file attachments

Do you have any requirements to print the filenames of the attached files from a document? This tip explains how.

Do you have any requirements to print the filenames of the attached files from a document such as Purchase Order or other SAP objects on a SAPscript or SmartForms? When you attached a PC document to your Purchase Order, Purchase Contract, RFQ etc., these file attachment documents are not part of the object tables hierarchy. In the case of PO, they are not in PO tables such as EKKO, EKPO, EKBE, etc. Instead, they are part of the SAP Office framework. So in order to get this information, which is associated to a particular object, you need to go through different tables. The example below pertains to a Purchase Order document, however, it can be modified to accommodate other objects. Just change the OBJTYPE-column value in SRRELROLES table. You can take a look at your object types in Business Object Builder (SWO1).
The code was written in 4.6c but has successfully tested on 4.7 as well.

REPORT yeb_get_file_attachment.

* This program will get the filenames of the
* attached files in a Purchase Order.

PARAMETERS:     p_ponbr(10) TYPE c.

TABLES: srrelroles.
DATA: w_roleid(22)  TYPE c,
      w_objkey(70)  TYPE c,
      w_foltp(3)    TYPE c,
      w_folyr(2)    TYPE c,
      w_folno(12)   TYPE c,
      w_doctp(3)    TYPE c,
      w_docyr(2)    TYPE c,
      w_docno(12)   TYPE c,
      w_objdes(50)  TYPE c.

TYPES: BEGIN OF ty_binrel,
         roleb(22) TYPE c,
       END OF ty_binrel.

DATA: it_binrel       TYPE TABLE OF ty_binrel.
DATA: w_roleb         LIKE LINE OF it_binrel.

* Get role id
SELECT roleid
  FROM srrelroles
  INTO w_roleid
  WHERE objkey  = p_ponbr        "Purchase Order nbr
    AND objtype = 'BUS2012'.     "Purchase Order object

IF sy-subrc = 4.
  MESSAGE i899(f2)
  WITH 'No File Attachments for ' p_ponbr.

* Get role ids given the primary role id
SELECT role_b
  FROM srgbinrel
  INTO TABLE it_binrel
  WHERE role_a   = w_roleid
    AND breltyp IN ('NOTE', 'URL', 'ATTA').

* Get object key from SRRELROLES using role B
LOOP AT it_binrel INTO w_roleb.

    FROM srrelroles
    INTO w_objkey
    WHERE roleid   = w_roleb.

* Prepare key fields for SOOD table
*  w_foltp = w_objkey(3).
*  w_folyr = w_objkey+3(2).
*  w_folno = w_objkey+5(12).
  w_doctp = w_objkey+17(3).
  w_docyr = w_objkey+20(2).
  w_docno = w_objkey+22(12).

    FROM sood
    INTO w_objdes
    WHERE objtp = w_doctp
      AND objyr = w_docyr
      AND objno = w_docno.

  WRITE: / w_objdes.

Dig Deeper on SAP development and programming languages