Thursday, May 24, 2012

Get Current Function Name


Dynamic programming is essential for keeping code clean, tidy and reusable. For this purpose we always need to get information about objects, types, elements (whatever) at runtime. For example, you have to log some kind of event and you need to include the report name which triggered the related event. Basically ABAP developers check sy fields in the first place. In this situation we can get the solution by using sy-repid however when you need the current function name at runtime that variable gives the function group program name which won't work for us. For finding function name at runtime, you can use the code below.




  DATA lt_callstack TYPE sys_callst ,
         ls_callstack LIKE LINE OF lt_callstack.

*** Get Stack
  CALL FUNCTION 'SYSTEM_CALLSTACK'
    EXPORTING
      max_level    0
    IMPORTING
      et_callstack lt_callstack.

  LOOP AT lt_callstack INTO ls_callstack
      WHERE eventtype 'FUNC'" Get Last FUNC Call
    EXIT.
  ENDLOOP.

*** Set Function Name
  e_function_name ls_callstack-eventname.

Thursday, May 17, 2012

Get Payslip PDF



*** Data
DATA gv_seqnr LIKE pa0001-seqnr,
       gv_pernr LIKE pa0001-pernr.

DATA gt_form      LIKE pc408 OCCURS WITH HEADER LINE,
       gs_info      LIKE pc407 ,
       gs_return TYPE bapireturn1.


*** Function
CALL FUNCTION 'GET_PAYSLIP'
  EXPORTING
    employee_number gv_pernr
    sequence_number gv_seqnr
    payslip_variant 'PDF'
  IMPORTING
    return          gs_return
    p_info          gs_info
  TABLES
    p_form          gt_form[].

Wednesday, May 16, 2012

Reverse Goods Issue

This code snippet shows you how to reverse goods issue.It is pretty simple.


DATA lv_vbtyp TYPE likp-vbtyp,
       lv_vbeln TYPE vbuk-vbeln.

DATA lt_mesg TYPE TABLE OF mesg ,
       ls_mesg LIKE LINE OF lt_mesg.


lv_vbeln '10000012'.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    input  lv_vbeln
  IMPORTING
    output lv_vbeln.

SELECT SINGLE vbtyp FROM likp
         INTO lv_vbtyp
        WHERE vbeln lv_vbeln" Check Document
CHECK sy-subrc EQ 0.  " Check Document

SELECT SINGLE COUNT(*FROM vbuk
        WHERE vbeln lv_vbeln
          AND wbstk 'C'.
CHECK sy-subrc EQ 0.  " Check Status.

CALL FUNCTION 'WS_REVERSE_GOODS_ISSUE'
  EXPORTING
    i_vbeln                   lv_vbeln
    i_budat                   sy-datum
    i_vbtyp                   lv_vbtyp
    i_tcode                   'VL09'
  TABLES
    t_mesg                    lt_mesg
  EXCEPTIONS
    error_reverse_goods_issue 1
    OTHERS                    2.
IF sy-subrc EQ 0.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait 'X'.
ELSE.
  LOOP AT lt_mesg INTO ls_mesg" Get Messages
  ENDLOOP.
ENDIF.