Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
ramakrishnappa
Active Contributor

Continues......

Pre-requisite: ( Please go through the below link before you continue )

Attach files with save, retrieve and delete functionality in Web Dynpro ABAP - Part 1

Step 7:

Create a table ui element TBL_LIST and bind it to the context node ATTACHMENT_LIST as below

Note: choose editor for FILE_NAME attribute as FILE DOWNLOAD (ui element )

Step 8:

Go the column editor TBL_LIST_FILE_NAME_EDITOR and bind the properties as show below

Step 9:

Create a tool bar to the table TBL_LIST and add a button BTN_SAVE and attach an action as show below

Add the below code (Save functionality) in event handler method ONACTIONSAVE

ONACTIONSAVE

METHOD onactionsave .
  DATA lo_nd_attachment_list  TYPE REF TO if_wd_context_node.
  DATA lt_attachment_list     TYPE wd_this->elements_attachment_list.
  DATA lo_msg_manager         TYPE REF TO if_wd_message_manager.

  FIELD-SYMBOLS <fs_attachment_list> LIKE LINE OF lt_attachment_list.


  lo_nd_attachment_list =
  wd_context->get_child_node( name = wd_this->wdctx_attachment_list ).

  lo_nd_attachment_list->get_static_attributes_table(
      IMPORTING table = lt_attachment_list ).


  DATA lt_data     TYPE TABLE OF ytr_attach_files.
  DATA ls_data     LIKE LINE OF lt_data.

  "==========================================================
  "Loop over attachment list for only new attachments
  "==========================================================

  LOOP AT lt_attachment_list ASSIGNING <fs_attachment_list>
    WHERE status = abap_false." new attachment

    " set status
    <fs_attachment_list>-status = abap_true.

    CLEAR ls_data.
    MOVE-CORRESPONDING <fs_attachment_list> TO ls_data.
    APPEND ls_data TO lt_data.

  ENDLOOP.

  "get message manager
  lo_msg_manager =
  wd_comp_controller->wd_get_api( )->get_message_manager( ).

  IF lt_data[] IS INITIAL.
    " Attachement list is already saved
    lo_msg_manager->report_error_message(
        message_text = 'Attachement list is already saved' ).

    RETURN.
  ENDIF.

  "==========================================================
  " Save attachments
  "==========================================================
  MODIFY ytr_attach_files FROM TABLE lt_data.
  IF sy-subrc IS INITIAL.
    " after successful save, update the status
    lo_nd_attachment_list->bind_table(
      EXPORTING
        new_items            =    lt_attachment_list
    ).

  " Show success message
  lo_msg_manager->report_success(
        message_text = 'Attachement list saved successfully !!!' ).
  ENDIF.


ENDMETHOD.

Step 10:

Create another button BTN_DELETE (to delete files ) and attach the action DELETE_ATTACHMENT as shown below

Add the below code in (Delete functionality) in event handler method ONACTIONDELETE_ATTACHMENT

ONACTIONDELETE_ATTACHMENT

METHOD onactiondelete_attachment .
  DATA lo_node        TYPE REF TO if_wd_context_node.
  DATA lt_elements    TYPE wdr_context_element_set.
  DATA lo_element     TYPE REF TO if_wd_context_element.
  DATA ls_attach_list TYPE wd_this->element_attachment_list.
  DATA lo_msg_manager TYPE REF TO if_wd_message_manager.

  "==========================================================
  " Get the selected elements from the context
  "==========================================================
  lo_node =
  wd_context->get_child_node( name = wd_this->wdctx_attachment_list ).

  lt_elements =
  lo_node->get_selected_elements( ).

  IF lt_elements[] IS INITIAL.
    "report error message, Please choose a file to delete
    lo_msg_manager =
    wd_comp_controller->wd_get_api( )->get_message_manager( ).

    lo_msg_manager->report_error_message(
        message_text              = 'Please select atleast 1 row' ).

    RETURN.
  ENDIF.

  DATA lt_data TYPE TABLE OF ytr_attach_files.
  DATA ls_data LIKE LINE OF lt_data.

  CLEAR lt_data.

  "==========================================================
  " Delete the record from attachment list
  "==========================================================
  LOOP AT lt_elements INTO lo_element.
    lo_element->get_static_attributes(
      IMPORTING
        static_attributes = ls_attach_list
    ).
    " delete from the context node
    lo_node->remove_element( element = lo_element  ).

    "Check if record exists in table
    IF ls_attach_list-status = abap_true.
      CLEAR ls_data.
      MOVE-CORRESPONDING ls_attach_list TO ls_data.

      APPEND ls_data TO lt_data.

    ENDIF.
  ENDLOOP.

  "==========================================================
  " Delete the record(s) from data base table
  "==========================================================
  DELETE ytr_attach_files FROM TABLE lt_data.
ENDMETHOD.

Step 11:

Go to method WDDOINIT(  ) of view and add the below code ( Retrieve functionality )

WDDOINIT

METHOD wddoinit .

  DATA lo_nd_attachment_list  TYPE REF TO if_wd_context_node.
  DATA lt_attachment_list     TYPE wd_this->elements_attachment_list.
  DATA ls_attachment_list     LIKE LINE OF lt_attachment_list.
  DATA lt_data                TYPE TABLE OF ytr_attach_files.

  FIELD-SYMBOLS: <ls_data> LIKE LINE OF lt_data.
  "==========================================================
  " get the attachment files from table for the current user
  "==========================================================
  SELECT *
    INTO TABLE lt_data
    FROM ytr_attach_files
    WHERE uname = sy-uname.


  CLEAR lt_attachment_list.
  LOOP AT lt_data ASSIGNING <ls_data>.
    CLEAR ls_attachment_list.
    MOVE-CORRESPONDING <ls_data> TO ls_attachment_list.
    APPEND ls_attachment_list TO lt_attachment_list.
  ENDLOOP.

  "==========================================================
  " Bind data to context node
  "==========================================================
* navigate from <CONTEXT> to <ATTACHMENT_LIST> via lead selection
  lo_nd_attachment_list =
  wd_context->get_child_node( name = wd_this->wdctx_attachment_list ).

  lo_nd_attachment_list->bind_table( new_items = lt_attachment_list
set_initial_elements = abap_true ).

ENDMETHOD.

  

Save the component and active it

Step 12:

Create WDA application as below

Now, we are ready to see some output and the save, retrieve & delete functionality while attaching files

Output:

Click on BROWSE button as shown below

The choose file to upload wizard appears & choose a file and click on OPEN button as below

Click on attach button as shown below

Now, file is attached into table but it is not yet saved into database table ( see the highlighted cell )

Click on Save attachments button as show below (Now, the attachment is saved into data base .. see the highlighted cell )

The data is stored into data base table YTR_ATTACH_FILES as below

Again, browse another file and attach ( see the second row – for newly added file )

To delete an attachment, we need to select a row & click on “Delete attachment” button as show below

Note: on click delete attachment button, it deletes the attachment from data base as well ( if file is already stored )

Error messages:

If user clicks on save attachment button and no fresh attachment to save, then user will be notified with the below error message

If user clicks on Delete attachment button and no row has been selected (chosen), then user will be notified with below error message

---------------------------------------------------

Hope this document helps for those who are looking for uploading files and storing in table and retrieving :smile:

I appreciate if any comments/feedback from you :smile: :smile: :smile:

49 Comments
Labels in this area