Saturday, December 29, 2007

Working with events in a Global Class

I would like to explain about Working with Events in Global Class” .

Go to Class Builder “SE24”.

Provide class name.

Press create button.

Save it.

Go to event tab.

Then provide event method.

Provide parameters also for this method.

Save it.

Then go to methods option.

We wouldn’t be able to write any code in the events directly.

For this we can create another method in the method tab.

Then provide link between method and also the event method.

Then we can click on this detail view button.

Then enable the event handler for check box.

Provide the class name and also the event name.

Save & activate. Following screen appears:




Now select the method.

And also copy the parameters of the event method.

By pressing this copy event parameter we can get the parameters.

Save and go back to the earlier screen..

Then double click on the method name.

Then provide the following logic for triggering the event.

METHOD METHOD_EVENT .

*check the condition

IF S_LIFNR_LOW <> 2000.

MESSAGE I000(0) WITH 'enter the values between 1000 and 2000'.

RAISE EVENT ZEVENT_METHOD.

ENDIF.

*provide select statement

SELECT *

FROM LFA1

INTO TABLE IT_LFA1

WHERE LIFNR BETWEEN S_LIFNR_LOW AND S_LIFNR_HIGH.

*transfer the values to another internal table

IT_LFA11 = IT_LFA1.

ENDMETHOD.

After that provide the logic in se38.

REPORT ZCL_EVENT_OPERATION .

*provide data objects

DATA: LFA1 TYPE LFA1,

OBJ TYPE REF TO ZCL_EVENT_OPERATION,

IT_LFA1 TYPE Z_LFA1,

IT_LFA11 TYPE Z_LFA1,

WA_LFA1 TYPE LFA1.

*provide select statement

SELECT-OPTIONS: S_LIFNR FOR LFA1-LIFNR.

*provide create object

START-OF-SELECTION.

CREATE OBJECT OBJ.

*call the method

CALL METHOD OBJ->METHOD_EVENT

EXPORTING

S_LIFNR_LOW = S_LIFNR-LOW

S_LIFNR_HIGH = S_LIFNR-HIGH

IT_LFA1 = IT_LFA1.

*provide attribute value

IT_LFA11 = OBJ->IT_LFA11.

*display the data

LOOP AT IT_LFA11 INTO WA_LFA1.

WRITE:/ WA_LFA1-LIFNR,

WA_LFA1-LAND1,

WA_LFA1-NAME1,

WA_LFA1-ORT01.

ENDLOOP.

Save it, check it, activate it and execute it.

Then the output is like this.

If lifnr value is <1000>2000.

Then press execute it.

The output is like this.

Then press enter.

The output is like this.



Events in Table Maintenance

Scenario: We have a following custom table which contains the fields “Date on which record was created” and “Name of the person who created the object”. We would like to have these to be filled up with SY-DATUM and SY-UNAME respectively.

Go to Table Maintenance Generator:

Enter the details as shown below:

Now click on Environment -> Modification -> Events

Following screen is displayed.

Click on F4. Following entries are displayed:

Here you can observe that there are different types of events available like before saving the data, after saving the data, before deleting, after deleting and others. Let us go with “Creating a new entry”.

Click on the button in the Editor column and enter the following code: (Please note that you should also code FORM and ENDFORM as well).

Save and activate the table.

Testing the scenario:

Go to SM30 and try creating new entries. Do not enter the values for “Created on” and “Created by”.

Check your entries in the database table. You can observe that the date and user name are automatically filled-in. See the screenshot below:

Archiving IDocs

This document details about the step-by-step process in archiving the IDocs. Archiving is the process of offloading “Out-of-date” data onto a file at the operating system. The following are the two important reasons for archiving:

  • Improving the performance
  • Saving the disk space on the SAP Server

Scenario: In this scenario, let us archive the IDocs created before Jan 1st 1995.

Let us view at the IDocs created on or before 31st Dec 1994. Go to transaction WE05 and provided necessary details:

Press Execute. Following are the IDocs available in the given date range.

You can observe that all the above IDocs are in error state. The IDocs in error state cannot be archived. However, we would leave this part to the end of the document and proceed further.

Initial settings to archive IDocs: (Maintaining Logical path and Physical path)

Go to Transaction FILE

Click on New Entries.

To assign physical path: Select the Z logical path created above and now double-click on the assignment of physical paths on the left side of the screen.

Click on New Entries and enter the following data

Leave the file name as shown above.

Maintaining File Names: Now click on “Logical file name definition” and click on new entries. Provide the details as shown below:



Archiving of IDocs: There are two methods of archiving IDocs:

  • Using the transaction SARA
  • Using the standard programs like RSEXARCA (for archiving), RSEXARCR (for reading) and RSEXARCL (reloading of data into the system)

This document would detail the procedure using the transaction SARA.

Go to transaction SARA and the enter the Archiving Object name as IDOC

Press Enter. You could observe some difference in the screen as shown below:

Click on Write button.

Click on Maintain. Enter a name for your variant.

Enter the information as shown below:

Click on Continue. Now in this screen, you would mention the data or the IDocs that needs to be archived.

Click on attributes.

Click on Save and return to main screen. Now we need to mention the start date for the archive to begin. Click on button “Start Date”.

Here we are scheduling the archival process immediate.

Save your entries. Now click on Execute button:

Following message appears:

From the above screenshot, you can observe that the IDOC could not be archived. The IDocs shouldn’t be in error status for the IDocs to be archived.

To change the status of the IDocs, execute the program RC1_IDOC_SET_STATUS.

Enter the list of IDocs and the new status

Click on Execute.

Now check in WE05 for the change in the IDoc status:

Now try executing the transaction SARA and archive the IDocs.

Using Sorted table and Index while processing Internal tables

There would have been many instances where we would have to process large entries in an internal table with a WHERE condition. This article is intended to demonstrate the comparison between three different methods in handling this situation.

First Method: The normal method used by most of us. Standard internal table processing using WHERE condition

Second Method: Same as above, but here we would be using the Sorted table

Third Method: Sorted table and using the Index

Following is the demo program illustrating the above three methods:

REPORT ZINTERNAL_TABLE_OPERATIONS.
* Program to find the best method in reading the internal tables
* Author: Suresh Kumar Parvathaneni
* Type declaration
TYPES:
BEGIN OF TY_MARA,
MATNR LIKE MARA-MATNR,
MTART LIKE MARA-MTART,
END OF TY_MARA.
* Internal table declaration
DATA:
T_MARA TYPE STANDARD TABLE OF TY_MARA,
T_MARA1 TYPE SORTED TABLE OF TY_MARA
WITH NON-UNIQUE KEY MTART.
* Variable declaration
DATA:
W_COUNTER TYPE I,
W_RUNTIME1 TYPE I,
W_RUNTIME2 TYPE I,
W_TABIX LIKE SY-TABIX.
* Table workarea definition
DATA:
WA_MARA TYPE TY_MARA.
SELECT MATNR                           " Material Number
MTART " Material Type
FROM MARA
INTO TABLE T_MARA.
T_MARA1[] = T_MARA[].
* CASE 1: Processing internal table using LOOP..WHERE Condition
GET RUN TIME FIELD W_RUNTIME1.
LOOP AT T_MARA INTO WA_MARA WHERE MTART EQ 'FHMI'.
ADD 1 TO W_COUNTER.
ENDLOOP.
GET RUN TIME FIELD W_RUNTIME2.
* Calculate Runtime
W_RUNTIME2 = W_RUNTIME2 - W_RUNTIME1.
WRITE W_RUNTIME2.
CLEAR W_COUNTER.
* CASE 2: Using a Sorted table
GET RUN TIME FIELD W_RUNTIME1.
LOOP AT T_MARA1 INTO WA_MARA WHERE MTART EQ 'FHMI'.
ADD 1 TO W_COUNTER.
ENDLOOP.
GET RUN TIME FIELD W_RUNTIME2.
* Calculate Runtime
W_RUNTIME2 = W_RUNTIME2 - W_RUNTIME1.
WRITE W_RUNTIME2.
CLEAR W_COUNTER.
* CASE 3: Using INDEX on a sorted table
GET RUN TIME FIELD W_RUNTIME1.
READ TABLE T_MARA1 INTO WA_MARA WITH KEY MTART = 'FHMI'.
IF SY-SUBRC EQ 0.
W_TABIX = SY-TABIX + 1.
ADD 1 TO W_COUNTER.
LOOP AT T_MARA1 INTO WA_MARA FROM W_TABIX.
IF WA_MARA-MTART NE 'FHMI'.
EXIT.
ENDIF.
ADD 1 TO W_COUNTER.
ENDLOOP.
ENDIF.
GET RUN TIME FIELD W_RUNTIME2.
* Calculate Runtime
W_RUNTIME2 = W_RUNTIME2 - W_RUNTIME1.
WRITE W_RUNTIME2.

Following is the analysis report in microseconds, as per the data volume:

Records: 21,390

Iteration No

Using Normal LOOP & WHERE

Using Sorted table LOOP & WHERE

Using INDEX on Sorted table

1

897

887

11

2

839

879

10

3

839

877

10

4

834

880

9

5

842

837

10

Records: 132,693

Iteration No

Using Normal LOOP & WHERE

Using Sorted table LOOP & WHERE

Using INDEX on Sorted table

1

34239

35774

3567

2

34271

38250

3592

3

34492

36534

3554

4

34198

35695

3584

Sorted table might have given a better performance here if the field in the WHERE condition is the first field in the internal table. However, from the above statistics, we can say that method 3 is better than the other 2 methods. In production environment, the data would be huge and the performance could be much improved with this simple technique.

Highlighting only a particular cell instead of entire row in ALV Grid

Let us execute the standard program: BCALV_DEMO_TOOLTIP:

On the selection screen, select the radio button “Output as Grid” and press Execute.

Output displayed:

Now let us select any cell in the ALV list:

From the above screenshot, you can observe that entire row is highlighted.

Instead of selecting the entire row, let us modify the program to highlight only the selected cell.

Copy the standard program BCALV_DEMO_TOOLTIP to a ‘Z’ program and make the following modifications:

The line number at which the modifications are to be made is shown in the above screenshot.

Save and activate the program. Now execute the program again and select any particular cell:

From the above screenshot, you can observe that only the selected cell is highlighted.

Blog Archive