Thursday, 5 July 2012

To Display the checkboxes in the ALV report output and to capture the user checkbox values

  *ALV data declarations
data: fieldcatalog type slis_t_fieldcat_alv with header line,
      gd_layout    type slis_layout_alv.

constants: c_x type char1 value 'X'.

  typesbegin of ty_tdgt,
        module_name type ztdgt_table-module_name,
        program_name type ztdgt_table-program_name,
        process_name type ztdgt_table-process_name,
        flag  type c, " TO store the flag values given in o/p checkboxes
       end of ty_tdgt.
  data: it_tdgt2 type standard table of ty_tdgt,
      wa_tdgt2 type ty_tdgt.


perform data_retrieval. "get the necessary data into our internal table
 perform build_fieldcatalog.
 perform display_alv_report.

*&      Form  build_fieldcatalog
*       text
form build_fieldcatalog.
  refresh fieldcatalog.
  clear fieldcatalog.
  fieldcatalog-fieldname   = 'FLAG'.
  fieldcatalog-seltext_m   = 'Check'.
  fieldcatalog-input     = 'X'.
  fieldcatalog-edit     = 'X'.
  fieldcatalog-checkbox = 'X'.
  fieldcatalog-col_pos     = 1.
  append fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'MODULE_NAME'.
  fieldcatalog-seltext_m   = 'Module Name'.
  fieldcatalog-input     = 'X'.
  fieldcatalog-col_pos     = 2.
  append fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'PROCESS_NAME'.
  fieldcatalog-seltext_m   = 'Process Name'.
  fieldcatalog-col_pos     = 3.
  fieldcatalog-outputlen   = '55'.
  append fieldcatalog.
  clear  fieldcatalog.

endform.                    " BUILD_FIELDCATALOG
*       Display report using ALV grid
form display_alv_report .
  gd_repid = sy-repid.

  call function 'REUSE_ALV_GRID_DISPLAY'
            i_callback_program        = gd_repid
*            i_callback_top_of_page   = 'TOP-OF-PAGE'
            i_callback_pf_status_set  = 'SET_PF_STATUS'
            i_callback_user_command   = 'USER_COMMAND'
*            i_grid_title             = 'My Title'
            is_layout                 = gd_layout
            it_fieldcat               = fieldcatalog[]
            t_outtab                  = it_tdgt2[] "Internal table with data
            program_error             = 1
            others                    = 2.

  if sy-subrc <> 0.
    write:/ sy-subrc.

endform.                    " DISPLAY_ALV_REPORT
*PF status
*                      FORM SET_PF_STATUS                              *
FORM set_pf_status USING rt_extab   TYPE  slis_t_extab.
ENDFORM.                    "set_pf_status
*User command

  FORM user_command  USING r_ucomm LIKE sy-ucomm
                         rs_selfield TYPE slis_selfield.

 PERFORM get_userinputs USING r_ucomm       "To capture the user inputs


*&      Form  GET_USERINPUTS
*       text
*  -->  p1        text
*  <--  p2        text
* When we click on the save button the data will save in the same internal tale
* in the flag field

* To capture the output values the SY-UCOMM = '&DATA_SAVE' , than only it will *save the date
form GET_USERINPUTS USING r_ucomm TYPE sy-ucomm
                          rs_selfield TYPE slis_selfield..

* when you check the internal table after clicking in the save button the *checkbox values were reflected in the table.

  CASE r_ucomm.
    WHEN l_execute . " &DATA_SAVE

      REFRESH: it_prgm_details.

      LOOP AT it_tdgt2 INTO wa_tdgt2 WHERE flag = c_x.

        MOVE-CORRESPONDING wa_tdgt2 TO wa_prgm_details.     "#EC ENHOK

        APPEND wa_prgm_details TO it_prgm_details.
        CLEAR: wa_prgm_details,wa_tdgt2.

endform.                    " GET_USERINPUTS

To supress the execute button in the report selection screen

* If you put the below code in at selection-screen it will supress the execute button

at selection-screen output.

  data gt_ucomm type table of sy-ucomm.

  append'ONLI' to gt_ucomm.

  call function 'RS_SET_SELSCREEN_STATUS'


            p_status  = '%_00'  "Status GUI for screen 1000


            p_exclude = gt_ucomm.

To create the dynamic internal table in programs

To create the dynamic internal table we have to create one Ztable with all the field names and the sequence number and required information.

  typesbegin of ty_tdgt_prgm,
         program_name type ztdgt_prgm-program_name,
         field_name   type ztdgt_prgm-field_name,
         sequence_number type ztdgt_prgm-sequence_number,
         table_name type ztdgt_prgm-table_name,
         domain_name type ztdgt_prgm-domain_name,
         description type ztdgt_prgm-description,
       end of ty_tdgt_prgm.

  it_tdgt_prgm type standard table of ty_tdgt_prgm,
      wa_tdgt_prgm type ty_tdgt_prgm.

  select program_name
    from ztdgt_prgm
    into table it_tdgt_prgm
    for all entries in it_prgm_details
    where program_name = it_prgm_details-program_name.

      sort it_tdgt_prgm by sequence_number.

  data : it_fldcat type lvc_t_fcat,
       wa_fldcat like line of it_fldcat.

*We have to loop the table to create the structure.
*for every one loop it will add one field and the structure will remains in wa_qual_tab-table.
* we can use this structure by wa_qual_tab-table->* 

   loop at it_tdgt_prgm into wa_tdgt_prgm where program_name = wa_prgm_details-program_name.

      wa_fldcat-fieldname = wa_tdgt_prgm-field_name.
      wa_fldcat-tabname = wa_tdgt_prgm-table_name.

           append wa_fldcat to it_fldcat.

      call method cl_alv_table_create=>create_dynamic_table
          it_fieldcatalog = it_fldcat
          ep_table        = wa_qual_tab-table.


*to use the dynamic structure created we have to do below.
   assign wa_qual_tab-table->* to <it_final>.
    import <it_final> from memory id 'memo1'.

CSV file in application server(internal table data into CSV)

*Declerations for the CSV file.
  * Declerations for CSV
* "CSV file path in application layer.
constants: c_file1 type rcgfiletr-ftappl value 'D:\Test.csv'. * Module name
  data: l_line type string.
  constants c_blank value ''.
  data: l_modulename(31). " MM - OS - 1.1
  constants: c_modulename(10type c value 'Module:'.
* process name
  data: l_processname(100).
  constants: c_processname(15type c value 'Process Name:'.
* step name  eg: Client
  data: c_stepend(75type c value 'End Of The Process',
        g_header(5000type c.

* Declarations for the CSV conversion*******

constants: c_sep type char1 value ','.

databegin of wa_csvdata,
 end of wa_csvdata.
data: gt_csvdata like table of wa_csvdata.

data: l_data type string.

field-symbols: <wa_field> type any,
                <ls_csv> type any.

* set to X if the no data has been put into the csv workarea
data: flag_firstcol type xflag.

*Declerations to display the status messages.
data : g_status type string.
constants: g_text(16type c value 'Start of Process' .

*Open the CSV file.
* Before processing the data we have to open the CSV file.

  open dataset c_file1 for output in text mode encoding default" To open the CSV file

*Process with the internal table data.

*here <it_final> is the field symbol(or internal table) which contains my data.
  import <it_final> from memory id 'memo1'.

   free memory id 'memo1'.

* TO append the data into CSV file
*To append the header to the csv document
* Module name
  transfer c_blank to c_file1. " To transfer one empty line
  concatenate c_modulename  p_wa_prgm_details-module_name  into l_line separated by c_comma. " To put some header
  transfer l_line to c_file1.

* Process name
  transfer c_blank to c_file1.
  clear l_line.
  concatenate c_processname  p_wa_prgm_details-process_name into l_line separated by c_comma.
  transfer l_line to c_file1.
  transfer c_blank to c_file1.

perform download_csv .

* To append the End of the Process
transfer c_blank to c_file1.
clear l_line.
move c_stepend to l_line.
*CONCATENATE C_stepend p_wa_prgm_details-process_name into l_line SEPARATED BY c_comma.
transfer l_line to c_file1.
transfer c_blank to c_file1.

*Download the CSV file.
*perform download_csv .
********************************************************  * here g_geader is the header of the file(Fields)
* we have to concatinate each field name into one string separatied by ','.
* concatenate g_header wa_tdgt_prgm-description into g_header separated by c_comma. " To get the headers 


form download_csv .
*Close the file.
* Before processing the data we have to open the CSV file.
  close dataset c_file1. " To close the CSV file

To view and download the created CSV file go to transaction cg3y and enter the csv file name and the target file name of our system than it will download.

 condense g_header.
  move g_header to wa_csvdata.
  append wa_csvdata to gt_csvdata.
  clear wa_csvdata.
  loop at <it_final> assigning <ls_csv>.
    flag_firstcol = 'X'.
      assign component sy-index of structure <ls_csv> to <wa_field>.
      if sy-subrc <> 0.
        l_data = <wa_field>.

        if flag_firstcol = 'X'.
          wa_csvdata = l_data.
          flag_firstcol = ''.
          concatenate wa_csvdata c_sep l_data into wa_csvdata.

      append wa_csvdata to gt_csvdata.
      clear wa_csvdata.

    loop at gt_csvdata into wa_csvdata.
      transfer wa_csvdata to c_file1.
      clear wa_csvdata.
   refresh gt_csvdata.