Wednesday, March 5, 2014

How to add a GOS Attachment to a Business objects

Both SAP ECC and CRM offer the opportunity to add so called GOS attachments to any object in SAP. These attachments are usually added to Quotations, Sales orders or Business partners (for example, a photograph of the Business Partner).

In this blog I will show how to manage these attachment in an ABAP Object Oriented way.

But first, let start one level higher: GOS stands for Generic Object Services and offers some generic functionality to any business object. Amongst others, these services are*:
  • Creating personal notes on an object
  • Starting a workflow
  • Adding and removing attachments to an object
The GOS framework is flexible and even allows you to use your own custom business object. See this link for more information on how to achieve this.

Show me the code

Today we will focus on managing attachments in ABAP. The example code uses Sales Orders, but it is easy to adapt the example to other types of objects (even your own object types). The code is split into two parts:

  • An API class encapsulating the detailed calls to the CL_CRM_DOCUMENTS** class
  • Some example code calling the API class

For clarity, we will start with some examples calling the API class:

DATA: lo_attachment TYPE REF TO zcl_attachment,
      lt_attachments TYPE TABLE OF REF TO zcl_attachment,
      lv_xml TYPE string.

 CALL METHOD zcl_gos_attachment=>get_attachments_for_bo(
   EXPORTING
     object_key  = '0017A45189CA1EE183DA3CA3941D4600'
     object_type = 'BUS2000131' “ Sales Order Line
     category_id = 'BO'
   RECEIVING
     attachments = lt_attachments ).

 CHECK lt_attachments IS NOT INITIAL.

 READ TABLE lt_attachments INTO lo_attachment INDEX 1.

 lv_xml = lo_attachment->get_ascii_content( ).

This example reads an XML document that has been stored on a Sales Order Line. The result is a table of attachments instances that can be accessed.

DATA: lo_attachment TYPE REF TO zcl_attachment,
      lt_attachments TYPE TABLE OF REF TO zcl_attachment,
      lv_binary_string TYPE xstring.

“ Fill with binary data
lv_binary_string = ‘’.

CALL METHOD zcl_gos_attachment=>upload_binary_file
  EXPORTING
    object_key    = '0017A45189CA1EE183DA3CA3941D4600'
    object_type   = 'BUS2000131'
    category_id   = 'BO'
    binary_string = lv_binary_string
    filename      = 'Example document.pdf'
  RECEIVING
    attachment    = lo_attachment.

This example stores some binary data as an attachment to a Sales Order Line.

As you can see, calling the API class is straightforward as it encapsulates most of the boiler plate code needed to call CL_CRM_DOCUMENTS. As an example, one method of the example class is shown here, the rest of the code is available in a zip file attached to this article.

METHOD get_ascii_content.

  FIELD-SYMBOLS: <line> TYPE sdokcntasc.

  DATA:
    lv_logical_key TYPE skwf_io,
    lt_content TYPE sdokcntascs,
    ls_error TYPE skwf_error,
    lv_dummy_message TYPE string.

* Construct logical key
  lv_logical_key-objtype = me->key-objtypelo.
  lv_logical_key-class = me->key-classlo.
  lv_logical_key-objid = me->key-objidlo.

* Get text lines
  CALL METHOD cl_crm_documents=>get_with_table
    EXPORTING
      loio               = lv_logical_key
    IMPORTING
      error              = ls_error
      file_content_ascii = lt_content.

  IF ls_error IS NOT INITIAL.
    MESSAGE ID ls_error-id TYPE ls_error-type NUMBER ls_error-no
      WITH ls_error-v1 ls_error-v2 ls_error-v3 ls_error-v4
      INTO lv_dummy_message.
    RAISE EXCEPTION TYPE zcx_gos_attachment_exception.
  ENDIF.

* Convert the separate text lines to a single string
  LOOP AT lt_content ASSIGNING <line>.
    CONCATENATE content <line> INTO content SEPARATED BY cl_abap_char_utilities=>cr_lf.
  ENDLOOP.

ENDMETHOD.

This method reads the text contents of a GOS attachment.

Full API class


You can download the full ABAP code here.

*: Most of this functionality is available in the SAP GUI, I’m not sure whether all GOS related functionality is available in the Web UI.

**: ECC uses a set of function modules instead of the CL_CRM_DOCUMENTS class. Check Your Friendly ABAPer (http://friendlyabaper.blogspot.nl/2008/07/oh-my-gos.html) for an excellent explanation

No comments:

Post a Comment