Did you know ?
The purpose of this blog is to reveal few interesting facts and techniques in ABAP that I came across during my ABAP career. While I am pretty sure that most of you are aware of these things I still wanted to publish these things for the benefit of my other ABAP programmer friends who are not aware.
I also urge you to get involved in this blog and share any such interesting facts or techniques in ABAP that can be of any use.
1. The first interesting technique which I am sharing is the use of a Case statement as an alternative for if..else chain
Suppose you have several radio buttons in your selection screen and you need to check which of them has been selected by the user and write logic accordingly. A conventional way of achieving this is by way of using if else statement like below:
If p_rad1 EQ ‘X’.
elseif p_rad2 EQ ‘X’.
elseif p_rad3 EQ ‘X’.
elseif p_rad4 EQ ‘X’.
Now here comes the shortcut. Instead of writing ‘N’ number of if else conditions you can write the code like:
The control will automatically branch to the selected radio button 🙂
2. You can set an icon before the selection text for radio buttons, check boxes and input fields.
Just go to the selection text and put the icon code before the selection text like given below. Save and activate. The icon code can be found in the type pool ICON for each icon.
Once you Run the program, you can see that the icon has been placed accordingly before the selection text.
3. Domain with fixed values ‘X’ and ‘ ‘ is automatically converted as a checkbox.
For example, if you create a PARAMETER in your selection screen which refers to a dictionary domain with fixed values ‘X’ and ‘ ‘ only the system automatically converts it to a checkbox even if you don’t give the keyword CHECKBOX in your PARAMETER statement.
Here the data element WDY_BOOLEAN refers to a data domain WDY_BOOLEAN which has only two fixed values: ‘X’ and ‘ ‘ .
You can see that the parameter is automatically converted to a checkbox even if there was no AS CHECKBOX addition mentioned in the program.
This is true not only in case of selection screen but also in normal dialog screens. If you have created any database table and you have any of your table field referring to a data domain like mentioned above, while generating the Table maintenance generatorthe system automatically converts those fields to checkboxes, provided there are only two domain values existing for that domain i.e. ‘X’ and space. Thus we don’t have to do any additional effort for checkboxes while creating a table maintenance dialog.
4. We can send a pop-up to another SAPGUI user using the function module TH_POPUP
The pop-up opens up at the receiver’s SAPGUI if the user is already logged in.
5. Function codes of a program can be entered directly in the command field (without using /n command)
The function codes of a program in the PF-STATUS can be triggered by entering the function key in the command field. This is similar to triggering the function code by clicking on a button.
A few examples:
Go to SE38, select any program name and enter STRT in the command field (without the prefix /n). You can see that the program has started executing. Now if you check the PF-STATUS for the program you can see that STRT is the function key for Execute button.
Similarly, Go to transaction code CMOD. Now in the command field put the transaction code PRFB (without the prefix /n) and hit enter. You can see that the transaction for field exit is opened in the same window. Likewise we use JDBG in SM37 to debug background jobs.
6. Domain value checks are not considered when a table record is updated from program.
Many people think that restricting data domain with values will still be checked if we update the database table through an INSERT, UPDATE or MODIFY statement. However this is not true. Only foreign key checks are considered in database updates.
Domain checks are only enforced in screens where they are placed. That means if user input data in a screen field which refers to a data domain, the check is done. But if you are inserting records to any database table from a program directly, this check is not enforced thereby resulting in updating records with invalid domain values. So we have to be sure that the data we update is correct.
7. ABAP editor shortcuts
In his blog http://scn.sap.com/community/abap/blog/2013/09/17/shortcuts-that-can-make-an-abaper-life-easier, Yuvaraj S has mentioned numerous keyboard shortcuts that indeed make an ABAP’ers life easier 🙂 Hats off to you Yuvaraj !!!
8. CL_GUI_FRONTEND_SERVICES, a powerful class
Ever wondered how we could get OS level information from SAP system such as Operating System Platform, Computer Name, IP Address etc or perform file operations like File execute (open a file), File Copy, File delete etc. or calculate how much drive space is free in the drive. Well this is a powerful class which gives you all of these. You get almost every detail by calling the static methods of this class.
9. Using lines keyword instead of DESCRIBE TABLE.
To get the number of records in an internal table, we generally use the statement DESCRIBE TABLE itab LINES count.
We can also get the number of records as follows:
count = lines( itab ).
10. Creating tables with time dependency.
Did you know that we could create a database table with de-limit option ? Well if you want to create such a table with time dependent entries do the follow steps:
i) First create your table with required fields. The table/view must also contain two fields for the begin and end dates. One of these fields must be in the key, the other must be the first non-key field in the table/view. Here I have made ENDDA as the key and BEGDA as the non key which comes immediately after ENDDA.
(For more information check SAP documentation: Time-Dependent Table/View – Customizing (BC-CUS) – SAP Library )
ii) Now after generating the table maintenance dialog for your table through table maintenance generator, activate time dependency from Menu->Environment->Generate Time-Dep.
iii) Now go to SM30 and click on Maintain button, you can see that a “DELIMIT” button has been automatically generated by the system.
iv) Now if I enter a record which overlaps with the existing BEGDA – ENDDA dates system will give an error like this:
v) Now press enter and save the records. Now open the table in SE16 and you can see that the system has de-limited the entries according to the new record.
However if you open SM30 again with the same table, you can see that the system will show only those valid records which are valid as on current date.
You can also use the delimit button to delimit entries likewise.
11. Displaying ALV without creating field catalog
We usually create the field catalog for an ALV before calling the ALV output through function modules likes REUSE_ALV_GRID_DISPLAY, REUSE_ALV_GRID_DISPLAY_LVC etc. The main purpose of the field catalog is to set field information like field position in output, column text, output length and various other properties. But what if you don’t want to mention these properties explicitly. You want it based on your data internal table only.
To be more precise, in cases where I want the field position or other field properties to be derived from my internal table which contains the data, I can use the ALV object model to create an ALV output without creating the field catalog. The field properties are automatically derived from your internal table which contains the data. The field position comes from the field position in the internal table and likewise other properties like column text will come from the data element to which your internal table field refers to.
Well most of you are aware of this ALV object model and how it is called. For those of you who are not aware, this is all you have to do. Check the example below. All you have to do is call two methods, that’s it !!!
1. Call method FACTORY to create the ALV object.
2. To display the ALV output on the screen, call up the method DISPLAY afterwards.
data: gt_outtab type table of SFLIGHT.
data: gr_table type ref to cl_salv_table.
*… Select Data
select * from SFLIGHT into corresponding fields of table gt_outtab.
*… Create Instance
call method cl_salv_table=>factory
R_SALV_TABLE = gr_table
t_table = gt_outtab.
*… Display Table
Similarly you can call a Hierarchical ALV, Tree ALV etc. Please check the SAP help for more information:
Finally as I said, these were few things which I had come across during my programming days. I am sure that you will also have something to share.
Lets collaborate, share and learn in SCN !!!
Thanks and Regards,