Triple A

It's all about ABAP.


Thursday, February 24, 2011

ABAP source scan

Having trouble to implement a RFC? need a piece of sample code but where-use list is not an option? Try this:

Report program: RPR_ABAP_SOURCE_SCAN

Specify package and search string, have a cup of tea and wait for the result.

Monday, February 21, 2011

Charts from XML doc

Simple transformation and CL_GUI_CHART_ENGINE do the magic!

All you need to do are:
1. Create a screen with a custom container.
2. Populate data to internal tables.
3. Create simple transformation based on your needs.
4. Call transformation to obtain XML document (Xstring).
5. Set the XML doc to chart engine object.
6. Render the chart in PBO.

Here's a very helpful SDN blog to show you the way.

Sunday, February 20, 2011

Graph in 3 minutes

Sample Graph:

Function module: GRAPH_MATRIX_3D

This FM calls a 3D SAP business graphic.
It's easy to use, just make sure you spend some time on the FM documentations.

Steps:
1. Create a structure type for your data and an internal table based on it.
TYPES:
BEGIN OF t_data,
company TYPE c LENGTH 15,
q1 TYPE i,
q2 TYPE i,
q3 TYPE i,
q4 TYPE i,
END OF t_data.

2. Create an internal table based on the structure type below:
TYPES :
BEGIN OF t_opt,
options TYPE c LENGTH 30,
END OF t_opt.

3. Populate both tables.
Sample for data table:
w_data-company = 'Company X'.
w_data-q1 = 55.
w_data-q2 = 68.
w_data-q3 = 24.
w_data-q4 = 77.

Sample for option table:
w_opt-options = 'P2TYPE = VB'.
* For more details, please refer to FM documentation.

4. Call FM.
CALL FUNCTION 'GRAPH_MATRIX_3D'
EXPORTING
col1 = 'Quarter 1'
col2 = 'Quarter 2'
col3 = 'Quarter 3'
col4 = 'Quarter 4'
dim1 = 'In Percentage%'
set_focus = 'X'
titl = 'Company growth'
TABLES
data = i_data
opts = i_opt
EXCEPTIONS
OTHERS = 1.




( ' ) VS ( ` )

The difference between " ' " and " ` " in a character literal:

Character sequence within single quote " ' " is character literal while within " ` " is a string literal.
you will notice the difference when trailing space exists.
Char literal ignores the trailing space but string literal preserves it.

Simple test:
DATA v_char TYPE c LENGTH 32.

* v_char will become "text sample".
v_char = 'sample'.
CONCATENATE 'text ' v_char INTO v_char SEPARATED BY space.
WRITE: / v_char.
* v_char will become "text sample".
v_char = 'sample'.
CONCATENATE `text ` v_char INTO v_char SEPARATED BY space.
WRITE: / v_char.




Thursday, February 17, 2011

ABAP Timer - Part 3

Class CL_GUI_TIMER
method: RUN

The known advantage of CL_GUI_TIMER is that it does not occupy resources (like the asynchronous wait or set processes to halt).

Steps to follow:
1. Define a event handler method for event FINISHED of CL_GUI_TIMER.
CLASS LCL_RECEIVER DEFINITION.
PUBLIC SECTION.
METHOD: HANDLE_FINISHED FOR EVENT FINISHED OF CL_GUI_TIMER.
ENDCLASS.

2. Place the code of refresh logic and call the method - RUN to complete the logic(to reactivate the timer) in it's implementation.
CLASS LCL_RECEIVER IMPLEMENTATION.
METHOD HANDLE_FINISHED.
PERFORM REFRESH_ALV.
CALL METHOD TIMER->RUN.
ENDMETHOD.
ENDCLASS.

Note - Object TIMER is created type ref to CL_GUI_TIMER.

3. Remember to register the handler method created.
SET HANDLER RECEIVER->HANDLE_FINISHED FOR TIMER.

Note - Object RECEIVER is created type ref to the class contains related event handler method.

4. Set the interval attribute (in seconds).
TIMER-> INTERVAL

5. Activate the timer.
TIMER->RUN

Sample code as below:
*-----------------------------------------------*
* CLASS lcl_receiver DEFINITION
*-----------------------------------------------*
*
*-----------------------------------------------*
CLASS lcl_receiver DEFINITION.
PUBLIC SECTION.
METHODS:
handle_finished FOR EVENT finished OF cl_gui_timer.
ENDCLASS.

DATA:
i_data TYPE TABLE OF spfli,
o_grid TYPE REF TO cl_gui_alv_grid,
receiver TYPE REF TO lcl_receiver,
timer TYPE REF TO cl_gui_timer.

CONSTANTS:
c_interval TYPE i VALUE 3.

FIELD-SYMBOLS:
LIKE LINE OF i_data.


SELECT * FROM spfli INTO TABLE i_data.
CREATE OBJECT timer.
CREATE OBJECT receiver.
SET HANDLER receiver->handle_finished FOR timer.
timer->interval = c_interval.
CALL METHOD timer->run.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_structure_name = 'SPFLI'
TABLES
t_outtab = i_data.

*------------------------------------------------*
* CLASS lcl_receiver IMPLEMENTATION
*------------------------------------------------*
*
*------------------------------------------------*
CLASS lcl_receiver IMPLEMENTATION.
METHOD handle_finished.
PERFORM refresh_view.
CALL METHOD timer->run.
ENDMETHOD.
ENDCLASS.

*&-----------------------------------------------*
*& Form refresh_view
*&-----------------------------------------------*
* Refresh logic
*------------------------------------------------*
FORM refresh_view.
* to simulate data updates
LOOP AT i_data ASSIGNING .
-period = -period + 1.
ENDLOOP.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = o_grid.

IF o_grid IS NOT INITIAL.
CALL METHOD o_grid->refresh_table_display.
ENDIF.
ENDFORM.

ABAP Timer - Part 2

Function Module - RFC_PING_AND_WAIT
This is the way to use it, the code is pretty self-explanatory:

CALL FUNCTION 'RFC_PING_AND_WAIT'
STARTING NEW TASK "task id"
PERFORMING "subroutine" ON END OF TASK
EXPORTING
seconds = 3
busy_waiting = ' '.

task id:
specify a character type data object(Max length 8) as a task identifier for the called function module. Eg. '001'

subroutine:
specify a subroutine with exactly one USING parameter (type CLIKE). It will be executed after terminating the FM call.
Note - method call is possible in release 6.2 and above, please check with SAP help :)

Here's the code for a simple timer program:

START-OF-SELECTION.

PERFORM trigger.

AT USER-COMMAND.
CASE sy-ucomm.
WHEN 'TRIGGER'.
PERFORM trigger.
WHEN 'EXIT' OR 'CANCEL' OR 'BACK'.
LEAVE PROGRAM.
ENDCASE.


*&---------------------------------------------------------------------*
*& Form on_finished
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->TASKNAME text
*----------------------------------------------------------------------*
FORM on_finished USING taskname.
SET USER-COMMAND 'TRIGGER'.
ENDFORM. "when_finished


*&---------------------------------------------------------------------*
*& Form trigger
*&---------------------------------------------------------------------*
* Trigger the timer
*----------------------------------------------------------------------*
FORM trigger.
WRITE: / sy-uzeit.
sy-lsind = 0.
CALL FUNCTION 'RFC_PING_AND_WAIT'
STARTING NEW TASK '001'
PERFORMING on_finished ON END OF TASK
EXPORTING
seconds = 3
busy_waiting = ' '.
ENDFORM. "triger

Important Note - In dialog program, you may set an OK_CODE that triggers PAI by using static method CL_GUI_CFW=>SET_NEW_OK_CODE. Eg. Refresh

ABAP Timer - Part 1

Timer?! Wonder why we need this in ABAP?
Here's the example of user requirement in one line:
Real time data is needed when user is viewing this ALV report for better business decisions.
Okay, not really "real-time". For my case, 3 minutes interval is the way.

There are 2 ways to achieve that requirement:

1. Function module RFC_PING_AND_WAIT
2. Class CL_GUI_TIMER