Technical Articles
Delete GOS Attachment from an Order (IW33) – ABAP Code
Prerequisite:
You have an order number which has got attachment(s).
Overview:
I was trying hard to delete this attachment problematically and would like to share the code.
Steps:
- Read document links
DATA: ls_lpor TYPE sibflporb, ls_relst TYPE obl_s_relt, lt_relst TYPE obl_t_relt, lt_links TYPE obl_t_link, lo_root TYPE REF TO cx_root. ls_lpor-instid = lv_aufnr. ls_lpor-typeid = 'BUS2007'. ls_lpor-catid = 'BO'. ls_relst-sign = 'I'. ls_relst-option = 'EQ'. ls_relst-low = 'ATTA'. APPEND ls_relst TO lt_relst. * Read document links CALL METHOD cl_binary_relation=>read_links EXPORTING is_object = ls_lpor it_relation_options = lt_relst IMPORTING et_links = lt_links.
- Find the document which you want to delete from internal table LT_LINKS
LOOP AT lt_links ASSIGNING FIELD-SYMBOL(<fs_links>) WHERE instid_b CS lv_objno. "<--this is the document ID which we want to delete DATA(ls_object_a) = VALUE borident( objkey = <fs_links>-instid_a objtype = <fs_links>-typeid_a ). DATA(ls_object_b) = VALUE borident( objkey = <fs_links>-instid_b objtype = <fs_links>-typeid_b ). EXIT. ENDLOOP.
- Delete binary relation
IF <fs_links> IS ASSIGNED. CALL FUNCTION 'BINARY_RELATION_DELETE' EXPORTING obj_rolea = ls_object_a obj_roleb = ls_object_b relationtype = 'ATTA' * FIRE_EVENTS = 'X' EXCEPTIONS entry_not_existing = 1 internal_error = 2 no_relation = 3 no_role = 4 OTHERS = 5. IF sy-subrc <> 0. * Implement suitable error handling here ENDIF.
- Delete SO Object
DATA: ls_folder_id TYPE soodk, ls_object_id TYPE soodk. ls_folder_id-objtp = <fs_links>-instid_b+0(3). ls_folder_id-objyr = <fs_links>-instid_b+3(2). ls_folder_id-objno = <fs_links>-instid_b+5(12). ls_object_id-objtp = <fs_links>-instid_b+17(3). ls_object_id-objyr = <fs_links>-instid_b+20(2). ls_object_id-objno = <fs_links>-instid_b+22(12). CALL FUNCTION 'SO_OBJECT_DELETE' EXPORTING folder_id = ls_folder_id object_id = ls_object_id EXCEPTIONS communication_failure = 1 folder_not_empty = 2 folder_not_exist = 3 folder_no_authorization = 4 forwarder_not_exist = 5 object_not_exist = 6 object_no_authorization = 7 operation_no_authorization = 8 owner_not_exist = 9 substitute_not_active = 10 substitute_not_defined = 11 system_failure = 12 x_error = 13 OTHERS = 14. IF sy-subrc = 0. COMMIT WORK AND WAIT. ENDIF. ENDIF.
There is no proper error handling and code modularization, so please do it yourself.
Conclusion:
If you follow these above mentioned steps, you will be able to selectively delete the attachments from and order.
Thank you, a blog post about that topic (delete attachment) was missing.
Note that this code is also provided as a class with error handling by Kerem Koseoglu at https://github.com/keremkoseoglu/ABAP-Library/blob/master/zcl_bc_gos_toolkit.abap.
Something that would be nice is to generalize the blog post to any kind of business object, not only IW33/BUS2007. NB: unfortunately, I could never find a blog post about how to determine the business object type (BUS2007) based on the transaction used (IW33).
Well,
This blog post would be quite short:
Excellent!
P.S.
There is also the good old class by Philip Johnston.