Showing posts with label ABAP OOPs. Show all posts
Showing posts with label ABAP OOPs. Show all posts

Saturday, November 22, 2008

Create transaction on a class method

By Joyjit Ghosh,
Kolkata
, India
.

In this demo I am going to show how to create transaction on a local class method.

Step1: First create a local class in a report from transaction SE38.

REPORT z_demo_oop_jg .

*---------------------------------------------------------------------*
* CLASS create_report DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*

CLASS create_report DEFINITION.

PUBLIC SECTION.

METHODS: main.

PRIVATE SECTION.

DATA: i_data TYPE STANDARD TABLE OF sbook INITIAL SIZE 0.

METHODS: fetch_data,

display_data.

ENDCLASS. "create_report DEFINITION

*---------------------------------------------------------------------*
* CLASS create_report IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*

CLASS create_report IMPLEMENTATION.

METHOD fetch_data.

* Select 100 records from SBOOK table

SELECT * FROM sbook

INTO TABLE i_data

UP TO 100 ROWS.

ENDMETHOD. "fetch_data

METHOD display_data.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_structure_name = 'SBOOK'

TABLES

t_outtab = i_data

EXCEPTIONS

program_error = 1

OTHERS = 2.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

ENDMETHOD. "display_data

METHOD main.

fetch_data( ).

display_data( ).

ENDMETHOD. "main

ENDCLASS. "create_report IMPLEMENTATION

Step2. Now from transaction SE93 create a transaction for the method MAIN as shown in the screen shots given below:

Give a transaction name and press create button.


In the next screen give a description and choose the proper radio button

In the next screen provide report name (where the local class is defined), local class name and method name.


Now save the transaction and execute it.
In this case it will display the report.



This technique can be used to call a method (local class) from another program using statement: call transaction.

EX: call transaction 'Z_OOP'.

Note: In the same way you can create a transaction on method of a global class.

Sunday, July 6, 2008

Data declaration based on data-type declared in Global class

By Joyjit Ghosh,
Kolkata
, India
.

Prob:

In our project we got one requirement where we need to create a data declaration in custom program based on data type (TY_MARA in this case) declared in global class (Z_TEST).

Solution:

As in this case data type is declared as public. So we have created an object of global class and then used that object to refer the data type in our data declaration.

*& Report Z_TEST_DEMO1
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

report z_test_demo1.

data oref type ref to z_test.

create object oref.

data: i_mara type standard table of oref->ty_mara initial size 0
with header line.

select * from mara into table i_mara
up to 10 rows.

check sy-subrc = 0.

loop at i_mara.

write:/ i_mara-matnr.

endloop.


Output:



Note:
This solution only works if data type is declared in the global class as public.