Tuesday, September 15, 2009

Generic E-Mail Program Part 3

If you go back to my post Generic E-Mail Program you will see several screen shots where I am giving the users the ability to resend a e-mail or fax on demand.  They can also check the start of a fax and override who to send a fax/e-mail to.
This program is basicly a collection of ALV Grids.
Well lets get down to the code
Global Data Section
report  zgeneric_email_review.
type-pools: slis.
types:
  begin of type_zemail_001,
    kunnr            type zemail_001-kunnr,
    source_program   type zemail_001-source_program,
    email_date       type zemail_001-email_date,
    email_time       type zemail_001-email_time,
    type_attachment  type zemail_001-type_attachment,
    attachment       type zemail_001-attachment,
    subject          type zemail_001-subject,
    sent             type zemail_001-sent,
    created_user     type zemail_001-created_user,
    status           type zemail_001-status,
    stored_offline   type zemail_001-stored_offline,
    traffic_light(1) type c,
    view(4)          type c,
    details(7)       type c,
    history(7)       type c,
    resend(6)        type c,
  end of type_zemail_001,

  begin of type_zemail_002,
    source_program type zemail_002-source_program,
    email_date     type zemail_002-email_date,
    email_time     type zemail_002-email_time,
    email_address  type zemail_002-email_address,
    sequence       type zemail_002-sequence,
    sent           type zemail_002-sent,
    com_type       type zemail_002-com_type,
    send(4)        type c,
    view(4)        type c,
    update(6)      type c,
  end of type_zemail_002,

  begin of type_lines,
    lines(256) type c,
  end of type_lines.

data: global_zemail_001 type table of type_zemail_001 with header line,
      global_zemail_002 type table of type_zemail_002 with header line,
      global_attachment type table of zemail_006  with header line,
      global_field_cat  type slis_t_fieldcat_alv,
      global_mult(1)    type c,
      global_layout     type slis_layout_alv occurs 0 with header line,
      bdcdata           like bdcdata    occurs 0 with header line,
      global_repid      like sy-repid,
      type_drive(1)     type c,
      wa_field(10)      type c,
      lt_event_exit     type slis_t_event_exit,
      ls_event_exit     type slis_event_exit,
      gc_refresh        type syucomm value '&REFRESH',
      c_x               value 'X',
      it_attach         type standard table of solisti1 initial size 0 with header line.

initialization.
  select-options: s_src   for global_zemail_001-source_program,
                  s_date  for sy-datum,
                  s_kunnr for wa_field,
                  s_sub   for global_zemail_001-subject,
                  s_fail  for global_zemail_001-sent no intervals.

start-of-selection.
  perform generic_process.
  perform build_fieldcat.

end-of-selection.
Subroutine Generic_Process
This routine is simply going to build an internal table of the records indicated by the selection criteria.

form generic_process.
  data local_zemail_002 type table of zemail_002 with header line.
  select kunnr source_program
         email_date email_time
         type_attachment attachment
         subject sent created_user status stored_offline
     from zemail_001 into table global_zemail_001
     where kunnr in s_kunnr and email_date in s_date
       and source_program in s_src and subject in s_sub
       and sent in s_fail.

  select * from zemail_002 into table local_zemail_002
    for all entries in global_zemail_001
    where source_program = global_zemail_001-source_program
      and email_date     = global_zemail_001-email_date
      and email_time     = global_zemail_001-email_time
      and com_type       = 'TEL'.

  loop at global_zemail_001.

    if global_zemail_001-sent <> ' '.
      global_zemail_001-history = 'History'.
    endif.

    if global_zemail_001-sent = '1'.
      global_zemail_001-traffic_light = '3'.
    else.
      if global_zemail_001-sent = ' '  or global_zemail_001-sent = '0'.
        global_zemail_001-traffic_light = '3'.
        loop at local_zemail_002 
              where source_program = global_zemail_001-source_program
                and email_date     = global_zemail_001-email_date
                and email_time     = global_zemail_001-email_time.
          if sy-subrc = 0.
            global_zemail_001-traffic_light = '2'.
          endif.
        endloop.
      else.
        global_zemail_001-traffic_light = '1'.
      endif.
    endif.
    global_zemail_001-resend    = 'Resend'.
    global_zemail_001-view      = 'View'.
    global_zemail_001-details   = 'Details'.
    modify global_zemail_001.
  endloop.


endform.
Build_fieldcat
This routine will build the field catalog and display the following ALV Grid.
form build_fieldcat.
  data: local_title     type lvc_title,
        local_field_cat type slis_fieldcat_alv.
  refresh global_field_cat.

  clear global_field_cat.
  local_field_cat-fieldname          = 'KUNNR'.
  local_field_cat-tabname            = 'Global_zemail_001'.
  local_field_cat-seltext_m          = 'Customer'.
  append local_field_cat to global_field_cat.

  clear local_field_cat.
  local_field_cat-fieldname          = 'SOURCE_PROGRAM'.
  local_field_cat-tabname            = 'Global_zemail_001'.
  local_field_cat-seltext_m          = 'Program'.
  append local_field_cat to global_field_cat.

  clear local_field_cat.
  local_field_cat-fieldname          = 'EMAIL_DATE'.
  local_field_cat-tabname            = 'Global_zemail_001'.
  local_field_cat-seltext_m          = 'Date Generated'.
  append local_field_cat to global_field_cat.

  clear local_field_cat.
  local_field_cat-fieldname          = 'EMAIL_TIME'.
  local_field_cat-tabname            = 'Global_zemail_001'.
  local_field_cat-seltext_m          = 'Time Generated'.
  append local_field_cat to global_field_cat.

  clear local_field_cat.
  local_field_cat-fieldname          = 'SUBJECT'.
  local_field_cat-tabname            = 'Global_zemail_001'.
  local_field_cat-seltext_m          = 'Subject'.
  append local_field_cat to global_field_cat.

  clear local_field_cat.
  local_field_cat-fieldname          = 'CREATED_USER'.
  local_field_cat-tabname            = 'Global_zemail_001'.
  local_field_cat-seltext_m          = 'E-Mail Created By'.
  append local_field_cat to global_field_cat.

  clear local_field_cat.
  local_field_cat-fieldname          = 'HISTORY'.
  local_field_cat-tabname            = 'Global_zemail_001'.
  local_field_cat-seltext_m          = 'History'.
  local_field_cat-hotspot            = 'X'.
  append local_field_cat to global_field_cat.

  clear local_field_cat.
  local_field_cat-fieldname          = 'VIEW'.
  local_field_cat-tabname            = 'Global_zemail_001'.
  local_field_cat-seltext_m          = 'View'.
  local_field_cat-hotspot            = 'X'.
  append local_field_cat to global_field_cat.

  clear local_field_cat.
  local_field_cat-fieldname          = 'DETAILS'.
  local_field_cat-tabname            = 'Global_zemail_001'.
  local_field_cat-seltext_m          = 'Details'.
  local_field_cat-hotspot            = 'X'.
  local_field_cat-edit               = 'X'.
  append local_field_cat to global_field_cat.

  clear local_field_cat.
  local_field_cat-fieldname          = 'RESEND'.
  local_field_cat-tabname            = 'Global_zemail_001'.
  local_field_cat-seltext_m          = 'Details'.
  local_field_cat-hotspot            = 'X'.
  append local_field_cat to global_field_cat.

  global_layout-no_input             = 'X'.
  global_layout-colwidth_optimize    = 'X'.
  global_layout-zebra                = 'X'.
  global_layout-lights_fieldname     = 'TRAFFIC_LIGHT'.
  global_layout-lights_tabname       = 'GLOBAL_ZEMAIL_001'.
  global_layout-info_fieldname       = 'LINE_COLOR'.

  global_repid = sy-repid.
  local_title  = 'Generic E-Mail Review'.

  clear ls_event_exit.
  ls_event_exit-ucomm = gc_refresh.    " Refresh
  ls_event_exit-after = c_x.
  append ls_event_exit to lt_event_exit.

  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      i_callback_program      = global_repid
      is_layout               = global_layout
      it_fieldcat             = global_field_cat
      i_grid_title            = local_title
      i_callback_user_command = 'USECOMMAND'
      it_event_exit           = lt_event_exit
    tables
      t_outtab                = global_zemail_001
    exceptions
      program_error           = 1
      others                  = 2.
endform.  
As you can see the i_callback_user_command = 'USECOMMAND' is populated.
This allows the program to handle events.
USECOMMAND Routine
form usecommand using r_ucomm like sy-ucomm rs_selfield type slis_selfield.
  data: local_title     type lvc_title,
        local_field_cat type slis_fieldcat_alv,
        local_date(8)   type c,
        local_tab_006   type table of zemail_006 with header line,
        local_com_type  type zemail_002-com_type,
        local_commandline(200) type c.

  case r_ucomm.
    when '&IC1'.
      case rs_selfield-value.
        when 'Details'.
This builds the following ALV Grid
clear global_field_cat.
          refresh: global_field_cat.

          clear local_field_cat.
          local_field_cat-fieldname          = 'VIEW'.
          local_field_cat-tabname            = 'Global_zemail_002'.
          local_field_cat-seltext_m          = 'View'.
          local_field_cat-icon               = 'X'.
          local_field_cat-hotspot            = 'X'.
          append local_field_cat to global_field_cat.
          clear  local_field_cat.

          local_field_cat-fieldname          = 'SOURCE_PROGRAM'.
          local_field_cat-tabname            = 'global_zemail_002'.
          local_field_cat-seltext_m          = 'Source'.
          local_field_cat-hotspot            = ''.
          append local_field_cat to global_field_cat.
          clear local_field_cat.

          local_field_cat-fieldname          = 'EMAIL_DATE'.
          local_field_cat-tabname            = 'global_zemail_002'.
          local_field_cat-seltext_m          = 'Date Generated'.
          local_field_cat-hotspot            = ''.
          append local_field_cat to global_field_cat.
          clear local_field_cat.

          local_field_cat-fieldname          = 'EMAIL_TIME'.
          local_field_cat-tabname            = 'global_zemail_002'.
          local_field_cat-seltext_m          = 'Time Generated'.
          append local_field_cat to global_field_cat.
          clear local_field_cat.

          local_field_cat-fieldname          = 'SEQUENCE'.
          local_field_cat-tabname            = 'global_zemail_002'.
          local_field_cat-seltext_m          = 'Sequence'.
          append local_field_cat to global_field_cat.
          clear local_field_cat.

          local_field_cat-fieldname          = 'EMAIL_ADDRESS'.
          local_field_cat-tabname            = 'global_zemail_002'.
          local_field_cat-seltext_m          = 'Address to send to'.
          local_field_cat-edit               = 'X'.
          local_field_cat-outputlen          = 50.
          local_field_cat-hotspot            = ''.
          append local_field_cat to global_field_cat.
          clear local_field_cat.

          clear local_field_cat.
          local_field_cat-fieldname          = 'SENT'.
          local_field_cat-tabname            = 'Global_zemail_001'.
          local_field_cat-seltext_m          = 'Valid'.
          append local_field_cat to global_field_cat.

          local_field_cat-fieldname          = 'SEND'.
          local_field_cat-tabname            = 'Global_zemail_002'.
          local_field_cat-seltext_m          = 'Send'.
          local_field_cat-icon               = 'X'.
          local_field_cat-hotspot            = 'X'.
          append local_field_cat to global_field_cat.
          clear  local_field_cat.

          local_field_cat-fieldname          = 'UPDATE'.
          local_field_cat-tabname            = 'Global_zemail_002'.
          local_field_cat-seltext_m          = 'Update'.
          local_field_cat-icon               = 'X'.
          local_field_cat-hotspot            = 'X'.
          append local_field_cat to global_field_cat.
          clear  local_field_cat.

          read table global_zemail_001 index rs_selfield-tabindex.

          concatenate 'Details of Email ' global_zemail_001-subject ' For ' global_zemail_001-kunnr
                     into local_title respecting blanks.

          select source_program email_date
                 email_time email_address sequence sent com_type
           from zemail_002 into table global_zemail_002
                                   where source_program = global_zemail_001-source_program
                                     and email_date     = global_zemail_001-email_date
                                     and email_time     = global_zemail_001-email_time.
          loop at global_zemail_002.
            global_zemail_002-send = 'Send'.
            global_zemail_002-view = 'View'.
            global_zemail_002-update = 'Update'.
            modify global_zemail_002.
          endloop.
          call function 'REUSE_ALV_GRID_DISPLAY'
            exporting
              i_callback_program      = global_repid
              is_layout               = global_layout
              it_fieldcat             = global_field_cat
              i_grid_title            = local_title
              i_callback_user_command = 'USECOMMAND2'
            tables
              t_outtab                = global_zemail_002
            exceptions
              program_error           = 1
              others                  = 2.

        when 'View'.
This builds the Routine to display attachments
read table global_zemail_001 index rs_selfield-tabindex.
          if global_zemail_001-stored_offline <> 'Y'.
            perform get_attachment.
          else.
            if global_zemail_001-attachment <> ''.
              perform run_java.
              wait up to 60 seconds.
            else.
              select * from zemail_006 into table global_attachment
                  where source_program = global_zemail_001-source_program
                    and email_date     = global_zemail_001-email_date
                    and email_time     = global_zemail_001-email_time.
              if sy-subrc = 0.
                loop at global_attachment.
                  global_zemail_001-attachment = 
                                  global_attachment-attachment.
                  perform run_java.
                  wait up to 120 seconds.
                  global_mult = 'Y'.
                  update zemail_006 set stored_offline = ' '
                       where source_program = 
                                  global_zemail_001-source_program
                         and email_date     = global_zemail_001-email_date
                         and email_time     = global_zemail_001-email_time.
                endloop.
              endif.
            endif.
          endif.
          if global_zemail_001-type_attachment <> 'PDF'.
            perform view_attachment.
          else.
            local_commandline = global_zemail_001-attachment.
            if not local_commandline is initial.
              translate  local_commandline using '/\'.
              if sy-sysid = 'QAS'.
                sy-sysid  = 'TST'.
              endif.
              if sy-sysid = 'PRD'.
                sy-sysid  = 'PROD'.
              endif.
              concatenate '\\SAP' 
                sy-sysid  '\rootbin' local_commandline into 
                                 local_commandline.
              call function 'WS_EXECUTE'
                exporting
                  program        = 
                  'C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.EXE'
                  commandline    = local_commandline
                  inform         = ''
                exceptions
                  prog_not_found = 1.
            else.
              select * from zemail_006 into table local_tab_006
                  where source_program = global_zemail_001-source_program
                    and email_date     = global_zemail_001-email_date
                    and email_time     = global_zemail_001-email_time.
              loop at local_tab_006.
                local_commandline = local_tab_006-attachment.
                translate  local_commandline using '/\'.
                if sy-sysid = 'QAS'.
                  sy-sysid = 'TST'.
                endif.
                if sy-sysid = 'PRD'.
                  sy-sysid = 'PROD'.
                endif.
If you were ever wondering how to call a windows program well here you go.
First you have to setup up the path to the file you want to display.
The function module WS_EXECUTE allows you to call a program located on your PC in this case I am calling the Adobe Reader I am sure there are better ways of doing this but this works.
concatenate '\\SAP' sy-sysid  '\rootbin' local_commandline
                   into local_commandline.
                call function 'WS_EXECUTE'
                  exporting
                    program        = 
                   'C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.EXE'
                    commandline    = local_commandline
                    inform         = ''
                  exceptions
                    prog_not_found = 1.
              endloop.
            endif.
          endif.
        when 'History'.
This routine will display the following Grid.
data: begin of local_zemail_008 occurs 0,
                  email_address    type zemail_008-email_address,
                  sent_date        type zemail_008-sent_date,
                  sent_time        type zemail_008-sent_time,
                  status           type zemail_008-status,
                  sequence         type zemail_008-sequence,
                  com_type         type zemail_008-com_type,
                  traffic_light(1) type c,
                end of local_zemail_008.

          clear global_field_cat.
          refresh: global_field_cat.

          local_field_cat-fieldname          = 'EMAIL_ADDRESS'.
          local_field_cat-tabname            = 'Global_zemail_008'.
          local_field_cat-seltext_m          = 'Address'.
          append local_field_cat to global_field_cat.
          clear  local_field_cat.

          local_field_cat-fieldname          = 'SENT_DATE'.
          local_field_cat-tabname            = 'Global_zemail_008'.
          local_field_cat-seltext_m          = 'Date Sent'.
          append local_field_cat to global_field_cat.
          clear  local_field_cat.

          local_field_cat-fieldname          = 'SENT_TIME'.
          local_field_cat-tabname            = 'Global_zemail_008'.
          local_field_cat-seltext_m          = 'Date Time'.
          append local_field_cat to global_field_cat.
          clear  local_field_cat.

          local_field_cat-fieldname          = 'STATUS'.
          local_field_cat-tabname            = 'Global_zemail_008'.
          local_field_cat-seltext_m          = 'Reason For Failure'.
          append local_field_cat to global_field_cat.
          clear  local_field_cat.

          read table global_zemail_001 index rs_selfield-tabindex.

          concatenate 'History of Email ' global_zemail_001-subject
                     into local_title respecting blanks.

          select email_address sent_date sent_time status sequence com_type
                                   from zemail_008 into table
                                        local_zemail_008
                 where source_program = global_zemail_001-source_program
                   and email_date     = global_zemail_001-email_date
                   and email_time     = global_zemail_001-email_time.

          loop at local_zemail_008.
            local_zemail_008-traffic_light = '3'.
            if local_zemail_008-com_type = 'TEL'.
              if local_zemail_008-status = ''.
                local_zemail_008-traffic_light = '2'.
              else.
                if local_zemail_008-status = 'Verify' or 
                   local_zemail_008-status = 'Verifed'.
                   local_zemail_008-traffic_light = '3'.
                else.
                  local_zemail_008-traffic_light = '1'.
                endif.
              endif.
            endif.
            modify local_zemail_008.
          endloop.

          call function 'REUSE_ALV_GRID_DISPLAY'
            exporting
              i_callback_program = global_repid
              is_layout          = global_layout
              it_fieldcat        = global_field_cat
              i_grid_title       = local_title
            tables
              t_outtab           = local_zemail_008
            exceptions
              program_error      = 1
              others             = 2.
        when 'Resend'.
          read table global_zemail_001 index rs_selfield-tabindex.
          update  zemail_001 set status = 'L'
          where source_program = global_zemail_001-source_program
            and email_date = global_zemail_001-email_date
            and email_time = global_zemail_001-email_time
            and kunnr = global_zemail_001-kunnr.
          perform open_group.
          concatenate global_zemail_001-email_date+4(4)
              global_zemail_001-email_date+0(4)
              into  local_date.
          perform bdc_dynpro      using 'ZGENERIC_EMAIL' '1000'.
          perform bdc_field       using 'BDC_CURSOR' 'S_TEST-LOW'.
          perform bdc_field       using 'BDC_OKCODE' '=ONLI'.
          perform bdc_field       using 'S_SOURCE-LOW' 
                  global_zemail_001-source_program.
          perform bdc_field       using 'S_DATE-LOW' local_date.
          perform bdc_field       using 'S_TIME-LOW' 
                  global_zemail_001-email_time.
          perform bdc_transaction using 'ZEMAIL_SEND'.
          call function 'BDC_CLOSE_GROUP'.
          call transaction 'ZEMAIL_SEND' using bdcdata mode 'E'.
          clear bdcdata.
          refresh bdcdata.
      endcase.
    when '&REFRESH'.
      perform generic_process.
  endcase.
endform.                    "usecommand
The next routine handles events from the prior routine.
form usecommand2 using r_ucomm like sy-ucomm rs_selfield type slis_selfield.
  types: begin of type_pdf,
           lines(250)          type c,
         end of type_pdf.
  data: pdf_table              type table of type_pdf with header line,
        local_tab_006          type table of zemail_006 with header line,
        pdf_fsize              type i,
        local_date(8)          type c,
        local_commandline(200) type c.
  case r_ucomm.
    when '&IC1'.
      case rs_selfield-value.
        when 'Send'.
          read table global_zemail_002 index rs_selfield-tabindex.
          concatenate global_zemail_002-email_date+4(4)
                  global_zemail_002-email_date+0(4)
                  into  local_date.
          perform open_group.
          perform bdc_dynpro      using 'ZGENERIC_EMAIL' '1000'.
          perform bdc_field       using 'BDC_CURSOR' 'S_TEST-LOW'.
          perform bdc_field       using 'BDC_OKCODE' '=ONLI'.
          perform bdc_field       using 'S_SOURCE-LOW' global_zemail_002-source_program.
          perform bdc_field       using 'S_DATE-LOW' local_date.
          perform bdc_field       using 'S_TIME-LOW' global_zemail_002-email_time.
          perform bdc_field       using 'S_ONLY-LOW' global_zemail_002-email_address.
          perform bdc_field       using 'S_TYPE-LOW' global_zemail_002-com_type.
          perform bdc_transaction using 'ZEMAIL_SEND'.
          call function 'BDC_CLOSE_GROUP'.
          call transaction 'ZEMAIL_SEND' using bdcdata mode 'E'.
          refresh bdcdata.
          clear bdcdata.
        when 'View'.
          if global_zemail_001-stored_offline <> 'Y'.
            perform get_attachment.
          else.
            if global_zemail_001-attachment <> ''.
              perform run_java.
              wait up to 60 seconds.
            else.
              select * from zemail_006 into table global_attachment
                  where source_program = global_zemail_001-source_program
                    and email_date     = global_zemail_001-email_date
                    and email_time     = global_zemail_001-email_time.
              if sy-subrc = 0.
                loop at global_attachment.
                  global_zemail_001-attachment = global_attachment-attachment.
                  perform run_java.
                  wait up to 120 seconds.
                  global_mult = 'Y'.
                  update zemail_006 set stored_offline = ' '
                       where source_program = global_zemail_001-source_program
                         and email_date     = global_zemail_001-email_date
                         and email_time     = global_zemail_001-email_time.
                endloop.
              endif.
            endif.
          endif.
          if global_zemail_001-type_attachment <> 'PDF'.
            perform view_attachment.
          else.
            local_commandline = global_zemail_001-attachment.
            if not local_commandline is initial.
              translate  local_commandline using '/\'.
              if sy-sysid = 'QAS'.
                sy-sysid  = 'TST'.
              endif.
              if sy-sysid = 'PRD'.
                sy-sysid  = 'PROD'.
              endif.
              concatenate '\\SAP' sy-sysid  '\rootbin' local_commandline into local_commandline.
              call function 'WS_EXECUTE'
                exporting
                  program        = 'C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.EXE'
                  commandline    = local_commandline
                  inform         = ''
                exceptions
                  prog_not_found = 1.
            else.
              select * from zemail_006 into table local_tab_006
                                       where source_program = global_zemail_001-source_program
                                         and email_date     = global_zemail_001-email_date
                                         and email_time     = global_zemail_001-email_time.
              loop at local_tab_006.
                local_commandline = local_tab_006-attachment.
                translate  local_commandline using '/\'.
                if sy-sysid = 'QAS'.
                  sy-sysid = 'TST'.
                endif.
                if sy-sysid = 'PRD'.
                  sy-sysid = 'PROD'.
                endif.
                concatenate '\\SAP' sy-sysid  '\rootbin' local_commandline into local_commandline.
                call function 'WS_EXECUTE'
                  exporting
                    program        = 'C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.EXE'
                    commandline    = local_commandline
                    inform         = ''
                  exceptions
                    prog_not_found = 1.
              endloop.
            endif.
          endif.
        when 'Update'.
          read table global_zemail_002 index rs_selfield-tabindex.
          modify global_zemail_002 index rs_selfield-tabindex.
          update zemail_002 set email_address = global_zemail_002-email_address
                 where source_program = global_zemail_002-source_program
                   and email_date = global_zemail_002-email_date
                   and email_time = global_zemail_002-email_time
                   and sequence   = global_zemail_002-sequence.
          data: local_time type i.
          local_time = 5.
* Pause execution for P_TIME seconds.
          call function 'ENQUE_SLEEP'
            exporting
              seconds = local_time.
      endcase.
  endcase.
endform.
Get_Attachment
form get_attachment.
  data: local_file_name(100) type c.
  clear: local_file_name,
         it_attach.
  refresh: it_attach.

  local_file_name = global_zemail_001-attachment.
  open dataset local_file_name for input in text mode
                                encoding default
                                ignoring
                                conversion errors.
  if sy-subrc eq 0.
    do.
      read dataset local_file_name into it_attach.
      if sy-subrc <> 0.
        exit.
      endif.
      append it_attach.
      clear it_attach.
    enddo.
    close dataset local_file_name.
  endif.
endform.
The following routines handles the BDC Routines
form bdc_transaction using tcode.
* batch input session
  call function 'BDC_INSERT'
    exporting
      tcode     = tcode
    tables
      dynprotab = bdcdata.
endform.                    "bdc_transaction
*&---------------------------------------------------------------------*
*&      Form  bdc_field
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->FNAM       text
*      -->FVAL       text
*----------------------------------------------------------------------*
form bdc_field  using  fnam fval.
  clear bdcdata.
  bdcdata-fnam = fnam.
  bdcdata-fval = fval.
  append bdcdata.
endform.                    " BDC_FIELD
*&---------------------------------------------------------------------*
*&      Form  bdc_dynpro
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PROGRAM    text
*      -->DYNPRO     text
*----------------------------------------------------------------------*
form bdc_dynpro using program dynpro.
  clear bdcdata.
  bdcdata-program  = program.
  bdcdata-dynpro   = dynpro.
  bdcdata-dynbegin = 'X'.
  append bdcdata.
endform.                    "BDC_DYNPRO
*&---------------------------------------------------------------------*
*&      Form  open_group
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form open_group.
  call function 'BDC_OPEN_GROUP'
    exporting
      client = sy-mandt
      group  = 'EMAIL_TRAN'
      user   = sy-uname
      keep   = 'X'.
endform.
This routine will display text files
form view_attachment.
  data: local_title     type lvc_title,
        local_field_cat type slis_fieldcat_alv.
  local_title = 'View Attachment'.
  refresh global_field_cat.
  clear local_field_cat.
  local_field_cat-fieldname    = 'LINE'.
  local_field_cat-tabname      = 'IT_ATTACH'.
  local_field_cat-seltext_m    = 'Text'.
  append local_field_cat to global_field_cat.

  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      i_callback_program = global_repid
      is_layout          = global_layout
      it_fieldcat        = global_field_cat
      i_grid_title       = local_title
    tables
      t_outtab           = it_attach
    exceptions
      program_error      = 1
      others             = 2.

endform.
I am including this routine but it will not help you this is to pull attachments from Amazon's Cloud
form run_java .
  data: local_commandline(350) type c,
        local_hold(350)        type c,
        local_hold2(250)       type c,
        local_file(100)        type c,
        local_position         type i.


  if sy-sysid = 'QAS'.
    sy-sysid  = 'TST'.
  endif.
  if sy-sysid = 'PRD'.
    sy-sysid  = 'PROD'.
  endif.
  search global_zemail_001-attachment for '/usr/sap/transfer/sapout/email/ar/stop_ship/'.
  if sy-subrc = 0.
    local_position = sy-fdpos + 44.
    local_file = global_zemail_001-attachment+local_position(100).
  else.
    search global_zemail_001-attachment  for '/usr/sap/transfer/sapout/email/ar/zauto/'.
    if sy-subrc = 0.
      local_position = sy-fdpos + 40.
      local_file = global_zemail_001-attachment+local_position(100).
    else.
      search global_zemail_001-attachment  for '/usr/sap/transfer/sapout/email/ap/zchecks/'.
      if sy-subrc = 0.
        local_position = sy-fdpos + 42.
        local_file = global_zemail_001-attachment+local_position(100).
      else.
        search global_zemail_001-attachment  for '/usr/sap/transfer/sapout/email/'.
        if sy-subrc = 0.
          local_position = sy-fdpos + 31.
          local_file = global_zemail_001-attachment+local_position(100).
        endif.
      endif.
    endif.
  endif.
  replace all occurrences of '/' in global_zemail_001-attachment with '\' .
  concatenate '"\\SAP' sy-sysid '\rootbin' global_zemail_001-attachment '"'
              into local_hold2.
  concatenate '"' local_file '"'
              into local_hold.
  concatenate local_hold local_hold2 into local_commandline separated by ' '.

  call function 'WS_EXECUTE'
    exporting
      program        = '\\mpfs-portalprod\sap\aws\dist\recvaws.bat'
      commandline    = local_commandline
      inform         = ''
    exceptions
      prog_not_found = 1.

  if sy-subrc = 0.
    if global_mult <> 'Y'.
      update zemail_001 set stored_offline = ' '
             where source_program = global_zemail_001-source_program
               and email_date     = global_zemail_001-email_date
               and email_time     = global_zemail_001-email_time.
    endif.
  endif.
endform.
If you would like the Java program I am using to do this let me know
form run_java .
  data: local_commandline(350) type c,
        local_hold(350)        type c,
        local_hold2(250)       type c,
        local_file(100)        type c,
        local_position         type i.


  if sy-sysid = 'QAS'.
    sy-sysid  = 'TST'.
  endif.
  if sy-sysid = 'PRD'.
    sy-sysid  = 'PROD'.
  endif.
  search global_zemail_001-attachment for '/usr/sap/transfer/sapout/email/ar/stop_ship/'.
  if sy-subrc = 0.
    local_position = sy-fdpos + 44.
    local_file = global_zemail_001-attachment+local_position(100).
  else.
    search global_zemail_001-attachment  for '/usr/sap/transfer/sapout/email/ar/zauto/'.
    if sy-subrc = 0.
      local_position = sy-fdpos + 40.
      local_file = global_zemail_001-attachment+local_position(100).
    else.
      search global_zemail_001-attachment  for '/usr/sap/transfer/sapout/email/ap/zchecks/'.
      if sy-subrc = 0.
        local_position = sy-fdpos + 42.
        local_file = global_zemail_001-attachment+local_position(100).
      else.
        search global_zemail_001-attachment  for '/usr/sap/transfer/sapout/email/'.
        if sy-subrc = 0.
          local_position = sy-fdpos + 31.
          local_file = global_zemail_001-attachment+local_position(100).
        endif.
      endif.
    endif.
  endif.
  replace all occurrences of '/' in global_zemail_001-attachment with '\' .
  concatenate '"\\SAP' sy-sysid '\rootbin' global_zemail_001-attachment '"'
              into local_hold2.
  concatenate '"' local_file '"'
              into local_hold.
  concatenate local_hold local_hold2 into local_commandline separated by ' '.

  call function 'WS_EXECUTE'
    exporting
      program        = '\\mpfs-portalprod\sap\aws\dist\recvaws.bat'
      commandline    = local_commandline
      inform         = ''
    exceptions
      prog_not_found = 1.

  if sy-subrc = 0.
    if global_mult <> 'Y'.
      update zemail_001 set stored_offline = ' '
             where source_program = global_zemail_001-source_program
               and email_date     = global_zemail_001-email_date
               and email_time     = global_zemail_001-email_time.
    endif.
  endif.
endform.
Again if you want a text file verison of this code drop me a note and I would be happy to send it to you.