Thursday, December 22, 2016

concerning iDOCs

Transactions

WE05IDOC Liste
WE09IDOC(Nach IDOCS über Inhalte bzw. Felder suchen)
WE20Partnevereinbarungen(Verb. Zwischen Belegen und IDOC Generiereung)
BD87Nachverarbeitung von IDOCS
WE19Anlage und Verarbeitung von Testidocs(Als Vorlage bestehendes IDOC)
WEDIÜberblick
WE60Dokumention Idoc Struktur

Tables

TBDBEBAPI-ALE Interface for Inbound Processing
Function modules to process inbound, per message type

Function Modules

BAPI_IDOC_INPUT1Inbound BAPI IDoc: Individual Processing

Friday, December 9, 2016

function module to get list of application servers

    call function 'TH_SERVER_LIST'
      tables
        list           = lt_server
      exceptions
        no_server_list = 1
        others         = 2.

check with abap which jobs are running

select single jobname 
  from tbtco 
  into lv_jobname
  where 

    jobname like lv_jobname_pat and 
    status = 'R'.

create and start background jobs from abap

    data lv_job_was_released type abap_bool.
    data lv_ret type i.
    data lv_stepcount type btcstepcnt.

    call function 'JOB_OPEN'
      exporting
        jobname          = lv_jobname
      importing
        jobcount         = lv_jobcount
      changing
        ret              = lv_ret
      exceptions
        cant_create_job  = 1
        invalid_job_data = 2
        jobname_missing  = 3
        others           = 4.

    if sy-subrc <> 0.
      " ...
      return.
    endif.

*    submit (mo_run->ms_run-client_report)
*      using selection-set mv_variant
*      user sy-uname via job mv_jobname number mv_jobcount
*      and return.
* Starting the report with submit, SM37  will display a 

* temporary variant 0.. insteat of the created variant 

    call function 'JOB_SUBMIT'
      exporting
        authcknam               = sy-uname
        jobcount                = lv_jobcount
        jobname                 = lv_jobname
        report                  = lv_client_report
        variant                 = lv_variant
      importing
        step_number             = lv_stepcount
      exceptions
        bad_priparams           = 1
        bad_xpgflags            = 2
        invalid_jobdata         = 3
        jobname_missing         = 4
        job_notex               = 5
        job_submit_failed       = 6
        lock_failed             = 7
        program_missing         = 8
        prog_abap_and_extpg_set = 9
        others                  = 10.

    if sy-subrc <> 0.
      " ...
      return.
    endif.

    call function 'JOB_CLOSE'
      exporting
        jobcount             = lv_jobcount
        jobname              = lv_jobname
        targetsystem         = lv_server
        strtimmed            = 'X' " start immediately
      importing
        job_was_released     = lv_job_was_released
      changing
        ret                  = lv_ret
      exceptions
        cant_start_immediate = 1
        invalid_startdate    = 2
        jobname_missing      = 3
        job_close_failed     = 4
        job_nosteps          = 5
        job_notex            = 6
        lock_failed          = 7
        invalid_target       = 8
        others               = 9.

    if sy-subrc <> 0.
      " ...
      return.
    endif.

save report variants for jobs

    data ls_varid type varid.
    data ls_varit type varit.
    data lt_varit type standard table of varit with empty key.
 
    ls_varid-mandt      = sy-mandt. " Mandant
    ls_varid-report     = lv_client_report. 
    ls_varid-variant    = lv_variant. 
    ls_varid-flag1      = ''. 

    ls_varid-flag2      = ''. 
    ls_varid-transport  = ''. 
    ls_varid-environmnt = 'B'. " Batch
    ls_varid-protected  = ''. 
    ls_varid-secu       = ''. " Berechtigungsgruppe
    ls_varid-version    = ''. " Versionsnummer der Variante
    ls_varid-ename      = sy-uname. 
    ls_varid-edat       = sy-datum. " 
    ls_varid-etime      = sy-uzeit. " 
    ls_varid-aename     = sy-uname. 
    ls_varid-aedat      = sy-datum. 
    ls_varid-aetime     = sy-uzeit. 
    ls_varid-mlangu     = sy-langu. 
    ls_varid-xflag1     = ''. 
    ls_varid-xflag2     = ''. 

    ls_varit-mandt = sy-mandt.
    ls_varit-langu = sy-langu.
    ls_varit-report = lv_client_report.
    ls_varit-variant = lv_variant.
    ls_varit-vtext = |My Variant { sy-datum date = iso } | ?? 

      |{ sy-uzeit time = iso }|.
    insert ls_varit into table lt_varit.

    call function 'RS_CREATE_VARIANT'
      exporting
        curr_report               = lv_client_report
        curr_variant              = lv_variant
        vari_desc                 = ls_varid
      tables
        vari_contents             = lt_rsparam
        vari_text                 = lt_varit
      exceptions
        illegal_report_or_variant = 1
        illegal_variantname       = 2
        not_authorized            = 3
        not_executed              = 4
        report_not_existent       = 5
        report_not_supplied       = 6
        variant_exists            = 7
        variant_locked            = 8
        others                    = 9.

    if sy-subrc = 0.
      commit work.


    else.
      " s...
    endif.

Monday, November 28, 2016

Convert internal table to CSV

report zjso_itab_to_csv.

types gtt_string type standard table of string with empty key.

class lcl_conv definition.
  public section.
    class-methods conv importing it_input type any table returning value(et_csv) type gtt_string.
    class-methods struct_to_csv_row importing is_row type any returning value(rv_csv) type string.
    class-methods struct_shorttext_to_csv_row importing is_row type any returning value(rv_csv) type string.
    class-methods convert_any_to_string importing iv_any type any returning value(rv_string) type string.
    class-methods esc importing iv_string type string returning value(rv_string) type string.
endclass.

start-of-selection.
  select * from t001w into table @data(gt_t001w).
  cl_demo_output=>display_data( lcl_conv=>conv( gt_t001w ) ).

class lcl_conv implementation.
  method conv.
    field-symbols <ls_row> type any.

    loop at it_input assigning <ls_row>.
      if sy-tabix = 1.
        insert struct_shorttext_to_csv_row( <ls_row> ) into table et_csv.
      endif.
      insert struct_to_csv_row( <ls_row> ) into table et_csv.
    endloop.
  endmethod.


  method struct_to_csv_row.
    data lv_component_count type i.
    data lv_datatype type c.                                "#EC NEEDED
    data lv_i type i.
    data lv_conv_string type string.
    field-symbols <lv_component> type any.

    clear rv_csv.
    describe field is_row type lv_datatype components lv_component_count.
    lv_i = 1.
    do lv_component_count times.
      assign component lv_i of structure is_row to <lv_component>.
      lv_conv_string = convert_any_to_string( <lv_component> ).
      if lv_i = 1.
        rv_csv = lv_conv_string.
      else.
        rv_csv = |{ rv_csv };{ lv_conv_string }|.
      endif.

      add 1 to lv_i.
    enddo.
  endmethod.


  method struct_shorttext_to_csv_row.
    data lo_sdesc type ref to cl_abap_structdescr.
    data lt_ddic_field type ddfields.
    data ls_ddic_field type dfies.
    data lv_tabix type sytabix.
    data lv_shorttext type string.

    clear rv_csv.
    lo_sdesc = cast #( cl_abap_structdescr=>describe_by_data( is_row ) ).
    lt_ddic_field = lo_sdesc->get_ddic_field_list( ).
    loop at lt_ddic_field into ls_ddic_field.
      lv_tabix = sy-tabix.
      lv_shorttext = esc( conv #( ls_ddic_field-fieldtext ) ).
      if lv_tabix = 1.
        rv_csv = lv_shorttext.
      else.
        rv_csv = |{ rv_csv };{ lv_shorttext }|.
      endif.
    endloop.
  endmethod.


  method convert_any_to_string.
    data lv_type type c.
    data lv_d type d.
    data lv_t type t.

    clear rv_string.
    describe field iv_any type lv_type.

    case lv_type.
      when 'D'. " Date
        lv_d = iv_any.
        rv_string = |{ lv_d date = user }|.
      when 'T'. " time
        lv_t = iv_any.
        rv_string = |{ lv_t time = user }|.
      when others.
        rv_string = iv_any.
    endcase.

    rv_string = esc( rv_string ).
  endmethod.


  method esc.
    if iv_string cs ';'.
      rv_string = |'{ rv_string }'|.
    else.
      rv_string = iv_string.
    endif.
  endmethod.

endclass.

Monday, October 31, 2016

Schedule duration on factory calendar in ERP

form schedule
  using
    iv_start_date type d
    iv_factorty_calendar type wfcid
    iv_length type i
  changing ev_end_date type d.

  data lv_factory_date type facdate.

  clear ev_end_date.

  call function 'DATE_CONVERT_TO_FACTORYDATE'
    exporting
      date                         = iv_start_date
      factory_calendar_id          = iv_factorty_calendar
    importing
      factorydate                  = lv_factory_date
    exceptions
      calendar_buffer_not_loadable = 1
      correct_option_invalid       = 2
      date_after_range             = 3
      date_before_range            = 4
      date_invalid                 = 5
      factory_calendar_not_found   = 6
      others                       = 7.

  if sy-subrc <> 0.
    write / 'Exception'.
    ev_end_date = iv_start_date + iv_length.
    return.
  endif.

  add iv_length to lv_factory_date.

  call function 'FACTORYDATE_CONVERT_TO_DATE'
    exporting
      factorydate                  = lv_factory_date
      factory_calendar_id          = iv_factorty_calendar
    importing
      date                         = ev_end_date
    exceptions
      calendar_buffer_not_loadable = 1
      factorydate_after_range      = 2
      factorydate_before_range     = 3
      factorydate_invalid          = 4
      factory_calendar_id_missing  = 5
      factory_calendar_not_found   = 6
      others                       = 7.

  if sy-subrc <> 0.
    write / 'Exception'.
    ev_end_date = iv_start_date + iv_length.
  endif.

endform.

Friday, October 28, 2016

Selection Screen with Maximum Selection (UP TO ... ROWS) MXSEL


constants gc_max_rows type tbmaxsel value '100000'. 
constants gc_default_rows type tbmaxsel value '200'. 

parameter p_mxsel type tbmaxsel obligatory default gc_default_rows.

initialization.
  select single tbmaxsel from rseumod into p_mxsel 

    where uname = sy-uname.
  perform trim_mxsel.

at selection-screen on p_mxsel.
  perform trim_mxsel.

start-of-selection.
  p_mxsel = p_mxsel + 1.
  select * from ...
    into corresponding fields of table gt_my
    up to p_mxsel rows
    where
       ...
  if gt_my is initial.
    message s429(mo).     " no data found
    return.
  endif.
  if lines( gt_my ) = p_mxsel.
    
    delete gt_my index p_mxsel.
    message s016(es) with p_mxsel. " result cut off
  endif.
  p_mxsel = p_mxsel - 1.


form trim_mxsel.
  if p_mxsel > gc_max_rows. p_mxsel = gc_max_rows. endif.
  if p_mxsel < 1. p_mxsel = gc_default_rows. endif.
endform.

Wednesday, October 26, 2016

RFC call function destination with standard exceptions

  data lv_exception_message type text255.

  call function 'REMOTE_FUNCTION'
    destination iv_rfc_destination
[...]
    exceptions
      system_failure        = 1  message lv_exception_message
      communication_failure = 2  message lv_exception_message

      others                = 3.


Datatype string is not supported.

Friday, October 21, 2016

Creating Background Jobs in SAP with ABAP

Starting Jobs

    call function 'JOB_OPEN'
      exporting
        jobname          = mv_jobname
      importing
        jobcount         = mv_jobcount
      changing
        ret              = lv_ret
      exceptions
        cant_create_job  = 1
        invalid_job_data = 2
        jobname_missing  = 3
        others           = 4.

    if sy-subrc <> 0.
      " log
      return.
    endif.

*    submit (mo_run->ms_run-client_report)
*      using selection-set mv_variant
*      user sy-uname via job mv_jobname number mv_jobcount
*      and return.
* Using the variant above, SM37 will create a temporary

* Variante ..001 but not the previously created variant

    call function 'JOB_SUBMIT'
      exporting
        authcknam               = sy-uname
        jobcount                = mv_jobcount
        jobname                 = mv_jobname
        report                  = mo_run->ms_run-client_report
        variant                 = mv_variant
      importing
        step_number             = lv_stepcount
      exceptions
        bad_priparams           = 1
        bad_xpgflags            = 2
        invalid_jobdata         = 3
        jobname_missing         = 4
        job_notex               = 5
        job_submit_failed       = 6
        lock_failed             = 7
        program_missing         = 8
        prog_abap_and_extpg_set = 9
        others                  = 10.

    if sy-subrc <> 0.
      " log
      return.
    endif.

    call function 'JOB_CLOSE'
      exporting
        jobcount             = mv_jobcount
        jobname              = mv_jobname
        targetsystem         = iv_server
        strtimmed            = 'X' " start immediately
      importing
        job_was_released     = lv_job_was_released
      changing
        ret                  = lv_ret
      exceptions
        cant_start_immediate = 1
        invalid_startdate    = 2
        jobname_missing      = 3
        job_close_failed     = 4
        job_nosteps          = 5
        job_notex            = 6
        lock_failed          = 7
        invalid_target       = 8
        others               = 9.

    if sy-subrc <> 0.
       " log
      return.
    endif.


Creating Variants

    data ls_varid type varid.

    ls_varid-mandt      = sy-mandt. " Mandant
    ls_varid-report     = mo_run->ms_run-client_report. 
    ls_varid-variant    = mv_variant. 
    ls_varid-flag1      = ''. " CHAR01-Datenelement fuer SYST
    ls_varid-flag2      = ''. " CHAR01-Datenelement fuer SYST
    ls_varid-transport  = ''. 
    ls_varid-environmnt = 'B'. " Umgebung (B = nur Batch)
    ls_varid-protected  = ''. 
    ls_varid-secu       = ''. " Berechtigungsgruppe
    ls_varid-version    = ''. " Versionsnummer der Variante
    ls_varid-ename      = sy-uname. " Benutzername
    ls_varid-edat       = sy-datum. " DATE (8-stelig) fuer SYST
    ls_varid-etime      = sy-uzeit. " TIME-Datenelement fuer SYST
    ls_varid-aename     = sy-uname. " Benutzername
    ls_varid-aedat      = sy-datum. " DATE (8-stelig) fuer SYST
    ls_varid-aetime     = sy-uzeit. " TIME-Datenelement fuer SYST
    ls_varid-mlangu     = sy-langu. " Sprachenschlüssel
    ls_varid-xflag1     = ''. " Flag für Variantenkatalog
    ls_varid-xflag2     = ''. " Flag für Variantenkatalog

    " Varianten Text
    data ls_varit type varit.
    data lt_varit type standard table of varit.

    ls_varit-mandt = sy-mandt.
    ls_varit-langu = sy-langu.
    ls_varit-report = mo_run->ms_run-client_report.
    ls_varit-variant = mv_variant.
    ls_varit-vtext = |{ sy-datum date = iso }|

    append ls_varit to lt_varit.

* Save Variante for Job
    call function 'RS_CREATE_VARIANT'
      exporting
        curr_report               = mv_client_report
        curr_variant              = mv_variant
        vari_desc                 = ls_varid
      tables
        vari_contents             = mt_rsparam
        vari_text                 = lt_varit
      exceptions
        illegal_report_or_variant = 1
        illegal_variantname       = 2
        not_authorized            = 3
        not_executed              = 4
        report_not_existent       = 5
        report_not_supplied       = 6
        variant_exists            = 7
        variant_locked            = 8
        others                    = 9.

    if sy-subrc = 0.
      commit work.
    else.
      " log
      clear mv_variant.
    endif.


Monday, September 5, 2016

Very first steps in UI5

Precondition:

You have your Eclipse with UI5 extensions installed.
 

Create new UI5 project

  1. File - New - Other - SAP UI5 Application Development - Application Project
  2. Next
  3. Enter project's name
  4. choose sap.m library (sap.ui.commons is obsolete)
  5. Next
  6. Enter name of Initial view in "Name"
  7. choose XML as "Development Paradigm"
  8. Finish

Start application

  1. right click on project
  2. Run as - Web App preview
  3. Application starts in new tab in Eclipse
  4. Copy the link and start the app in Chrome
 

 Debugging

  • Use Chrome's Debugger, pressing F12
  • Add the Parameter ?sap-ui-debug=true to the URL

Thursday, September 1, 2016

Create heuristic with own algorithm in APO PP/DS: how to get data into /SAPAPO/HEURFUNC

How to create your own heuristic is very well described in Creating Heuristics Using Your Own Algorithm.
(@Thorsten: Many thanks for the fantastic heuristic framework!)

But how do you get your data in /SAPAPO/HEURFUNC? Just use SM30 with view /SAPAPO/VHEURFNC!

Tuesday, August 30, 2016

SALV with own toolbar

The SALV is really nice, unfortunately the toolbar can only be enhanced when the SALV with assigend to a container... so you have to provide a Dynpro


Example:


Selection Screen:
parameters p_layout type slis_vari.

at selection-screen on value-request for p_layout.
   p_layout
= lcl_o=>layout_f4( ).




The method pai is called from pai module of the dynpro containing the custom container CC
method pai.
  
check mo_cc is not bound.

  
" read data from db
  
read( ... ).

  
" prepare front end
  mo_cc
= new cl_gui_custom_container( 'CC' ).
  
try.
    
" setup SALV
    cl_salv_table
=>factory(
      
exporting r_container = mo_cc
      importing r_salv_table =
mo_salv
      changing t_table = ...

    ).


    " prepare SALV
     
mo_salv->get_columns( )->get_column( 'ORDERID' )->set_visible( abap_false ).
    cast cl_salv_column_table
(
      mo_salv->get_columns( )->get_column( 'ORDERNR' )
    )->set_cell_type( if_salv_c_cell_type=>hotspot ).
    mo_salv->get_sorts( )->add_sort( 'STARTTI' ).
    mo_salv->get_sorts( )->add_sort( 'ENDTI' ).
    mo_salv->get_selections( )->set_selection_mode(
     
if_salv_c_selection_mode=>multiple ).

    " prepare toolbar    

    mo_salv->get_functions( )->set_all( ). " activate toolbar
    mo_salv->get_functions( )->add_function(
      name = 'REFRESH'
      icon = '@42@'
      tooltip = conv string( 'Aktualisieren'(010) )
      position = if_salv_c_function_position=>
left_of_salv_functions
    ).

    
mo_salv->get_layout( )->set_key( value salv_s_layout_key(
      report = sy-repid ) ).
    mo_salv->get_layout( )->set_save_restriction(
     
cl_salv_layout=>restrict_none ).

    mo_salv->get_layout( )->set_initial_layout( mv_layout ).

    " setup event handlers
    set handler on_user_command for mo_salv->get_event( ).
    set handler on_hot_spot for mo_salv->get_event( ).

    " Display SALV
    mo_salv->display( ).
    catch
      cx_salv_msg
      cx_salv_not_found
      cx_salv_data_error
      cx_salv_existing
      cx_salv_wrong_call
into data(lo_x).
      message lo_x type 'E'.
   endtry.
endmethod.

method on_user_command.
case e_salv_function.
when 'CREATE'. dialog_create( ).
when 'DELETE'. dialog_delete( ).
when 'REFRESH'. refresh( ).
endcase.
endmethod.

method on_hot_spot.
data lt_bdc type standard table of bdcdata with empty key.
lt_bdc = value #(
( program = '/SAPAPO/SAPRRP_ENTRY' dynpro = '2000' dynbegin = 'X' )
( fnam = 'BDC_CURSOR' fval = '/SAPAPO/RRPIO-SL_PRORDER' )
( fnam = 'BDC_OKCODE' fval = '=ORDTYPE_CHANGE' )
( fnam = '/SAPAPO/RRPIO-SL_CUORDER' fval = '' )
( fnam = '/SAPAPO/RRPIO-SL_PRORDER' fval = 'X' )
( program = '/SAPAPO/SAPRRP_ENTRY' dynpro = '2000' dynbegin = 'X' )
( fnam = 'BDC_CURSOR' fval = '/SAPAPO/RRPIO-DELNRPR' )
( fnam = 'BDC_OKCODE' fval = '=SHOW' )
( fnam = '/SAPAPO/RRPIO-DELNRPR' fval = mt_opr_d[ row ]-ordernr )
).
data(ls_opt) = value ctu_params( dismode = 'E' defsize = 'X' ).
call transaction '/SAPAPO/RRP2' using lt_bdc options from ls_opt.
endmethod.



CLASS-METHODS on_user_command FOR EVENT added_function OF cl_salv_events IMPORTING e_salv_function.

Working with the session Id // ID für den Modus

Sometimes it can be helpful to know the id of the current session     CALL FUNCTION 'TH_GET_CONTEXT_ID'       IMPORTING         cont...