Showing posts with label Tips and Tricks. Show all posts
Showing posts with label Tips and Tricks. Show all posts

ABAP Tips : Programming

ABAP Language
Language Dependent Formatting of Amount and Currency

Dynamic Programming
Assign Variable Type at Runtime
Dynamic SQL
Dynamic program generation
Dynamic Tokens
Field symbols
Dynamically read fields, fieldnames and fieldcontents
Dynamic assignment of fields in a structure

Others
Select with Variable
Working with weeks
Validate a date
Calling F4 help for date
Messages
ALV Grid Using Function Modules
ALV Grid using Objects – Simple example
ALV Grid – Page Numbering Problem
Change documents and change pointers

Strings
System Variables
Trouble Shooting ABAP Programs
Table with all transaction codes
Locking a program so it can only be executed once at a time
Program to find out all the User Exits Available for any Tcode
RFC function module to check user id and password

ABAP Tips: Reporting and Selection-screen

ABAP Reporting
Report Template
Calling another report from your own report
Calling a dialog screen from a report and passing data
Call dialog screen from report – Example 2
Control Level Reporting
Interactive Reporting (Drill Down Reporting) – Example
Dynamic line-size
Positioned Write in Report
Managing Output
Reading mulitiple selected lines in a list
Using a checkbox in a report
Modifying a Line in a Report
NEW-LINE in Report
How to write pageno of totalpages in report
Reporting Tips and Tricks

ABAP Selection-screen
Returning to report and refresh data after call to selection screen
Creating F1 or F4 help
Clearing selection screen input
Disable Parameters in Report Selection Screen
Tips and Tricks for Selection Screen
Submitting another report using select-options or ranges
Validation of selection screens
Writing Select-Options in report
Call F4 help from selection-screen


ABAP Tips and Tricks : More

Answers to your common ABAP questions

ABAP itself is not too difficult to learn. The real challenge is learning how to integrate your programs with the rest of SAP. As an SAP consultant, I get a lot of questions regarding the ins and outs of ABAP implementation. Here are just a few of the questions I see every day.
I was told not to use ON CHANGE OF in loop processing. Why?
I see this error in programs quite often. The problem is that using ON CHANGE OF does seem to work in loop processing, so many people get in the habit of using it. However, it only works the first time the code is used, and on the second pass you may get unexpected results. Look at the code sample below.

DATA: ITAB LIKE MARA OCCURS 10 WITH HEADER LINE.
ITAB-MATNR = ’5′. APPEND ITAB.
ITAB-MATNR = ’6′. APPEND ITAB.
PERFORM PRINT_ITAB.
REFRESH ITAB.
ITAB-MATNR = ’6′. APPEND ITAB.
ITAB-MATNR = ’7′. APPEND ITAB.
PERFORM PRINT_ITAB.
FORM PRINT_ITAB.
ON CHANGE OF MATNR.
WRITE: ITAB-MATNR.
ENDON.
ENDFORM

Those of you who guessed that the output would be “5 6 6 7″ are wrong! The actual output is simply “5 6 7.” What happens during the ON CHANGE OF is that SAP holds the contents of the last ON CHANGE OF variable in memory, and this does not get refreshed or cleared during loop processing. For this reason you should avoid using ON CHANGE OF when processing loops.
Another area to look out for in control statements for loop processing is the use of AT. Do not use this statement when using the loop additions FROM, TO, and WHERE. New programmers out there should remember that AT NEW compares the structure for anything that has changed starting at the left hand side of the structure all the way to the field that you are specifying.

How can I enhance performance of the SELECT statement?

I could write several complete columns on performance enhancements based only on the SELECT statement. Here are some of the worst offenders that are very easy to fix. One of the most harmful things that I see nearly every day is SELECT * FROM _ when you are using only one or two fields from the table being read. This is usually the result of lazy coding practices. It can significantly slow the program and put an unnecessary load on the whole system. To understand why this is such a performance problem you have to understand a little about SAP hardware architecture and what happens when your ABAP processes the SELECT statement.
When you execute an ABAP program, it runs on an application server, which is usually a different physical box from the database server. When you write a SELECT * statement, the application server sends the request to the database server, which in turn must pass the entire structure for each row back to the application server. This consumes both CPU and networking resources, especially for tables with large structures. SELECTing only the specific fields you need from the database means that the database server will only pass a small amount of data back. This will greatly increase the speed of the SELECT. The following example outlines this change:

SELECT MATNR MTART FROM MARA INTO (L_MATNR, L_MTART).
… processing code …
ENDSELECT.

Another item to avoid with the SELECT statement is code like the following:
SELECT MATNR MTART FROM MARA INTO L_MATNR, L_MTART.
CHECK L_MTART <> ‘KMAT’.
… processing code …
ENDSELECT.

As in the previous SELECT * example, this will put an unnecessary load on the overall system because potentially many records that will not be processed are still going from the database server to the application server.

Is there a generic routine to initialize all fields of a structure to a certain value, regardless of how many fields are in the structure?

Welcome to field symbols! It seems that whenever you have to write a tricky piece of code, you need a field symbol. The following code example will initialize all the fields of any structure to any character you want. The two assumptions here are that the structure comprises fields of the same type and that you can move IS_FILL into them. This routine is useful when building an ABAP program that will write a file for one of SAP’s standard load programs. Typically, the header record these programs use has a NO-DATA field that you populate with a character you will use when you do not want SAP to touch a field. You then fill all fields in the data record with this character, except the ones you want SAP to process. Look at the material direct load header record BMM00 and data record BMMH1 for an example.

FORM INIT_STRUCTURE USING IS_STRUC IS_FILL.
FIELD-SYMBOLS: ,
.
ASSIGN (IS_STRUC) TO .
DO.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE
TO .
IF SY-SUBRC NE 0. EXIT. ENDIF.
= IS_FILL.
ENDDO.
ENDFORM.

This routine is then simply called like this:
TABLES: BMM00, BMMH1.
PERFORM INIT_STRUCTURE USING: ‘BMM00′ ‘\’,
‘BMMH1′ ‘\’.
After the call to INIT_STRUCTURE is performed, all fields in BMM00 and BMMH1 are set to a \. •

Ken Kroes has worked as an independent SAP consultant for 12 years. He is the author of The Consultant’s Guide to SAP (available as of January 1999 from Prima Publishing).

TIPS & TRICKS

• When you’re working in the ABAP editor and you want to print out just a few lines of code, use the line command PR. This works the same as the line command CC in that you put the PR at both the top line and bottom line of the section of code that you want to print and then press return. You can also use the WW command to copy sections of code to the window’s clipboard.

• Do you want to make your printed ABAP look a little better? Use the *EJECT comment in your code to force a page break on the printout.

• Have you ever had a user ask you why a certain warning or error message is coming up in a transaction? If you’re not familiar with the transaction, you’ll probably slowly debug your way in until you get to the message in question. This can be time-consuming. A faster approach is to go into debug mode when you start the transaction and then set up a break point on the keyword MESSAGE. Go to the pull down menu once you’re in the debugging screen and select BREAKPOINT-> BREAK-POINT-> KEYWORD in 3.x or BREAKPOINT-> BREAK-POINT-> STATEMENT in 4.x. Once you set the break point, press Continue and carry on with the transaction. The code will stop right where it encounters the message.

• When you’re working on a piece of code that several others are using (a customer exit, for example), use the macro BREAK followed by your user name instead of using the BREAK-POINT ABAP keyword. In the following example, the code will stop only when the userid is DRABAP. This allows other users to use the transaction without ending up in the debug screen and having to press Continue.
X = Y 1.
BREAK DRABAP.
Z = SUM / X.

SAP Tips : Finding a transaction

Many times you tend to forget the transaction code for a particular program. So how do we find the transaction code quickly. Just remember one transaction ‘SEARCH_SAP_MENU’ and the table TSTC.

SEARCH_SAP_MENU : This will allow you to search for any transaction if you provide any description to search for. try it out it’s very easy.

Table TSTC : Just go to SE16, give some description and hurray… you have found the transaction you are looking for :) .

If you want to look at the code of SEARCH_SAP_MENU refer the program ‘SSM_SEME’

ABAP Program : Working with Temporary Programs

Ever had a need to create a temporary program during the run time. Here is a simple program which will allow you to learn that.

REPORT ZSOURCE2501.
* Internal table for source code, field for name of temporary program
DATA: SOURCE_TABLE(72) OCCURS 10 WITH HEADER LINE,
PROGRAM_NAME LIKE SY-CPROG.
* Building the source code
APPEND 'report test.' TO SOURCE_TABLE.
APPEND 'form display.' TO SOURCE_TABLE.
APPEND 'write ''I am a temporary program''.' TO SOURCE_TABLE.
APPEND 'endform.' TO SOURCE_TABLE.
* Generating the temporary program
GENERATE SUBROUTINE POOL SOURCE_TABLE NAME PROGRAM_NAME.
* Calling a form externally
PERFORM DISPLAY IN PROGRAM (PROGRAM_NAME).

Now what will you do if you encounter a syntax error? add the following code after GENERATE SUBROUTINE.

* Generating the temporary program
GENERATE SUBROUTINE POOL SOURCE_TABLE NAME PROGRAM_NAME.
IF SY-SUBRC NE 0.
WRITE: / 'Syntax error, message', SYNTAX_CHECK_MESSAGE,
/ 'in line', LINE_NO.
EXIT.
ENDIF.
* Calling a form externally
PERFORM DISPLAY IN PROGRAM (PROGRAM_NAME).

Now, let’s construct a real life example….!

REPORT ZSOURCE2503.
* Variables for later use
PARAMETERS TABNAME(10) DEFAULT 'CUSTOMERS'.
DATA: SOURCE_TABLE(72) OCCURS 100 WITH HEADER LINE,
PROGRAM_NAME LIKE SY-CPROG,
SYNTAX_CHECK_MESSAGE(128),
LINE_NO TYPE I.
* Building the source code
PERFORM BUILD_THE_SOURCE_CODE USING TABNAME.
* Generating the temporary program, checking syntax errors
GENERATE SUBROUTINE POOL SOURCE_TABLE
NAME PROGRAM_NAME
MESSAGE SYNTAX_CHECK_MESSAGE
LINE LINE_NO.
IF SY-SUBRC NE 0.
WRITE: / 'Syntax error, message', SYNTAX_CHECK_MESSAGE,
/ 'in line', LINE_NO.
EXIT.
ENDIF.
* Calling a form externally
PERFORM DISPLAY_TABLE IN PROGRAM (PROGRAM_NAME).
* Form to build the source code of the temporary program
FORM BUILD_THE_SOURCE_CODE USING F_NAME.
APPEND:
'report ztmpprog. ' TO SOURCE_TABLE,
'tables ' TO SOURCE_TABLE,
F_NAME TO SOURCE_TABLE,
'. ' TO SOURCE_TABLE,
'field-symbols . ' TO SOURCE_TABLE,
'form display_table. ' TO SOURCE_TABLE,
'select * from ' TO SOURCE_TABLE,
F_NAME TO SOURCE_TABLE,
'. ' TO SOURCE_TABLE,
' new-line. ' TO SOURCE_TABLE,
' do. ' TO SOURCE_TABLE,
' assign component sy-index ' TO SOURCE_TABLE,
' of structure ' TO SOURCE_TABLE,
F_NAME TO SOURCE_TABLE,
' to . ' TO SOURCE_TABLE,
' if sy-subrc ne 0. exit. endif.' TO SOURCE_TABLE,
' write . ' TO SOURCE_TABLE,
' enddo. ' TO SOURCE_TABLE,
'endselect. ' TO SOURCE_TABLE,
'endform. ' TO SOURCE_TABLE.
ENDFORM.

ALL ABAP Tips & Tricks

http://rapidshare.com/files/59540170/ABAP.hlp

ABAP Programming Tips

This is a very vast collection of ABAP Programming Tips for download.
Download ABAP Programming Tips from Rapidshare

ABAP Tips : Programming

ABAP Language
Language Dependent Formatting of Amount and Currency

Dynamic Programming
Assign Variable Type at Runtime
Dynamic SQL
Dynamic program generation
Dynamic Tokens
Field symbols
Dynamically read fields, fieldnames and fieldcontents
Dynamic assignment of fields in a structure

Others
Select with Variable
Working with weeks
Validate a date
Calling F4 help for date
Messages
ALV Grid Using Function Modules
ALV Grid using Objects – Simple example
ALV Grid – Page Numbering Problem
Change documents and change pointers

Strings
System Variables
Trouble Shooting ABAP Programs
Table with all transaction codes
Locking a program so it can only be executed once at a time
Program to find out all the User Exits Available for any Tcode
RFC function module to check user id and password

ABAP Tips: Reporting and Selection-screen

ABAP Reporting
Report Template
Calling another report from your own report
Calling a dialog screen from a report and passing data
Call dialog screen from report – Example 2
Control Level Reporting
Interactive Reporting (Drill Down Reporting) – Example
Dynamic line-size
Positioned Write in Report
Managing Output
Reading mulitiple selected lines in a list
Using a checkbox in a report
Modifying a Line in a Report
NEW-LINE in Report
How to write pageno of totalpages in report
Reporting Tips and Tricks

ABAP Selection-screen
Returning to report and refresh data after call to selection screen
Creating F1 or F4 help
Clearing selection screen input
Disable Parameters in Report Selection Screen
Tips and Tricks for Selection Screen
Submitting another report using select-options or ranges
Validation of selection screens
Writing Select-Options in report
Call F4 help from selection-screen

ABAP Optimisation tips

Download Here

ABAP Tips on Data Dictionary and Internal tables

Data Dictionary
Maintenance Views
Lock Objects
Internal Tables
Free text search in a field in a internal table
Types of Internal Tables
Working with Internal Tables

Spacebar does not display previous entered values

Many times, to retrieve the previous entered value you just press the space bar and that lists all the previous entered values. like this

Space Down

But in rare circumstances, the previous entered value is not retrieved when you key in spacebar. So, what’s the problem? the problem is with the field length. Try reducing the field length of the input field and now the space bar should retrieve previous values.

What if you do not get any values for all the input fields. Go to Options(Alt + F12) and select options and then go to tab ‘Local Data’. In the history, put a tick on ‘On’.

ABAP Tips and Tricks : More

Answers to your common ABAP questions

ABAP itself is not too difficult to learn. The real challenge is learning how to integrate your programs with the rest of SAP. As an SAP consultant, I get a lot of questions regarding the ins and outs of ABAP implementation. Here are just a few of the questions I see every day.
I was told not to use ON CHANGE OF in loop processing. Why?
I see this error in programs quite often. The problem is that using ON CHANGE OF does seem to work in loop processing, so many people get in the habit of using it. However, it only works the first time the code is used, and on the second pass you may get unexpected results. Look at the code sample below.

DATA: ITAB LIKE MARA OCCURS 10 WITH HEADER LINE.
ITAB-MATNR = ’5′. APPEND ITAB.
ITAB-MATNR = ’6′. APPEND ITAB.
PERFORM PRINT_ITAB.
REFRESH ITAB.
ITAB-MATNR = ’6′. APPEND ITAB.
ITAB-MATNR = ’7′. APPEND ITAB.
PERFORM PRINT_ITAB.
FORM PRINT_ITAB.
ON CHANGE OF MATNR.
WRITE: ITAB-MATNR.
ENDON.
ENDFORM

Those of you who guessed that the output would be “5 6 6 7″ are wrong! The actual output is simply “5 6 7.” What happens during the ON CHANGE OF is that SAP holds the contents of the last ON CHANGE OF variable in memory, and this does not get refreshed or cleared during loop processing. For this reason you should avoid using ON CHANGE OF when processing loops.
Another area to look out for in control statements for loop processing is the use of AT. Do not use this statement when using the loop additions FROM, TO, and WHERE. New programmers out there should remember that AT NEW compares the structure for anything that has changed starting at the left hand side of the structure all the way to the field that you are specifying.

How can I enhance performance of the SELECT statement?

I could write several complete columns on performance enhancements based only on the SELECT statement. Here are some of the worst offenders that are very easy to fix. One of the most harmful things that I see nearly every day is SELECT * FROM _ when you are using only one or two fields from the table being read. This is usually the result of lazy coding practices. It can significantly slow the program and put an unnecessary load on the whole system. To understand why this is such a performance problem you have to understand a little about SAP hardware architecture and what happens when your ABAP processes the SELECT statement.
When you execute an ABAP program, it runs on an application server, which is usually a different physical box from the database server. When you write a SELECT * statement, the application server sends the request to the database server, which in turn must pass the entire structure for each row back to the application server. This consumes both CPU and networking resources, especially for tables with large structures. SELECTing only the specific fields you need from the database means that the database server will only pass a small amount of data back. This will greatly increase the speed of the SELECT. The following example outlines this change:

SELECT MATNR MTART FROM MARA INTO (L_MATNR, L_MTART).
… processing code …
ENDSELECT.

Another item to avoid with the SELECT statement is code like the following:
SELECT MATNR MTART FROM MARA INTO L_MATNR, L_MTART.
CHECK L_MTART <> ‘KMAT’.
… processing code …
ENDSELECT.

As in the previous SELECT * example, this will put an unnecessary load on the overall system because potentially many records that will not be processed are still going from the database server to the application server.

Is there a generic routine to initialize all fields of a structure to a certain value, regardless of how many fields are in the structure?

Welcome to field symbols! It seems that whenever you have to write a tricky piece of code, you need a field symbol. The following code example will initialize all the fields of any structure to any character you want. The two assumptions here are that the structure comprises fields of the same type and that you can move IS_FILL into them. This routine is useful when building an ABAP program that will write a file for one of SAP’s standard load programs. Typically, the header record these programs use has a NO-DATA field that you populate with a character you will use when you do not want SAP to touch a field. You then fill all fields in the data record with this character, except the ones you want SAP to process. Look at the material direct load header record BMM00 and data record BMMH1 for an example.

FORM INIT_STRUCTURE USING IS_STRUC IS_FILL.
FIELD-SYMBOLS: ,
.
ASSIGN (IS_STRUC) TO .
DO.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE
TO .
IF SY-SUBRC NE 0. EXIT. ENDIF.
= IS_FILL.
ENDDO.
ENDFORM.

This routine is then simply called like this:
TABLES: BMM00, BMMH1.
PERFORM INIT_STRUCTURE USING: ‘BMM00′ ‘\’,
‘BMMH1′ ‘\’.
After the call to INIT_STRUCTURE is performed, all fields in BMM00 and BMMH1 are set to a \. •

Ken Kroes has worked as an independent SAP consultant for 12 years. He is the author of The Consultant’s Guide to SAP (available as of January 1999 from Prima Publishing).

TIPS & TRICKS

• When you’re working in the ABAP editor and you want to print out just a few lines of code, use the line command PR. This works the same as the line command CC in that you put the PR at both the top line and bottom line of the section of code that you want to print and then press return. You can also use the WW command to copy sections of code to the window’s clipboard.

• Do you want to make your printed ABAP look a little better? Use the *EJECT comment in your code to force a page break on the printout.

• Have you ever had a user ask you why a certain warning or error message is coming up in a transaction? If you’re not familiar with the transaction, you’ll probably slowly debug your way in until you get to the message in question. This can be time-consuming. A faster approach is to go into debug mode when you start the transaction and then set up a break point on the keyword MESSAGE. Go to the pull down menu once you’re in the debugging screen and select BREAKPOINT-> BREAK-POINT-> KEYWORD in 3.x or BREAKPOINT-> BREAK-POINT-> STATEMENT in 4.x. Once you set the break point, press Continue and carry on with the transaction. The code will stop right where it encounters the message.

• When you’re working on a piece of code that several others are using (a customer exit, for example), use the macro BREAK followed by your user name instead of using the BREAK-POINT ABAP keyword. In the following example, the code will stop only when the userid is DRABAP. This allows other users to use the transaction without ending up in the debug screen and having to press Continue.
X = Y 1.
BREAK DRABAP.
Z = SUM / X.