Wednesday, 3 January 2018

R/3 Take a step back into time – ALV Interactive Cut / Paste

Requirement:


Upload data from a spreadsheet into sales order conditions and a custom table.

The solution:

Use copy / paste from a clipboard into a program.   Create an ALV    The ALV will allow for some of the fields to be changed.


So here is the selection screen:

SAP ABAP Tutorials and Materials, SAP ABAP Guides, SAP ABAP Certifications, SAP ABAP Live

Before they execute they must have the excel copied in the Clipboard:

Here’s the result

SAP ABAP Tutorials and Materials, SAP ABAP Guides, SAP ABAP Certifications, SAP ABAP Live

The program validates the data and only allows updates to the valid data – Load can be updated.  Because I had trouble just filling in the load.   I allowed a double click and a box to change it.

SAP ABAP Tutorials and Materials, SAP ABAP Guides, SAP ABAP Certifications, SAP ABAP Live

Then save from the ALV will save the information.

It’s a simple application.   The the end user loves because of the ease of use.

So here is some of the code:

Get the clipboard information:

   CALL METHOD cl_gui_frontend_services=>clipboard_import(
      IMPORTING
         data                 = gt_clipdata
         length               = gv_clip_len
       EXCEPTIONS
         cntl_error           = 1
         error_no_gui         = 2
         OTHERS               = 4 ).
   IF sy-subrc NE 0.
     MESSAGE i000(z3)
       WITH 'Error while importing data from clipboard'.
     EXIT.
   ENDIF.

Update table:

   LOOP AT gt_clipdata INTO gs_clipdata.
     zlog-cnt = zlog-cnt + 1.
     CLEAR zlog-data.
     zlog-data = gs_clipdata.
     zlog-leng = 500.
     INSERT zcs1_log FROM zlog.
     SPLIT gs_clipdata AT gc_hex_tab
       INTO TABLE gt_record.
     APPEND INITIAL LINE TO gt_data.
     READ TABLE gt_data ASSIGNING <fs_data> INDEX sy-tabix.

     LOOP AT gt_record INTO gs_record.
       ASSIGN COMPONENT sy-tabix
         OF STRUCTURE <fs_data> TO <fs_field>.
       IF sy-subrc EQ 0.
         <fs_field> = gs_record-zdata.
       ENDIF.
     ENDLOOP.
   ENDLOOP.
   COMMIT WORK.

Of course, some more processing validate the data, put into an output table.  Finally display the ALV:

   IF g_custom_container IS INITIAL.
     CREATE OBJECT g_custom_container
            EXPORTING container_name = g_container.

     CREATE OBJECT g_grid
            EXPORTING i_parent = g_custom_container.

     CREATE OBJECT event_rec.
     SET HANDLER event_rec->handle_double_click FOR g_grid.
     SET HANDLER event_rec->handle_top_of_page FOR g_grid.
     gs_layout-info_fname = 'ROWCOLOR'.

     gs_layout-sel_mode = 'A'.
     gs_variant-report = sy-repid.
     gs_variant-username = sy-uname.
     PERFORM build_fieldcat.
     CALL METHOD g_grid->set_table_for_first_display
          EXPORTING i_structure_name = 'ZSTR_TRUCK_UPLOAD'
                    is_layout        = gs_layout
                    is_print          = gs_print
                    is_variant       = gs_variant
                    i_save           = 'A'
          CHANGING  it_outtab        = gt_out
                    it_fieldcatalog  = gt_field_cat.

*  Register the EDIT Event
     CALL METHOD g_grid->register_edit_event
       EXPORTING
         i_event_id = cl_gui_alv_grid=>mc_evt_modified
       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.

* Sets up a handler for any data changed to detail
     CREATE OBJECT g_event_receiver.
     SET HANDLER g_event_receiver->handle_data_changed FOR g_grid.
   ENDIF.

And then the update

   METHOD handle_data_changed.
     DATA: ls_good TYPE lvc_s_modi,
           ls_diff TYPE lvc_s_modi.
     LOOP AT er_data_changed->mt_mod_cells INTO ls_good.
       CASE ls_good-fieldname.
         WHEN 'TOTAL' OR 'LOAD'.
           CALL METHOD update_change
               EXPORTING
                 ps_total = ls_good
                 pr_data_change = er_data_changed.

       ENDCASE.
     ENDLOOP.
   ENDMETHOD.
*---------------------------------------------------------------------*
* Updates the ALV difference field in the master data
*---------------------------------------------------------------------*
   METHOD update_change.
     DATA: lv_vbeln(10),
           lv_posnr(10).
*     DATA: lv_total TYPE zeco_master-total.

     READ TABLE gt_out INTO gs_out INDEX ps_total-row_id.
     IF p_disp <> 'X'.
       CALL METHOD pr_data_change->modify_cell
               EXPORTING i_row_id    = ps_total-row_id
                         i_fieldname = 'PRICE'
                         i_value     = gs_out-price.

       CALL METHOD pr_data_change->modify_cell
         EXPORTING i_row_id    = ps_total-row_id
                   i_fieldname = 'BILL_CUST'
                   i_value     = gs_out-bill_cust.
     ELSE.
       CALL METHOD pr_data_change->modify_cell
               EXPORTING i_row_id    = ps_total-row_id
                         i_fieldname = 'LOAD'
                         i_value     = ps_total-value.

       SPLIT ps_total-value  AT '-' INTO lv_vbeln lv_posnr.
       PERFORM alpha_conver USING lv_vbeln
                                  gs_out-vbeln.

       PERFORM alpha_conver USING lv_posnr
                                  gs_out-posnr.

       CALL METHOD pr_data_change->modify_cell
               EXPORTING i_row_id    = ps_total-row_id
                         i_fieldname = 'VBELN'
                         i_value     = gs_out-vbeln.

       CALL METHOD pr_data_change->modify_cell
              EXPORTING i_row_id    = ps_total-row_id
                        i_fieldname = 'POSNR'
                        i_value     = gs_out-posnr.
     ENDIF.
   ENDMETHO

No comments:

Post a Comment