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 linesgt_my p_mxsel.
    
    delete gt_my index p_mxsel.
    message s016(eswith p_mxsel" result cut off
  endif.
  p_mxsel p_mxsel 1.


form trim_mxsel.
  if p_mxsel > gc_max_rowsp_mxsel gc_max_rowsendif.
  if p_mxsel < 1p_mxsel gc_default_rowsendif.
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.


SAP ABAP: Determine Timezone for Plant

    DATA:       lt_tzone TYPE STANDARD TABLE OF tznzone WITH DEFAULT KEY,       l_tzone  TYPE tznzone.     " get time zone for plant   ...