Saturday, May 2, 2015

TIP: ABAP code to add an IDoc to your application object

Sometimes you need to write ABAP code to generate an IDoc. With this new custom IDoc generation you would like to have it link to your SAP application object (E.g. Sales Order, Delivery, …) You would like to see your IDoc linked to the Generic Object Services (GOS) display relationships service. Well here is the code needed to do just that…


TYPE-POOLS BD11.
DATA: it_bor TYPE BD11_OBJECT_TAB,
      wa_bor TYPE BD11_OBJECT .
CONSTANTS: c_object_type TYPE SWO_OBJTYP VALUE ‘BUS2032′. “Applicable application object type
CLEAR: wa_bor, it_bor[].
wa_bor-OBJECT_TYPE = c_object_type.
wa_bor-OBJECT_KEY = wa_vbak-vbeln. “Applicable object key for the application object.
wa_bor-log_system = wa_edidc-sndprn.
APPEND wa_bor TO it_bor.
CALL FUNCTION ‘BOR_OBJECTS_LINK_TO_IDOC’
  EXPORTING
    idoc_number = wa_edidc-docnum
    direction = wa_edidc-direct
    t_bor_object = it_bor
  EXCEPTIONS
    many_workitems_found = 1
    parameter_error = 2
    own_logical_system_not_defined = 3
    OTHERS = 4.
IF SY-SUBRC = 0.
  COMMIT WORK.
ENDIF.