When you open standard transaction you may be surprised sometimes that although you're able to attach standard attachments while creating document, you're not able to attach business document at this time. It's because business documents needs to have specified an object to which it is linked and in case of GOS for standard transactions it's always called without object id (in creation mode). When you develop own transaction and you use GOS for standard or Z object then there is a possibility to omit that restriction, as during creation of GOS manager you can assign temporary object name to it. At save (or after save) you can move attachments and business documents from temporary object to final one. 
 
The question is what will happen when you'll attach business document to temporary object but you will not save the document at the end? The document itself will be kept so you have to be sure that it will be deleted next archiving run. 
 
I will show you an example on Purchase Requisition object which is used in Z-tcode.
In the main program we have define global parameters:

datag_gos_ident     type borident.
datag_banfn         type eban-banfn.
datago_gos_manager  type ref to cl_gos_manager.

And we activate GOS with temporary object name if we're in creation mode:
 

clear g_gos_ident.
  if g_creation_mode is initial.
    g_gos_ident-objkey   g_banfn.
    clear p_no_commit.
  else.
"temporary object key which is unique
    concatenate 'ZBANF' sy-uname sy-datum sy-uzeit  into g_gos_ident-objkey.
  endif.
  g_gos_ident-objtype  'BUS2105'. "PR BUS object

  create object go_gos_manager
  
exporting
"    io_container         = g_cc_att
    is_object            g_gos_ident
    it_service_selection = 
*    IO_CALLBACK         = 
    ip_start_direct      ' '
"    IP_NO_INSTANCE       = f_ip_no_instance
    ip_no_commit         'X' "commit done manually
  exceptions
    object_invalid       1
    callback_invalid     2
    others               3.

So now each attachment and business document will be linked to temporary object. Now after successful save of new PR we need to move the link from temporary object to final one.

Let's start with attachments, I will move them using cl_gos_service_tools=>move_linked_objects.

Importing:

I_TEMP_OBJECT TYPE BORIDENT-OBJKEY -> temporary key for GOS

I_PURCHASE_REQUISITION TYPE EBAN-BANFN -> taget Purchase requistion

Implementation:

method gos_move_att_tpr_to_pr.
  datamt_services type tgos_sels.    " Services table typ
  datams_service  type sgos_sels.    " Services structure type
  datams_source   type sibflporb.    " Source
  datams_target   type sibflporb.    " Target

* Source
  ms_source-instid =  i_temp_object.
  ms_source-typeid 'BUS2105'.
  ms_source-catid  'BO'.
*
* Target
  ms_target-instid i_purchase_requisition.
  ms_target-typeid 'BUS2105'.
  ms_target-catid  'BO'.

  cl_gos_service_tools=>move_linked_objects(
                                              is_source            ms_source
                                              is_target            
ms_target
                                              it_service_selection 
mt_services
                                              
).

endmethod.

 

Now business documents, here I will use cl_alink_connection=>find, cl_alink_connection=>insert and cl_alink_connection=>delete.

Importing:

I_PURCHASE_REQUISITION TYPE EBAN-BANFN -> Purchase Requisition Number
I_TEMP_OBJECT TYPE TOA02-OBJECT_ID -> SAP ArchiveLink: Object ID (object identifier)

Implementation:

method gos_alink_move_link_tpr_to_pr.
  datamt_connections type standard table of toav0.
  datams_toavo type toav0.
  field-symbols<toavo> type toav0.

  check i_purchase_requisition is not initial and i_temp_object is not initial.

  cl_alink_connection=>find(
    exporting
      sap_object         'BUS2105'
      object_id          i_temp_object
      mandt              
sy-mandt
*    archiv_id          = archiv_id
*    arc_doc_id         = arc_doc_id
*    ar_object          = ar_object
*    from_ar_date       = from_ar_date
*    until_ar_date      = SY-DATUM
*    doc_type           = doc_type
*    del_date           = del_date
*    limited            = limited
       no_auth_check      'X'
*    limit              = limit
*    parameter          = parameter
    importing
*    count              = count
*    reducedbylimit     = reducedbylimit
*    reducedbyauthority = reducedbyauthority
      connections        mt_connections
    
exceptions
      not_found          1
      error_authorithy   2
      error_parameter    3
      others             4
         ).
  if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  endif.

  loop at mt_connections assigning <toavo>.
    clear ms_toavo.
    move-corresponding <toavo> to ms_toavo.
    ms_toavo-object_id i_purchase_requisition.

    cl_alink_connection=>insert(
      exporting
        link     ms_toavo
*      barcode  = barcode
*    receiving
*      tab_name =
      exceptions
        error    1
        others   2
           ).
    if sy-subrc eq 0.

      cl_alink_connection=>delete(
        exporting
          link      <toavo>
          tab_name  
'TOA02'
*  importing
*    tab_names = tab_names
        exceptions
          not_found 1
          others    2
             ).
      if sy-subrc <> 0.
        "raise error_moving_attachments.
      endif.
    else.
      "raise error_moving_attachments.
    endif.


  endloop.

endmethod.

This is all fine when at the end you move links from temporary object to final one, but in case you didn't then in your program you should run a method or function to clear not needed entries. In the method bellow you'll see that I look for particular or all dummy links older than 1 day and then I set deletion date for the file to tomorrow with cl_alink_connection=>update. So next archiving run not needed files will be deleted.

 
Importing:

I_TEMP_OBJECT TYPE TOA02-OBJECT_ID OPTIONAL -> SAP ArchiveLink: Object ID (object identifier)

Implementation:

method gos_alink_temp_date_change.
  datamt_toa02 type standard table of toa02.
  datamr_object_id type range of toa02-object_id.
  datams_object_id like line of mr_object_id.
  datams_taov0 type toav0.
  datamr_ar_date type range of toa02-ar_date.
  datams_ar_date like line of mr_ar_date.
  datamr_del_date type range of toa02-del_date.
  datams_del_date like line of mr_del_date.
  field-symbols<toa02> type toa02.


  if i_temp_pr_object is not initial.
    ms_object_id-sign 'I'.
    ms_object_id-option 'EQ'.
    ms_object_id-low i_temp_object.
    append ms_object_id to mr_object_id.
  else"change the date to tommorow for all  temporary files older than 1 day.
    ms_object_id-sign 'I'.
    ms_object_id-option 'CP'.
    ms_object_id-low 'ZBANF*'.
    append ms_object_id to mr_object_id.
    ms_ar_date-sign 'I'.
    ms_ar_date-option 'LT'.
    ms_ar_date-low    sy-datum 1.
    append ms_ar_date to mr_ar_date.
    ms_del_date-sign 'I'.
    ms_del_date-option 'GT'.
    ms_del_date-low    sy-datum + 1.
    append ms_del_date to mr_del_date.
  endif.

  select into corresponding fields of table mt_toa02
    
from toa02
    
where sap_object eq 'BUS2105'
      and object_id  in  mr_object_id
      
and ar_date    in  mr_ar_date
      
and del_date   in mr_del_date.

  loop at mt_toa02 assigning <toa02>.
    clear ms_taov0.
    move-corresponding <toa02> to ms_taov0.
    ms_taov0-del_date sy-datum + 1.

    cl_alink_connection=>update(
      exporting
        link     ms_taov0
      
exceptions
        error    1
        others   2
           ).
    if sy-subrc <> 0.
*     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    endif.
  endloop.
endmethod.

 

Job done :)


Eclipse Plugins for ABAP

ABAP Favorites

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

ABAP ADT Extensions

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

ABAP Quick Fixes

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client